GetUtf8StrLen

来源:互联网 发布:mac中page up 编辑:程序博客网 时间:2024/06/05 18:58
size_t GetUtf8StrLen(const char* pStr)
{
    size_t nLen = 0;
    char c = '\0';
    while ('\0' != (c = *pStr))
    {
        // This char is ascii
        if (0 == (0x80 & c))
        {
            nLen++;
            pStr++;
            continue;
        }
        else
        {
            // This is NOT a utf-8 header char
            if (0 == (0x40 & c))
            {
                pStr++;
                continue;
            }


            // Parse the utf-8 header char to parse the char length
            unsigned char l = ((0xF0 & c) >> 4);
            switch (l)
            {
            case 0xF:// utf-8 char is 4 bytes
                pStr += 4;
                break;
            case 0xE:// utf-8 char is 3 bytes
                pStr += 3;
                break;
            case 0xC:// utf-8 char is 2 bytes
                pStr += 2;
                break;
            }
            nLen++;
        }
    }
    return nLen;
}
0 0
原创粉丝点击