GetUtf8StrLen
size_t GetUtf8StrLen(const char* pStr)
{
size_t nLen = 0;
char c = " ";
while (" " != (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;
}
{
size_t nLen = 0;
char c = " ";
while (" " != (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;
}
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: 【总结】C#中的数组
- 下一篇: MySQL优化必须调整的10项配置