c++ - 如何调整 ICU 的 UnicodeString::caseCompare(或获得相同的效果)
问题描述
我不太熟悉大小写折叠/不区分大小写的比较如何工作,以及一般的 ICU。
现在,我们有一些包装各种重载的方法,UnicodeString::caseCompare
我想改变它们来做一些稍微不同的事情:我想要 dotted & dotless i 来比较相等(不管大小写)。
我知道 ICU 有一个整理 API,但我不确定如何从与 完全相同的规则开始UnicodeString::caseCompare
,然后从那里进行修改。
解决方案
我看不到使用 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)
推荐阅读
- rsync - 对传递的文件使用 rsync
- javascript - 使用 websocket 发送 blob
- git - 荒谬的 GitHub 行为显示两个极其不同的分支之间没有差异
- c++ - 如何在运行时设置纪元创建可实例化的自定义时钟
- c# - 以编程方式禁用 WPF RichTextBox 自动文本换行的更好方法?
- javascript - 能不能去掉 Mocha 报的毫秒级速度?
- node.js - MERN如何检查连接Node,React和数据库的登录
- pip - Statsd 和 Airflow 安装步骤
- c# - Android java.io.IOException即使在读取Mifare Classic时认证成功
- python - 使用 SocketIO 关闭 Change Stream Mongo db 的正确方法 - Flask