C++中可准確獲得UTF-8字符長度的函數分享。本站提示廣大學習愛好者:(C++中可准確獲得UTF-8字符長度的函數分享)文章只能為提供參考,不一定能成為您想要的結果。以下是C++中可准確獲得UTF-8字符長度的函數分享正文
在C++的char*和string中,應用的是字撙節編碼,即sizeof(char) == 1。
也就是說,C++是不辨別字符的編碼的。
而一個正當UTF8的字符長度能夠為1~4位。
如今假定一串輸出為UTF8編碼,若何能精確的定位到每一個UTF8字符的“CharPoint”,而不會毛病的朋分字符呢?
參考這個頁面:http://www.nubaria.com/en/blog/?p=289
可以改革出上面的函數:
const unsigned char kFirstBitMask = 128; // 1000000
const unsigned char kSecondBitMask = 64; // 0100000
const unsigned char kThirdBitMask = 32; // 0010000
const unsigned char kFourthBitMask = 16; // 0001000
const unsigned char kFifthBitMask = 8; // 0000100
int utf8_char_len(char firstByte)
{
std::string::difference_type offset = 1;
if(firstByte & kFirstBitMask) // This means the first byte has a value greater than 127, and so is beyond the ASCII range.
{
if(firstByte & kThirdBitMask) // This means that the first byte has a value greater than 224, and so it must be at least a three-octet code point.
{
if(firstByte & kFourthBitMask) // This means that the first byte has a value greater than 240, and so it must be a four-octet code point.
offset = 4;
else
offset = 3;
}
else
{
offset = 2;
}
}
return offset;
}