ICU 允许您使用特定规则音译字符串。我的规则是NFD; [:M:] Remove; NFC
:分解,删除变音符号,重新组合。以下代码将 UTF-8std::string
作为输入并返回另一个 UTF-8 std::string
:
#include <unicode/utypes.h>
#include <unicode/unistr.h>
#include <unicode/translit.h>
std::string desaxUTF8(const std::string& str) {
// UTF-8 std::string -> UTF-16 UnicodeString
UnicodeString source = UnicodeString::fromUTF8(StringPiece(str));
// Transliterate UTF-16 UnicodeString
UErrorCode status = U_ZERO_ERROR;
Transliterator *accentsConverter = Transliterator::createInstance(
"NFD; [:M:] Remove; NFC", UTRANS_FORWARD, status);
accentsConverter->transliterate(source);
// TODO: handle errors with status
// UTF-16 UnicodeString -> UTF-8 std::string
std::string result;
source.toUTF8String(result);
return result;
}