哪些开源C或c++库可以将任意UTF-32转换为NFC

What open source C or C++ libraries can convert arbitrary UTF-32 to NFC?

本文关键字:任意 UTF-32 转换 NFC 开源 c++      更新时间:2023-10-16

哪些开源C或c++库可以将任意UTF-32转换为NFC?

我认为到目前为止可以做到这一点的库:ICU, Qt, GLib(不确定?)。

我不需要任何其他复杂的Unicode支持;从任意但已知正确的UTF-32转换为NFC格式的UTF-32。

我最感兴趣的是一个可以直接做到这一点的库。例如,Qt和ICU(据我所知)都通过与UTF-16之间的中间转换阶段来完成所有工作。

ICU或Boost。现场(包装ICU)将是你最好的一个非常,非常长的路要走。规范化映射将与来自更多软件的映射等效,我认为这是此转换的重点。

这是我在决定使用ICU后最终使用的代码的主要部分。我想我应该把它放在这里,以防它对尝试同样事情的人有所帮助。

std::string normalize(const std::string &unnormalized_utf8) {
    // FIXME: until ICU supports doing normalization over a UText
    // interface directly on our UTF-8, we'll use the insanely less
    // efficient approach of converting to UTF-16, normalizing, and
    // converting back to UTF-8.
    // Convert to UTF-16 string
    auto unnormalized_utf16 = icu::UnicodeString::fromUTF8(unnormalized_utf8);
    // Get a pointer to the global NFC normalizer
    UErrorCode icu_error = U_ZERO_ERROR;
    const auto *normalizer = icu::Normalizer2::getInstance(nullptr, "nfc", UNORM2_COMPOSE, icu_error);
    assert(U_SUCCESS(icu_error));
    // Normalize our string
    icu::UnicodeString normalized_utf16;
    normalizer->normalize(unnormalized_utf16, normalized_utf16, icu_error);
    assert(U_SUCCESS(icu_error));
    // Convert back to UTF-8
    std::string normalized_utf8;
    normalized_utf16.toUTF8String(normalized_utf8);
    return normalized_utf8;
}