我实现了两种
UTF-8<->
UTF-16<->
UTF-32之间的转换变体,第一种变体完全从头开始实现了所有转换,第二种使用标准
std::codecvt和
std::wstring_convert(这两个类从C++17开始已被弃用,但仍然存在,并保证在C++11/C++14中存在)。
如果您不喜欢我的代码,则可以使用几乎单个头文件的C ++库
utfcpp,该库应该经过许多客户的充分测试。
要将UTF-8转换为UTF-16,只需调用
Utf32To16(Utf8To32(str))
,要将UTF-16转换为UTF-8,请调用
Utf32To8(Utf16To32(str))
。或者您可以使用我的便捷助手函数
UtfConv<std::wstring>(std::string("abc"))
进行UTF-8到UTF-16的转换,或者使用
UtfConv<std::string>(std::wstring(L"abc"))
进行UTF-16到UTF-8的转换,实际上,
UtfConv
可以从任何一种UTF编码字符串转换为另一种。在
Test(cs)
宏内部查看这些和其他用法的示例。
这两个变体都符合C++11标准。它们还可以在CLang/GCC/MSVC编译器中编译(请参见下面的“在线试用!”链接),并已经在Windows/Linux操作系统中测试过。
你需要将我的两个代码片段保存在以UTF-8编码的文件中,并向CLang/GCC提供选项
-finput-charset=UTF-8 -fexec-charset=UTF-8
,向MSVC提供选项
/utf-8
。只有在您放置了具有非ASCII字符的文字字符串(例如我为测试目的在代码中放置的内容)时,才需要进行这种UTF-8保存和选项设置。如果要使用函数本身,则不需要进行此操作。
包含
<windows.h>
、
<clocale>
和
<iostream>
,以及调用
SetConsoleOutputCP(65001)
和
std::setlocale(LC_ALL, "en_US.UTF-8")
仅用于测试目的,以正确设置和输出到UTF-8控制台。这些内容对于转换函数是不必要的。
代码的一部分并不是非常必要,我指的是与UtfHelper相关的结构和函数,它们只是用于转换的辅助函数,主要是为了以跨平台的方式处理std::wstring
而创建的,因为在Linux上wchar_t
通常是32位,而在Windows上是16位。只有低级函数Utf8To32
、Utf32To8
、Utf16To32
、Utf32To16
才是真正需要进行转换的东西。
变体1是根据维基百科对UTF-8和UTF-16编码的描述创建的。
如果您发现错误或任何改进(特别是在变体1中),请告诉我,我会修复它们。
变体1
在线试用!
#include <string>
#include <iostream>
#include <stdexcept>
#include <type_traits>
#include <cstdint>
#ifdef _WIN32
#include <windows.h>
#else
#include <clocale>
#endif
#define ASSERT_MSG(cond, msg) { if (!(cond)) throw std::runtime_error("Assertion (" #cond ") failed at line " + std::to_string(__LINE__) + "! Msg: " + std::string(msg)); }
#define ASSERT(cond) ASSERT_MSG(cond, "")
template <typename U8StrT = std::string>
inline static U8StrT Utf32To8(std::u32string const & s) {
static_assert(sizeof(typename U8StrT::value_type) == 1, "Char byte-size should be 1 for UTF-8 strings!");
typedef typename U8StrT::value_type VT;
typedef uint8_t u8;
U8StrT r;
for (auto c: s) {
size_t nby = c <= 0x7FU ? 1 : c <= 0x7FFU ? 2 : c <= 0xFFFFU ? 3 : c <= 0x1FFFFFU ? 4 : c <= 0x3FFFFFFU ? 5 : c <= 0x7FFFFFFFU ? 6 : 7;
r.push_back(VT(
nby <= 1 ? u8(c) : (
(u8(0xFFU) << (8 - nby)) |
u8(c >> (6 * (nby - 1)))
)
));
for (size_t i = 1; i < nby; ++i)
r.push_back(VT(u8(0x80U | (u8(0x3FU) & u8(c >> (6 * (nby - 1 - i)))))));
}
return r;
}
template <typename U8StrT>
inline static std::u32string Utf8To32(U8StrT const & s) {
static_assert(sizeof(typename U8StrT::value_type) == 1, "Char byte-size should be 1 for UTF-8 strings!");
typedef uint8_t u8;
std::u32string r;
auto it = (u8 const *)s.c_str(), end = (u8 const *)(s.c_str() + s.length());
while (it < end) {
char32_t c = 0;
if (*it <= 0x7FU) {
c = *it;
++it;
} else {
ASSERT((*it & 0xC0U) == 0xC0U);
size_t nby = 0;
for (u8 b = *it; (b & 0x80U) != 0; b <<= 1, ++nby) {(void)0;}
ASSERT(nby <= 7);
ASSERT((end - it) >= nby);
c = *it & (u8(0xFFU) >> (nby + 1));
for (size_t i = 1; i < nby; ++i) {
ASSERT((it[i] & 0xC0U) == 0x80U);
c = (c << 6) | (it[i] & 0x3FU);
}
it += nby;
}
r.push_back(c);
}
return r;
}
template <typename U16StrT = std::u16string>
inline static U16StrT Utf32To16(std::u32string const & s) {
static_assert(sizeof(typename U16StrT::value_type) == 2, "Char byte-size should be 2 for UTF-16 strings!");
typedef typename U16StrT::value_type VT;
typedef uint16_t u16;
U16StrT r;
for (auto c: s) {
if (c <= 0xFFFFU)
r.push_back(VT(c));
else {
ASSERT(c <= 0x10FFFFU);
c -= 0x10000U;
r.push_back(VT(u16(0xD800U | ((c >> 10) & 0x3FFU))));
r.push_back(VT(u16(0xDC00U | (c & 0x3FFU))));
}
}
return r;
}
template <typename U16StrT>
inline static std::u32string Utf16To32(U16StrT const & s) {
static_assert(sizeof(typename U16StrT::value_type) == 2, "Char byte-size should be 2 for UTF-16 strings!");
typedef uint16_t u16;
std::u32string r;
auto it = (u16 const *)s.c_str(), end = (u16 const *)(s.c_str() + s.length());
while (it < end) {
char32_t c = 0;
if (*it < 0xD800U || *it > 0xDFFFU) {
c = *it;
++it;
} else if (*it >= 0xDC00U) {
ASSERT_MSG(false, "Unallowed UTF-16 sequence!");
} else {
ASSERT(end - it >= 2);
c = (*it & 0x3FFU) << 10;
if ((it[1] < 0xDC00U) || (it[1] > 0xDFFFU)) {
ASSERT_MSG(false, "Unallowed UTF-16 sequence!");
} else {
c |= it[1] & 0x3FFU;
c += 0x10000U;
}
it += 2;
}
r.push_back(c);
}
return r;
}
template <typename StrT, size_t NumBytes = sizeof(typename StrT::value_type)> struct UtfHelper;
template <typename StrT> struct UtfHelper<StrT, 1> {
inline static std::u32string UtfTo32(StrT const & s) { return Utf8To32(s); }
inline static StrT UtfFrom32(std::u32string const & s) { return Utf32To8<StrT>(s); }
};
template <typename StrT> struct UtfHelper<StrT, 2> {
inline static std::u32string UtfTo32(StrT const & s) { return Utf16To32(s); }
inline static StrT UtfFrom32(std::u32string const & s) { return Utf32To16<StrT>(s); }
};
template <typename StrT> struct UtfHelper<StrT, 4> {
inline static std::u32string UtfTo32(StrT const & s) {
return std::u32string((char32_t const *)(s.c_str()), (char32_t const *)(s.c_str() + s.length()));
}
inline static StrT UtfFrom32(std::u32string const & s) {
return StrT((typename StrT::value_type const *)(s.c_str()),
(typename StrT::value_type const *)(s.c_str() + s.length()));
}
};
template <typename StrT> inline static std::u32string UtfTo32(StrT const & s) {
return UtfHelper<StrT>::UtfTo32(s);
}
template <typename StrT> inline static StrT UtfFrom32(std::u32string const & s) {
return UtfHelper<StrT>::UtfFrom32(s);
}
template <typename StrToT, typename StrFromT> inline static StrToT UtfConv(StrFromT const & s) {
return UtfFrom32<StrToT>(UtfTo32(s));
}
#define Test(cs) \
std::cout << Utf32To8(Utf8To32(std::string(cs))) << ", "; \
std::cout << Utf32To8(Utf16To32(Utf32To16(Utf8To32(std::string(cs))))) << ", "; \
std::cout << Utf32To8(Utf16To32(std::u16string(u##cs))) << ", "; \
std::cout << Utf32To8(std::u32string(U##cs)) << ", "; \
std::cout << UtfConv<std::string>(UtfConv<std::u16string>(UtfConv<std::u32string>(UtfConv<std::u32string>(UtfConv<std::u16string>(std::string(cs)))))) << ", "; \
std::cout << UtfConv<std::string>(UtfConv<std::wstring>(UtfConv<std::string>(UtfConv<std::u32string>(UtfConv<std::u32string>(std::string(cs)))))) << ", "; \
std::cout << UtfFrom32<std::string>(UtfTo32(std::string(cs))) << ", "; \
std::cout << UtfFrom32<std::string>(UtfTo32(std::u16string(u##cs))) << ", "; \
std::cout << UtfFrom32<std::string>(UtfTo32(std::wstring(L##cs))) << ", "; \
std::cout << UtfFrom32<std::string>(UtfTo32(std::u32string(U##cs))) << std::endl; \
std::cout << "UTF-8 num bytes: " << std::dec << Utf32To8(std::u32string(U##cs)).size() << ", "; \
std::cout << "UTF-16 num bytes: " << std::dec << (Utf32To16(std::u32string(U##cs)).size() * 2) << std::endl;
int main() {
#ifdef _WIN32
SetConsoleOutputCP(65001);
#else
std::setlocale(LC_ALL, "en_US.UTF-8");
#endif
try {
Test("World");
Test("Привет");
Test("");
Test("");
return 0;
} catch (std::exception const & ex) {
std::cout << "Exception: " << ex.what() << std::endl;
return -1;
}
}
输出:
World, World, World, World, World, World, World, World, World, World
UTF-8 num bytes: 5, UTF-16 num bytes: 10
Привет, Привет, Привет, Привет, Привет, Привет, Привет, Привет, Привет, Привет
UTF-8 num bytes: 12, UTF-16 num bytes: 12
, , , , , , , , ,
UTF-8 num bytes: 8, UTF-16 num bytes: 8
, , , , , , , , ,
UTF-8 num bytes: 4, UTF-16 num bytes: 4
变体2
在线尝试!
#include <string>
#include <iostream>
#include <stdexcept>
#include <type_traits>
#include <locale>
#include <codecvt>
#include <cstdint>
#ifdef _WIN32
#include <windows.h>
#else
#include <clocale>
#endif
#define ASSERT(cond) { if (!(cond)) throw std::runtime_error("Assertion (" #cond ") failed at line " + std::to_string(__LINE__) + "!"); }
#if defined(_MSC_VER) && (!_DLL) && (_MSC_VER >= 1900 ) && (_MSC_VER <= 1914 )
std::locale::id std::codecvt<char16_t, char, _Mbstatet>::id;
std::locale::id std::codecvt<char32_t, char, _Mbstatet>::id;
#endif
template <typename U8StrT>
inline static std::u32string Utf8To32(U8StrT const & s) {
static_assert(sizeof(typename U8StrT::value_type) == 1, "Char byte-size should be 1 for UTF-8 strings!");
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> utf_8_32_conv_;
return utf_8_32_conv_.from_bytes((char const *)s.c_str(), (char const *)(s.c_str() + s.length()));
}
template <typename U8StrT = std::string>
inline static U8StrT Utf32To8(std::u32string const & s) {
static_assert(sizeof(typename U8StrT::value_type) == 1, "Char byte-size should be 1 for UTF-8 strings!");
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> utf_8_32_conv_;
std::string res = utf_8_32_conv_.to_bytes(s.c_str(), s.c_str() + s.length());
return U8StrT(
(typename U8StrT::value_type const *)(res.c_str()),
(typename U8StrT::value_type const *)(res.c_str() + res.length()));
}
template <typename U16StrT>
inline static std::u32string Utf16To32(U16StrT const & s) {
static_assert(sizeof(typename U16StrT::value_type) == 2, "Char byte-size should be 2 for UTF-16 strings!");
std::wstring_convert<std::codecvt_utf16<char32_t, 0x10ffff, std::little_endian>, char32_t> utf_16_32_conv_;
return utf_16_32_conv_.from_bytes((char const *)s.c_str(), (char const *)(s.c_str() + s.length()));
}
template <typename U16StrT = std::u16string>
inline static U16StrT Utf32To16(std::u32string const & s) {
static_assert(sizeof(typename U16StrT::value_type) == 2, "Char byte-size should be 2 for UTF-16 strings!");
std::wstring_convert<std::codecvt_utf16<char32_t, 0x10ffff, std::little_endian>, char32_t> utf_16_32_conv_;
std::string res = utf_16_32_conv_.to_bytes(s.c_str(), s.c_str() + s.length());
return U16StrT(
(typename U16StrT::value_type const *)(res.c_str()),
(typename U16StrT::value_type const *)(res.c_str() + res.length()));
}
template <typename StrT, size_t NumBytes = sizeof(typename StrT::value_type)> struct UtfHelper;
template <typename StrT> struct UtfHelper<StrT, 1> {
inline static std::u32string UtfTo32(StrT const & s) { return Utf8To32(s); }
inline static StrT UtfFrom32(std::u32string const & s) { return Utf32To8<StrT>(s); }
};
template <typename StrT> struct UtfHelper<StrT, 2> {
inline static std::u32string UtfTo32(StrT const & s) { return Utf16To32(s); }
inline static StrT UtfFrom32(std::u32string const & s) { return Utf32To16<StrT>(s); }
};
template <typename StrT> struct UtfHelper<StrT, 4> {
inline static std::u32string UtfTo32(StrT const & s) {
return std::u32string((char32_t const *)(s.c_str()), (char32_t const *)(s.c_str() + s.length()));
}
inline static StrT UtfFrom32(std::u32string const & s) {
return StrT((typename StrT::value_type const *)(s.c_str()),
(typename StrT::value_type const *)(s.c_str() + s.length()));
}
};
template <typename StrT> inline static std::u32string UtfTo32(StrT const & s) {
return UtfHelper<StrT>::UtfTo32(s);
}
template <typename StrT> inline static StrT UtfFrom32(std::u32string const & s) {
return UtfHelper<StrT>::UtfFrom32(s);
}
template <typename StrToT, typename StrFromT> inline static StrToT UtfConv(StrFromT const & s) {
return UtfFrom32<StrToT>(UtfTo32(s));
}
#define Test(cs) \
std::cout << Utf32To8(Utf8To32(std::string(cs))) << ", "; \
std::cout << Utf32To8(Utf16To32(Utf32To16(Utf8To32(std::string(cs))))) << ", "; \
std::cout << Utf32To8(Utf16To32(std::u16string(u##cs))) << ", "; \
std::cout << Utf32To8(std::u32string(U##cs)) << ", "; \
std::cout << UtfConv<std::string>(UtfConv<std::u16string>(UtfConv<std::u32string>(UtfConv<std::u32string>(UtfConv<std::u16string>(std::string(cs)))))) << ", "; \
std::cout << UtfConv<std::string>(UtfConv<std::wstring>(UtfConv<std::string>(UtfConv<std::u32string>(UtfConv<std::u32string>(std::string(cs)))))) << ", "; \
std::cout << UtfFrom32<std::string>(UtfTo32(std::string(cs))) << ", "; \
std::cout << UtfFrom32<std::string>(UtfTo32(std::u16string(u##cs))) << ", "; \
std::cout << UtfFrom32<std::string>(UtfTo32(std::wstring(L##cs))) << ", "; \
std::cout << UtfFrom32<std::string>(UtfTo32(std::u32string(U##cs))) << std::endl; \
std::cout << "UTF-8 num bytes: " << std::dec << Utf32To8(std::u32string(U##cs)).size() << ", "; \
std::cout << "UTF-16 num bytes: " << std::dec << (Utf32To16(std::u32string(U##cs)).size() * 2) << std::endl;
int main() {
#ifdef _WIN32
SetConsoleOutputCP(65001);
#else
std::setlocale(LC_ALL, "en_US.UTF-8");
#endif
try {
Test("World");
Test("Привет");
Test("");
Test("");
return 0;
} catch (std::exception const & ex) {
std::cout << "Exception: " << ex.what() << std::endl;
return -1;
}
}
输出:
World, World, World, World, World, World, World, World, World, World
UTF-8 num bytes: 5, UTF-16 num bytes: 10
Привет, Привет, Привет, Привет, Привет, Привет, Привет, Привет, Привет, Привет
UTF-8 num bytes: 12, UTF-16 num bytes: 12
, , , , , , , , ,
UTF-8 num bytes: 8, UTF-16 num bytes: 8
, , , , , , , , ,
UTF-8 num bytes: 4, UTF-16 num bytes: 4
std::wstring_convert
。 - Remy Lebeau