18 enum class charset_id {
36 _Inout_ std::wstring& dst,
37 _In_reads_or_z_opt_(count_src)
const char* src, _In_
size_t count_src,
38 _In_ charset_id charset = charset_id::default)
40 assert(src || !count_src);
42 assert(count_src < INT_MAX || count_src == SIZE_MAX);
43 constexpr DWORD dwFlags = MB_PRECOMPOSED;
46 WCHAR szStackBuffer[1024/
sizeof(WCHAR)];
47 int cch = MultiByteToWideChar(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), szStackBuffer, _countof(szStackBuffer));
50 dst.append(szStackBuffer, count_src != SIZE_MAX ? wcsnlen(szStackBuffer, cch) : (size_t)cch - 1);
51 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
53 cch = MultiByteToWideChar(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), NULL, 0);
54 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[cch]);
55 cch = MultiByteToWideChar(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), szBuffer.get(), cch);
56 dst.append(szBuffer.get(), count_src != SIZE_MAX ? wcsnlen(szBuffer.get(), cch) : (size_t)cch - 1);
59 throw std::exception(
"not implemented");
73 _Inout_ std::wstring& dst,
74 _In_
const std::string& src,
75 _In_ charset_id charset = charset_id::default)
77 str2wstr(dst, src.data(), src.size(), charset);
89 inline std::wstring str2wstr(
90 _In_reads_or_z_opt_(count_src)
const char* src, _In_
size_t count_src,
91 _In_ charset_id charset = charset_id::default)
94 str2wstr(dst, src, count_src, charset);
106 inline std::wstring str2wstr(
107 _In_
const std::string& src,
108 _In_ charset_id charset = charset_id::default)
110 return str2wstr(src.c_str(), src.size(), charset);
121 inline void wstr2str(
122 _Inout_ std::string& dst,
123 _In_reads_or_z_opt_(count_src)
const wchar_t* src,
124 _In_
size_t count_src,
125 _In_ charset_id charset = charset_id::default)
127 assert(src || !count_src);
129 assert(count_src < INT_MAX || count_src == SIZE_MAX);
130 constexpr DWORD dwFlags = 0;
131 constexpr LPCCH lpDefaultChar = NULL;
134 CHAR szStackBuffer[1024/
sizeof(CHAR)];
135 int cch = WideCharToMultiByte(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, NULL);
138 dst.append(szStackBuffer, count_src != SIZE_MAX ? strnlen(szStackBuffer, cch) : (size_t)cch - 1);
139 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
141 cch = WideCharToMultiByte(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), NULL, 0, lpDefaultChar, NULL);
142 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[cch]);
143 cch = WideCharToMultiByte(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), szBuffer.get(), cch, lpDefaultChar, NULL);
144 dst.append(szBuffer.get(), count_src != SIZE_MAX ? strnlen(szBuffer.get(), cch) : (size_t)cch - 1);
147 throw std::exception(
"not implemented");
158 inline void wstr2str(
159 _Inout_ std::string& dst,
160 _In_
const std::wstring& src,
161 _In_ charset_id charset = charset_id::default)
163 wstr2str(dst, src.c_str(), src.size(), charset);
175 inline std::string wstr2str(
176 _In_reads_or_z_opt_(count_src)
const wchar_t* src,
177 _In_
size_t count_src,
178 _In_ charset_id charset = charset_id::default)
181 wstr2str(dst, src, count_src, charset);
193 inline std::string wstr2str(
194 _In_
const std::wstring& src,
195 _In_ charset_id charset = charset_id::default)
197 return wstr2str(src.c_str(), src.size(), charset);