Logo Search packages:      
Sourcecode: icu version File versions  Download package

U_STABLE int32_t U_EXPORT2 u_countChar32 ( const UChar *  s,
int32_t  length 
)

Count Unicode code points in the length UChar code units of the string. A code point may occupy either one or two UChar code units. Counting code points involves reading all code units.

This functions is basically the inverse of the U16_FWD_N() macro (see utf.h).

Parameters:
s The input string.
length The number of UChar code units to be checked, or -1 to count all code points before the first NUL (U+0000).
Returns:
The number of code points in the specified code units. ICU 2.0

Definition at line 1004 of file ustring.c.

References NULL, UTF_IS_LEAD, and UTF_IS_TRAIL.

                                              {
    int32_t count;

    if(s==NULL || length<-1) {
        return 0;
    }

    count=0;
    if(length>=0) {
        while(length>0) {
            ++count;
            if(UTF_IS_LEAD(*s) && length>=2 && UTF_IS_TRAIL(*(s+1))) {
                s+=2;
                length-=2;
            } else {
                ++s;
                --length;
            }
        }
    } else /* length==-1 */ {
        UChar c;

        for(;;) {
            if((c=*s++)==0) {
                break;
            }
            ++count;

            /*
             * sufficient to look ahead one because of UTF-16;
             * safe to look ahead one because at worst that would be the terminating NUL
             */
            if(UTF_IS_LEAD(c) && UTF_IS_TRAIL(*s)) {
                ++s;
            }
        }
    }
    return count;
}


Generated by  Doxygen 1.6.0   Back to index