对UTF8编码方式字数做统计的函数


#define UTF8_ASCII(byte) (((unsigned char)(byte)>=0x00)&&((unsigned char)(byte)<=0x7F))
#define UTF8_FIRST(byte) (((unsigned char)(byte)>=0xC0)&&((unsigned char)(byte)<=0xFD))
#define UTF8_OTHER(byte) (((unsigned char)(byte)>=0x80)&&((unsigned char)(byte)<=0xBF))

long utf8_wchar_count(char *buffer, long len)
{
    char *p = 0;
    long count = 0;
    
    if (!buffer || len <= 0) {
        return 0;
    }

    for(p=buffer; p<buffer+len; p++) {
        if (UTF8_ASCII(*p) || (UTF8_FIRST(*p))) {
            count++;
        }
    }

    return count;
}

你可能感兴趣的:(buffer,byte)