如何调整ICU的UnicodeString :: caseCompare(或获得相同的效果)

问题描述 投票:-1回答:1

我不太熟悉案例折叠/不区分大小写的比较如何工作,以及ICU。

现在,我们有一些方法来包装UnicodeString::caseCompareand的各种重载我想改变它们做一些稍微不同的事情:我想要点缀和无点我比较相等(不管情况如何)。

我知道ICU有一个校对API,但我不知道如何从与UnicodeString::caseCompare完全相同的规则开始,并从那里进行修改。

c++ unicode icu
1个回答
0
投票

我没有看到使用C ++ UnicodeString类的方法。

你必须从unicode/ustring.h下载到较低级别的字符串作为数组的UChars函数。特别是,u_strCaseCompare()可能是你想要的,或者u_strcasecmp()结合了UnicodeString的getTerminatedBuffer()方法。

U_FOLD_CASE_EXCLUDE_SPECIAL_I选项的文档:

使用CaseFolding.txt中提供的修改后的映射集来处理针对突厥语(tr,az)的点I和无点i。

我认为这意味着将它们视为等同物。

用实际测试编辑:

#include <stdio.h>
#include <stdlib.h>
#include <unicode/ustring.h>
#include <unicode/stringoptions.h>

void comp(const char *a, const char *b) {
  UChar s1[10], s2[10];
  UErrorCode err = U_ZERO_ERROR;
  int32_t len1, len2;
  u_strFromUTF8(s1, 10, &len1, a, -1, &err);
  u_strFromUTF8(s2, 10, &len2, b, -1, &err);
  printf("%s <=> %s: %d (Without special i) %d (With special i)\n", a, b,
         u_strCaseCompare(s1, len1, s2, len2, 0, &err),
         u_strCaseCompare(s1, len1, s2, len2, U_FOLD_CASE_EXCLUDE_SPECIAL_I, &err));
}

int main(void) {
  const char *lc_dotted_i = "i";
  const char *lc_dotless_i = "\u0131";
  const char *uc_dotless_i = "I";
  const char *uc_dotted_i = "\u0130";

  comp(lc_dotted_i, lc_dotless_i);
  comp(uc_dotted_i, uc_dotless_i);
  comp(lc_dotted_i, uc_dotted_i);
  comp(lc_dotless_i, uc_dotless_i);
  comp(lc_dotted_i, uc_dotless_i);
  comp(lc_dotless_i, uc_dotted_i);
  return 0;
}

结果:

i <=> ı: -200 (Without special i) -200 (With special i)
İ <=> I: 1 (Without special i) -200 (With special i)
i <=> İ: -1 (Without special i) 0 (With special i)
ı <=> I: 200 (Without special i) 0 (With special i)
i <=> I: 0 (Without special i) -200 (With special i)
ı <=> İ: 200 (Without special i) 200 (With special i)
© www.soinside.com 2019 - 2024. All rights reserved.