是否有与“ memchr”相似的函数,可以将每个炭与位掩码匹配,而不是确切的平等
Is there a function with similar performance to `memchr` that can match each char with a bit-mask instead of for exact equality?
我要解决的实际问题是搜索位模式的出现( 110xxxxx
, 1110xxxx
, 11110xxx
),这表明UTF-8中的多比例字符的开始。
我希望找到与memchr()
相似的东西,但尚未设法找到任何东西。我自己不能写大会,因为它必须是便携的。
您正在尝试找到第一个UTF-8序列启动字节。测试是:
(c >= 0xC0 && c <= 0xF7)
这可以通过每个字节进行单个测试来有效地完成:
void *memfind_start_byte(const void *p, size_t len) {
unsigned char *s = (unsigned char *)p;
while (len-- > 0) {
if ((unsigned char)(*s++ - 0xC0) <= 0xF7 - 0xC0)
return (void *)(s - 1);
}
return NULL;
}
通过优化编译器,可以自动将此循环自动传输。
您可以使用比特的技巧一次检查多个字节,就像strlen()
:对齐源指针后,您可以一次检查8个字节:
if (*(uint64_t*)p & 0x8080808080808080) {
/* one byte might match: write 8 tests */
}
这是未经测试的尝试:
void *memfind_start_byte(const void *p, size_t len) {
unsigned char *s = (unsigned char *)p;
while (((uintptr_t)s & 7) && len-- > 0) {
if ((unsigned char)(*s++ - 0xC0) <= 0xF7 - 0xC0)
return (void *)(s - 1);
}
for (; len >= 8; len -= 8, s += 8) {
if (*(uint64_t *)s & 0x8080808080808080) {
if ((unsigned char)(s[0] - 0xC0) <= 0xF7 - 0xC0) return (void *)(s + 0);
if ((unsigned char)(s[1] - 0xC0) <= 0xF7 - 0xC0) return (void *)(s + 1);
if ((unsigned char)(s[2] - 0xC0) <= 0xF7 - 0xC0) return (void *)(s + 2);
if ((unsigned char)(s[3] - 0xC0) <= 0xF7 - 0xC0) return (void *)(s + 3);
if ((unsigned char)(s[4] - 0xC0) <= 0xF7 - 0xC0) return (void *)(s + 4);
if ((unsigned char)(s[5] - 0xC0) <= 0xF7 - 0xC0) return (void *)(s + 5);
if ((unsigned char)(s[6] - 0xC0) <= 0xF7 - 0xC0) return (void *)(s + 6);
if ((unsigned char)(s[7] - 0xC0) <= 0xF7 - 0xC0) return (void *)(s + 7);
}
}
while (len-- > 0) {
if ((unsigned char)(*s++ - 0xC0) <= 0xF7 - 0xC0)
return (void *)(s - 1);
}
return NULL;
}
相关文章:
- 位移操作和位掩码未检测到重复字符
- OpenCV - 带有掩码的absdiff
- 生成前缀位掩码
- 如何从__m64值的 lsb 创建 8 位掩码?
- 如何对无符号长 int 进行位掩码?
- 删除K的背景掩码-意味着Python或C++中的集群/
- 如何在C++中优雅地处理位掩码
- 将uint64_t位掩码转换为 std::布尔数组
- 使输入二进制掩码适应 ITK 网格生成器
- 如何从 getifaddr 读取子网掩码
- 优化从子位掩码生成父位掩码
- 基于模式创建位掩码作为 constexpr
- 使用二进制掩码 C++ ITK 获取感兴趣区域
- C++中的运行时位复制(位掩码)
- 根据 IP 和掩码C++打印所有 IP
- C++设置"blank"或重置 ifstrean (ios) 的异常掩码
- OpenCV 检测带有掩码的斑点
- OPENCV:如何创建多边形形状的掩码
- 递归函数,用于使用位掩码 c++ 显示集合的所有子集
- 是否有与“ memchr”相似的函数,可以将每个炭与位掩码匹配,而不是确切的平等