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");
66 inline _Deprecated_(
"Use stdex::strcat") void str2wstr(
67 _Inout_ std::wstring& dst,
68 _In_reads_or_z_opt_(count_src) const
char* src, _In_
size_t count_src,
69 _In_ charset_id charset = charset_id::default)
71 strcat(dst, src, count_src, charset);
84 _Inout_ std::wstring& dst,
85 _In_
const std::string& src,
86 _In_ charset_id charset = charset_id::default)
88 strcat(dst, src.data(), src.size(), charset);
91 inline _Deprecated_(
"Use stdex::strcat") void str2wstr(
92 _Inout_ std::wstring& dst,
93 _In_ const std::
string& src,
94 _In_ charset_id charset = charset_id::default)
96 strcat(dst, src, charset);
110 _Inout_ std::wstring& dst,
111 _In_reads_or_z_opt_(count_src)
const char* src, _In_
size_t count_src,
112 _In_ charset_id charset = charset_id::default)
115 strcat(dst, src, count_src, charset);
128 _Inout_ std::wstring& dst,
129 _In_
const std::string& src,
130 _In_ charset_id charset = charset_id::default)
132 strcpy(dst, src.data(), src.size(), charset);
144 inline std::wstring str2wstr(
145 _In_reads_or_z_opt_(count_src)
const char* src, _In_
size_t count_src,
146 _In_ charset_id charset = charset_id::default)
149 strcat(dst, src, count_src, charset);
161 inline std::wstring str2wstr(
162 _In_
const std::string& src,
163 _In_ charset_id charset = charset_id::default)
165 return str2wstr(src.c_str(), src.size(), charset);
177 _Inout_ std::string& dst,
178 _In_reads_or_z_opt_(count_src)
const wchar_t* src, _In_
size_t count_src,
179 _In_ charset_id charset = charset_id::default)
181 assert(src || !count_src);
183 assert(count_src < INT_MAX || count_src == SIZE_MAX);
184 constexpr DWORD dwFlags = 0;
185 constexpr LPCCH lpDefaultChar = NULL;
188 CHAR szStackBuffer[1024/
sizeof(CHAR)];
189#pragma warning(suppress: 6387)
190 int cch = WideCharToMultiByte(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, NULL);
193 dst.append(szStackBuffer, count_src != SIZE_MAX ? strnlen(szStackBuffer, cch) : (size_t)cch - 1);
194 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
196 cch = WideCharToMultiByte(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), NULL, 0, lpDefaultChar, NULL);
197 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[cch]);
198 cch = WideCharToMultiByte(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), szBuffer.get(), cch, lpDefaultChar, NULL);
199 dst.append(szBuffer.get(), count_src != SIZE_MAX ? strnlen(szBuffer.get(), cch) : (size_t)cch - 1);
202 throw std::exception(
"not implemented");
206 inline _Deprecated_(
"Use stdex::strcat") void wstr2str(
207 _Inout_ std::
string& dst,
208 _In_reads_or_z_opt_(count_src) const
wchar_t* src, _In_
size_t count_src,
209 _In_ charset_id charset = charset_id::default)
211 strcat(dst, src, count_src, charset);
222 _Inout_ std::string& dst,
223 _In_
const std::wstring& src,
224 _In_ charset_id charset = charset_id::default)
226 strcat(dst, src.c_str(), src.size(), charset);
229 inline _Deprecated_(
"Use stdex::strcat") void wstr2str(
230 _Inout_ std::
string& dst,
231 _In_ const std::wstring& src,
232 _In_ charset_id charset = charset_id::default)
234 strcat(dst, src, charset);
246 _Inout_ std::string& dst,
247 _In_reads_or_z_opt_(count_src)
const wchar_t* src, _In_
size_t count_src,
248 _In_ charset_id charset = charset_id::default)
251 strcat(dst, src, count_src, charset);
262 _Inout_ std::string& dst,
263 _In_
const std::wstring& src,
264 _In_ charset_id charset = charset_id::default)
266 strcpy(dst, src.data(), src.size(), charset);
278 inline std::string wstr2str(
279 _In_reads_or_z_opt_(count_src)
const wchar_t* src, _In_
size_t count_src,
280 _In_ charset_id charset = charset_id::default)
283 strcat(dst, src, count_src, charset);
295 inline std::string wstr2str(
296 _In_
const std::wstring& src,
297 _In_ charset_id charset = charset_id::default)
299 return wstr2str(src.c_str(), src.size(), charset);