9static bool multiByteToUtf16(
zstring_view multiByte, UINT cp, DWORD dwFlags, std::wstring& utf16)
11 const char* multiByteA = multiByte.
c_str();
12 if (
int len = MultiByteToWideChar(cp, dwFlags, multiByteA, -1,
nullptr, 0)) {
14 int len2 = MultiByteToWideChar(cp, dwFlags, multiByteA, -1, utf16.data(), len);
15 utf16.resize(len - 1);
16 if (len2)
return true;
21static bool utf16ToMultiByte(
const std::wstring& utf16, UINT cp, std::string& multiByte)
23 const wchar_t* utf16W = utf16.c_str();
24 if (
int len = WideCharToMultiByte(cp, 0, utf16W, -1,
nullptr, 0,
nullptr,
nullptr)) {
25 multiByte.resize(len);
26 int len2 = WideCharToMultiByte(cp, 0, utf16W, -1, multiByte.data(), len,
nullptr,
nullptr);
27 multiByte.resize(len - 1);
28 if (len2)
return true;
36 if (!multiByteToUtf16(
utf8, CP_UTF8, MB_ERR_INVALID_CHARS, utf16)) {
38 "MultiByteToWideChar failed: ", GetLastError());
42 if (!utf16ToMultiByte(utf16, CP_ACP, ansi)) {
44 "MultiByteToWideChar failed: ", GetLastError());
52 if (!multiByteToUtf16(
utf8, CP_UTF8, MB_ERR_INVALID_CHARS, utf16))
55 "MultiByteToWideChar failed: ", GetLastError());
60std::string
utf16to8(
const std::wstring& utf16)
63 if (!utf16ToMultiByte(utf16, CP_UTF8,
utf8))
66 "MultiByteToWideChar failed: ", GetLastError());
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
constexpr const char * c_str() const
u16bit_iterator utf8to16(octet_iterator start, octet_iterator end, u16bit_iterator result)
octet_iterator utf16to8(u16bit_iterator start, u16bit_iterator end, octet_iterator result)