10static bool multiByteToUtf16(
zstring_view multiByte, UINT cp, DWORD dwFlags, std::wstring& utf16)
12 const char* multiByteA = multiByte.
c_str();
13 if (
int len = MultiByteToWideChar(cp, dwFlags, multiByteA, -1,
nullptr, 0)) {
14 VLA(
wchar_t, utf16W, len);
15 len = MultiByteToWideChar(cp, dwFlags, multiByteA, -1, utf16W.data(), len);
17 utf16 = utf16W.data();
24static bool utf16ToMultiByte(
const std::wstring& utf16, UINT cp, std::string& multiByte)
26 const wchar_t* utf16W = utf16.c_str();
27 if (
int len = WideCharToMultiByte(cp, 0, utf16W, -1,
nullptr, 0,
nullptr,
nullptr)) {
28 VLA(
char, multiByteA, len);
29 len = WideCharToMultiByte(cp, 0, utf16W, -1, multiByteA.data(), len,
nullptr,
nullptr);
31 multiByte = multiByteA.data();
41 if (!multiByteToUtf16(
utf8, CP_UTF8, MB_ERR_INVALID_CHARS, utf16)) {
43 "MultiByteToWideChar failed: ", GetLastError());
47 if (!utf16ToMultiByte(utf16, CP_ACP, ansi)) {
49 "MultiByteToWideChar failed: ", GetLastError());
57 if (!multiByteToUtf16(
utf8, CP_UTF8, MB_ERR_INVALID_CHARS, utf16))
60 "MultiByteToWideChar failed: ", GetLastError());
65std::string
utf16to8(
const std::wstring& utf16)
68 if (!utf16ToMultiByte(utf16, CP_UTF8,
utf8))
71 "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)
#define VLA(TYPE, NAME, LENGTH)