18 enum class charset_id {
33 _Inout_ std::wstring& dst,
34 _In_reads_or_z_opt_(count_src)
const char* src, _In_
size_t count_src,
35 _In_ charset_id charset = charset_id::default)
37 assert(src || !count_src);
39 assert(count_src < INT_MAX || count_src == SIZE_MAX);
40 constexpr DWORD dwFlags = MB_PRECOMPOSED;
43 WCHAR szStackBuffer[1024/
sizeof(WCHAR)];
44 int cch = MultiByteToWideChar(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), szStackBuffer, _countof(szStackBuffer));
47 dst.append(szStackBuffer, count_src != SIZE_MAX ? wcsnlen(szStackBuffer, cch) : (size_t)cch - 1);
48 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
50 cch = MultiByteToWideChar(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), NULL, 0);
51 std::unique_ptr<WCHAR[]> szBuffer(
new WCHAR[cch]);
52 cch = MultiByteToWideChar(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), szBuffer.get(), cch);
53 dst.append(szBuffer.get(), count_src != SIZE_MAX ? wcsnlen(szBuffer.get(), cch) : (size_t)cch - 1);
56 throw std::exception(
"not implemented");
70 _Inout_ std::wstring& dst,
71 _In_
const std::string& src,
72 _In_ charset_id charset = charset_id::default)
74 str2wstr(dst, src.data(), src.size(), charset);
86 inline std::wstring str2wstr(
87 _In_reads_or_z_opt_(count_src)
const char* src, _In_
size_t count_src,
88 _In_ charset_id charset = charset_id::default)
91 str2wstr(dst, src, count_src, charset);
103 inline std::wstring str2wstr(
104 _In_
const std::string& src,
105 _In_ charset_id charset = charset_id::default)
107 return str2wstr(src.c_str(), src.size(), charset);
118 inline void wstr2str(
119 _Inout_ std::string& dst,
120 _In_reads_or_z_opt_(count_src)
const wchar_t* src,
121 _In_
size_t count_src,
122 _In_ charset_id charset = charset_id::default)
124 assert(src || !count_src);
126 assert(count_src < INT_MAX || count_src == SIZE_MAX);
127 constexpr DWORD dwFlags = 0;
128 constexpr LPCCH lpDefaultChar = NULL;
131 CHAR szStackBuffer[1024/
sizeof(CHAR)];
132 int cch = WideCharToMultiByte(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, NULL);
135 dst.append(szStackBuffer, count_src != SIZE_MAX ? strnlen(szStackBuffer, cch) : (size_t)cch - 1);
136 }
else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
138 cch = WideCharToMultiByte(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), NULL, 0, lpDefaultChar, NULL);
139 std::unique_ptr<CHAR[]> szBuffer(
new CHAR[cch]);
140 cch = WideCharToMultiByte(
static_cast<UINT
>(charset), dwFlags, src,
static_cast<int>(count_src), szBuffer.get(), cch, lpDefaultChar, NULL);
141 dst.append(szBuffer.get(), count_src != SIZE_MAX ? strnlen(szBuffer.get(), cch) : (size_t)cch - 1);
144 throw std::exception(
"not implemented");
155 inline void wstr2str(
156 _Inout_ std::string& dst,
157 _In_
const std::wstring& src,
158 _In_ charset_id charset = charset_id::default)
160 wstr2str(dst, src.c_str(), src.size(), charset);
172 inline std::string wstr2str(
173 _In_reads_or_z_opt_(count_src)
const wchar_t* src,
174 _In_
size_t count_src,
175 _In_ charset_id charset = charset_id::default)
178 wstr2str(dst, src, count_src, charset);
190 inline std::string wstr2str(
191 _In_
const std::wstring& src,
192 _In_ charset_id charset = charset_id::default)
194 return wstr2str(src.c_str(), src.size(), charset);