有谁知道下面的代码可能做什么?
/* utf-8: 0xc0, 0xe0, 0xf0, 0xf8, 0xfc */
static unsigned char _mblen_table_utf8[] =
{
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1
};
我敢打赌这与编码有关,
但它到底是如何工作的呢?
更新
while (str < ptr)
{
j = mblen[(*str)];
tree_nput(r->tree, cr, sizeof(struct rule_item), str, j);
str += j;
}
}
/* utf-8: 0xc0, 0xe0, 0xf0, 0xf8, 0xfc */
static unsigned char _mblen_table_utf8[] =
{
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1
};
I bet it has something to do with the encodings,
but how exactly it works?
UPDATE
while (str < ptr)
{
j = mblen[(*str)];
tree_nput(r->tree, cr, sizeof(struct rule_item), str, j);
str += j;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于多字节字符串中的字符具有可变长度,因此该表将每个字符映射到一个长度。
最后 64 个字符比 1 个字节宽,长度为
2
到6
。用法是这样的:
Because a character in a multibyte string has a variable length, this table maps each character to a length.
The last 64 characters are wider than one byte, having lengths of
2
to6
.The usage would be something like that:
从历史上看,每个字符都用 7 位(后来是 8 位)进行编码,这足以对欧洲语言字母表进行编码。
只有前 128 个字符对每个人来说都是通用的,其余 128 个通过代码页进行标准化 (ISO-8859 -1 是一个例子)。
对较长字母语言(例如中文)进行编码的需要导致了 Unicode 工作,每个字符都编码在多个字节上。
UTF-8 是一种以高效、可变代码长度的方式对 Unicode 字符进行编码的方法。这意味着您读取的第一个字节决定了字符字节序列的长度。
基本上,您的表是一个查找表,用于检查从用作表索引的字节开始的字符有多少字节。您将在此处看到此表的另一个版本以及说明。
我添加了表索引作为注释以使其更清晰:
Historically, each character was coded on 7 bits (then 8 bits) which was more than enough to encode european languages alphabets.
Only the 128 first characters were common to everyone, the remaining 128 were standardized through codepages (ISO-8859-1 is an example).
The need to encode longer alphabet languages such as Chinese resulted in the Unicode effort were each character is coded on several bytes.
UTF-8 is a way to encode Unicode characters in an efficient, variable code-length way. This means that the first byte you read determines the length of the character byte-sequence.
Basically, your table is a lookup-table to check how many bytes is a character that start from the byte you use as table index. You will see another version of this table here with explanations.
I added the table indexes as comments to make it clearer:
该数组似乎是一个查找表,用于在给定第一个字节的情况下确定 UTF-8 字符中的字节数。基本上,第一个字节(作为无符号值)用作数组的索引,该索引处的元素给出 UTF-8 字符的字节序列的长度。
无效和中间序列字节似乎映射到此表中的 1 字节,因此如果遇到不适当的情况,使用此表的代码可能会将它们视为单个字符(除非它专门忽略它们)。
此类表的一种用途是计算 UTF-8 字符串中的字符(不是字节,而是 Unicode 字符)。每次计算一个字符时,您都会查找长度并向前移动字符字节序列的长度,而不是向前移动一个字节......只要您从字符的开头开始并且字符串是,它就可以正常工作。始终有效的 UTF-8。
The array appears to be a lookup table for determining the number of bytes in a UTF-8 character, given the first byte. Basically the first byte (as an unsigned value) is used as an index into the array, and the element at that index gives the length of the byte sequence for the UTF-8 character.
Invalid and mid-sequence bytes seem to map to 1-byte in this table, so if encountered out of place the code using this table would probably treat them as single characters (unless it specifically ignores them).
One use for a table like this is for counting characters in a UTF-8 string (not bytes, but Unicode characters). Each time you count a character, you look up the length and move ahead by the length of the character's byte sequence instead of moving ahead one byte... it works well as long as you start at the beginning of a character and the string is valid UTF-8 all the way through.
没有任何进一步的细节,上面的代码正是这样做的:它声明一个静态 unsigned char 数组,并使用大括号内的值对其进行初始化。
Without any further details, the code above does exactly this: it declares a static unsigned char array and initializes it with the values inside the curly brackets.