utf8编码是一个变成的编码方式,最小1字节,最长4字节。

如下是python3.10.2中对字符进行utf8校验的函数
/* Check whether the characters at s start a valid
UTF-8 sequence. Return the number of characters forming
the sequence if yes, 0 if not. */
static int valid_utf8(const unsigned char* s)
{
int expected = 0;
int length;
if (*s < 0x80)
/* single-byte code */
return 1;
if (*s < 0xc0)
/* following byte */
return 0;
if (*s < 0xE0)
expected = 1;
else if (*s < 0xF0)
expected = 2;
else if (*s < 0xF8)
expected = 3;
else
return 0;
length = expected + 1;
for (; expected; expected--)
if (s[expected] < 0x80 || s[expected] >= 0xC0)
return 0;
return length;
}
本文介绍了Python3.10.2中用于检查UTF-8编码有效性的函数,该函数通过判断字节序列是否符合UTF-8编码规则来确定字符串的正确性。函数主要针对单字节到四字节的UTF-8编码进行校验,确保编码的正确无误。
55万+

被折叠的 条评论
为什么被折叠?



