【问题标题】:How to compare ICU iterators values?如何比较 ICU 迭代器的值?
【发布时间】:2021-12-04 06:09:59
【问题描述】:

我正在使用 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 没有就地执行它的功能)

【问题讨论】:

    标签: c icu


    【解决方案1】:

    我不得不为 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-10
      • 1970-01-01
      • 2018-05-31
      • 2013-04-29
      • 2012-03-28
      • 2017-05-02
      • 1970-01-01
      • 2012-03-03
      相关资源
      最近更新 更多