繁体   English   中英

如何比较ICU迭代器值?

[英]How to compare ICU iterators values?

我正在使用 UCharIterators 在 C 中的 unicode 字符串上编写 KMP 子字符串搜索算法,我面临的问题是我需要通过迭代器比较值并且比较应该标准化,而所有 ICU colls 都吸收字符串而不是单个字符。

UCharIterator first_iter, second_iter

uiter_setUTF8( &first_iter, needle_str, n_needle_bytes);
uiter_setUTF8(&second_iter, needle_str, n_needle_bytes);

...
if (firts_iter.current(&first_iter) != second_iter.current(&second_iter)) {
    ...

当前条件在 'a' 和 'ä' 上失败,而我也不想要它。 我不喜欢预规范化的想法,因为它需要 O(n + m) 额外的内存(据我所知,ICU 没有就地执行的功能)

对于 UTF-8 ICU 字符串,我不得不切换到 U8_* 宏。 使用U8_NEXT移动偏移量

U8_NEXT((uint8_t *)string, string_offset, string_size, status);

并且这样比较

U8_GET((uint8_t *)key, 0,  first_key_end, key_size,  first_key_c);
U8_GET((uint8_t *)key, 0, second_key_end, key_size, second_key_c);
if (coll->cmp(key +  first_key_end, U8_LENGTH(first_key_c),
              key + second_key_end, U8_LENGTH(second_key_c),
              coll)

也就是说,通过第一个代码点(而不是偏移量或字符串的一部分)计算出带有U8_LENGTH的单个字母的长度。 更多关于这里https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/utf8_8h.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM