簡體   English   中英

如何在Unicode / UCS代碼點和UTF16代理對之間轉換?

[英]How to convert between a Unicode/UCS codepoint and a UTF16 surrogate pair?

如何在C ++ 14和更高版本中在Unicode / UCS代碼點和UTF16代理對之間來回轉換?

編輯:刪除了對UCS-2替代的提及,因為沒有這樣的東西。 謝謝@ remy-lebeau

標簽信息頁說明了(比Unicode標准9.0在表3-5中的3.9中指定的更好),從代碼點轉換為代理對的算法如下:

基本多語言平面之外的Unicode字符(即代碼大於0xFFFF的字符)通過UTF-16由稱為代理對的16位代碼對對通過以下方案編碼:

  • 從代碼點減去0x010000,剩下20位數字,范圍為0..0x0FFFFF;
  • 前十位(0..0x03FF范圍內的數字)被添加到0xD800,以給出第一個代碼單元或高代理,其范圍為0xD800..0xDBFF;
  • 將低十位(也位於0..0x03FF范圍內)添加到0xDC00以提供第二個代碼單元或低代理,其范圍為0xDC00..0xDFFF。

在C ++ 14及更高版本中,可以這樣寫:

#include <cstdint>

using codepoint = std::uint32_t;
using utf16 = std::uint16_t;

struct surrogate {
    utf16 high; // Leading
    utf16 low;  // Trailing
};

constexpr surrogate split(codepoint const in) noexcept {
    auto const inMinus0x10000 = (in - 0x10000);
    surrogate const r{
            static_cast<utf16>((inMinus0x10000 / 0x400) + 0xd800), // High
            static_cast<utf16>((inMinus0x10000 % 0x400) + 0xdc00)}; // Low
    return r;
}

在相反的方向上,只需要將高代理的最后10位和低代理的最后10位組合起來,並加上0x10000

constexpr codepoint combine(surrogate const s) noexcept {
    return static_cast<codepoint>(
            ((s.high - 0xd800) * 0x400) + (s.low - 0xdc00) + 0x10000);
}

這是對這些轉換的測試:

#include <cassert>

constexpr bool isValidUtf16Surrogate(utf16 v) noexcept
{ return (v & 0xf800) == 0xd800; }

constexpr bool isValidCodePoint(codepoint v) noexcept {
    return (v <= 0x10ffff)
        && ((v >= 0x10000) || !isValidUtf16Surrogate(static_cast<utf16>(v)));
}

constexpr bool isValidUtf16HighSurrogate(utf16 v) noexcept
{ return (v & 0xfc00) == 0xd800; }

constexpr bool isValidUtf16LowSurrogate(utf16 v) noexcept
{ return (v & 0xfc00) == 0xdc00; }

constexpr bool codePointNeedsUtf16Surrogates(codepoint v) noexcept
{ return (v >= 0x10000) && (v <= 0x10ffff); }

void test(codepoint const in) {
    assert(isValidCodePoint(in));
    assert(codePointNeedsUtf16Surrogates(in));
    auto const s = split(in);
    assert(isValidUtf16HighSurrogate(s.high));
    assert(isValidUtf16LowSurrogate(s.low));
    auto const out = combine(s);
    assert(isValidCodePoint(out));
    assert(in == out);
}

int main() {
    for (codepoint c = 0x10000; c <= 0x10ffff; ++c)
        test(c);
}

在C ++ 11和更高版本中,可以使用std::wstring_convert在以下各種std::codecvt類型之間進行各種UTF / UCS編碼之間的轉換:

您無需手動處理代理。

您可以使用std::u32string保留代碼點,並使用std::u16string保留UTF-16 / UCS-2代碼單元。

例如:

using convert_utf16_uf32 = std::wstring_convert<std::codecvt_utf16<char32_t>, char16_t>;

std::u16string CodepointToUTF16(const char32_t codepoint)
{
    const char32_t *p = &codepoint;
    return convert_utf16_uf32{}.from_bytes(
        reinterpret_cast<const char*>(p),
        reinterpret_cast<const char*>(p+1)
    );
}

std::u16string UTF32toUTF16(const std::u32string &str)
{
    return convert_utf16_uf32{}.from_bytes(
        reinterpret_cast<const char*>(str.data()),
        reinterpret_cast<const char*>(str.data()+str.size())
    );
}

char32_t UTF16toCodepoint(const std::u16string &str)
{
    std::string bytes = convert_utf16_uf32{}.to_bytes(str);
    return *(reinterpret_cast<const char32_t*>(bytes.data()));
}

std::u32string UTF16toUTF32(const std::u16string &str)
{
    std::string bytes = convert_utf16_uf32{}.to_bytes(str);
    return std::u32string(
       reinterpret_cast<const char32_t*>(bytes.data()),
       bytes.size() / sizeof(char32_t)
    );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM