對UTF8編碼方式字數做統計的函式

牛晨光發表於2012-03-16

#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;
}

相關文章