17 enum class charset_id : uint16_t {
38 _Inout_ std::wstring& dst,
39 _In_reads_or_z_opt_(count_src)
const char* src, _In_
size_t count_src,
40 _In_ charset_id charset = charset_id::default)
42 assert(src || !count_src);
44 assert(count_src < INT_MAX || count_src == SIZE_MAX);
45 constexpr DWORD dwFlags = MB_PRECOMPOSED;
48 WCHAR szStackBuffer[1024/
sizeof(WCHAR)];
49#pragma warning(suppress: 6387)
50 int cch = MultiByteToWideChar(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), szStackBuffer, _countof(szStackBuffer));
53 dst.append(szStackBuffer, count_src != SIZE_MAX ? wcsnlen(szStackBuffer, cch) : (size_t)cch - 1);
54 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
56 cch = MultiByteToWideChar(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), NULL, 0);
57 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[cch]);
58 cch = MultiByteToWideChar(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), szBuffer.get(), cch);
59 dst.append(szBuffer.get(), count_src != SIZE_MAX ? wcsnlen(szBuffer.get(), cch) : (size_t)cch - 1);
62 throw std::exception(
"not implemented");
76 _Inout_ std::wstring& dst,
77 _In_
const std::string& src,
78 _In_ charset_id charset = charset_id::default)
80 str2wstr(dst, src.data(), src.size(), charset);
92 inline std::wstring str2wstr(
93 _In_reads_or_z_opt_(count_src)
const char* src, _In_
size_t count_src,
94 _In_ charset_id charset = charset_id::default)
97 str2wstr(dst, src, count_src, charset);
109 inline std::wstring str2wstr(
110 _In_
const std::string& src,
111 _In_ charset_id charset = charset_id::default)
113 return str2wstr(src.c_str(), src.size(), charset);
124 inline void wstr2str(
125 _Inout_ std::string& dst,
126 _In_reads_or_z_opt_(count_src)
const wchar_t* src,
127 _In_
size_t count_src,
128 _In_ charset_id charset = charset_id::default)
130 assert(src || !count_src);
132 assert(count_src < INT_MAX || count_src == SIZE_MAX);
133 constexpr DWORD dwFlags = 0;
134 constexpr LPCCH lpDefaultChar = NULL;
137 CHAR szStackBuffer[1024/
sizeof(CHAR)];
138#pragma warning(suppress: 6387)
139 int cch = WideCharToMultiByte(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, NULL);
142 dst.append(szStackBuffer, count_src != SIZE_MAX ? strnlen(szStackBuffer, cch) : (size_t)cch - 1);
143 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
145 cch = WideCharToMultiByte(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), NULL, 0, lpDefaultChar, NULL);
146 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[cch]);
147 cch = WideCharToMultiByte(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), szBuffer.get(), cch, lpDefaultChar, NULL);
148 dst.append(szBuffer.get(), count_src != SIZE_MAX ? strnlen(szBuffer.get(), cch) : (size_t)cch - 1);
151 throw std::exception(
"not implemented");
162 inline void wstr2str(
163 _Inout_ std::string& dst,
164 _In_
const std::wstring& src,
165 _In_ charset_id charset = charset_id::default)
167 wstr2str(dst, src.c_str(), src.size(), charset);
179 inline std::string wstr2str(
180 _In_reads_or_z_opt_(count_src)
const wchar_t* src,
181 _In_
size_t count_src,
182 _In_ charset_id charset = charset_id::default)
185 wstr2str(dst, src, count_src, charset);
197 inline std::string wstr2str(
198 _In_
const std::wstring& src,
199 _In_ charset_id charset = charset_id::default)
201 return wstr2str(src.c_str(), src.size(), charset);