如何在C ++ 14及更高版本中在Unicode / UCS代码点和UTF16代理项对之间来回转换?
编辑:删除了对UCS-2代理人的提及,因为没有这样的事情。谢谢@remy-lebeau!
答案 0 :(得分:4)
surrogate-pairs标记info page解释(优于§3.9,表3-5中的Unicode Standard 9.0指定。)从codepoint转换为代理对的算法如下:< / p>
基本多语言平面之外的Unicode字符,即代码大于0xFFFF的字符,由UTF-16编码,由称为代理项对的16位代码单元对,通过以下方案:
从代码点中减去
- 0x010000,在0..0x0FFFFF范围内留下20位数字;
- 前十位(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);
}
答案 1 :(得分:4)
在C ++ 11及更高版本中,您可以使用std::wstring_convert
使用以下std::codecvt
类型在各种UTF / UCS编码之间进行转换:
UTF-8&lt; - &gt; UCS-2:
std::codecvt_utf8<char16_t>
UTF-8&lt; - &gt; UTF-16:
std::codecvt_utf8_utf16
UTF-8&lt; - &gt; UTF-32 / UCS-4:
std::codecvt_utf8<char32_t>
UCS-2&lt; - &gt; UTF-16:
std::codecvt_utf16<char16_t>
UTF-16&lt; - &gt; UTF-32 / UCS-4:
std::codecvt_utf16<char32_t>
UCS-2&lt; - &gt; UTF-32 / UCS-4:
没有标准转换,但如果需要,您可以为它编写自己的std::codecvt
类。否则,请使用上述转换之一:
UCS-2 <-> UTF-X <-> UTF-32/UCS-4
您不需要手动处理代理人。
您可以使用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)
);
}