stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
unicode.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023 Amebis
4*/
5
6#pragma once
7
8#include "sal.hpp"
9#include "system.hpp"
10#include <assert.h>
11#include <stdint.h>
12#include <memory>
13#include <string>
14
15namespace stdex
16{
17 enum class charset_id : uint16_t {
18#ifdef _WIN32
19 default = CP_ACP,
20 utf8 = CP_UTF8,
21 utf16 = 1200 /*CP_WINUNICODE*/,
22#else
23 default = 0,
24#endif
25 };
26
37 inline void strcat(
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)
41 {
42 assert(src || !count_src);
43#ifdef _WIN32
44 assert(count_src < INT_MAX || count_src == SIZE_MAX);
45 constexpr DWORD dwFlags = MB_PRECOMPOSED;
46
47 // Try to convert to stack buffer first.
48 WCHAR szStackBuffer[1024/sizeof(WCHAR)];
49#pragma warning(suppress: 6387) // Testing indicates src may be NULL when count_src is also 0. Is SAL of the lpMultiByteStr parameter wrong?
50 int cch = MultiByteToWideChar(static_cast<UINT>(charset), dwFlags, src, static_cast<int>(count_src), szStackBuffer, _countof(szStackBuffer));
51 if (cch) {
52 // Append from stack.
53 dst.append(szStackBuffer, count_src != SIZE_MAX ? wcsnlen(szStackBuffer, cch) : (size_t)cch - 1);
54 } else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
55 // Query the required output size. Allocate buffer. Then convert again.
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);
60 }
61#else
62 throw std::exception("not implemented");
63#endif
64 }
65
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)
70 {
71 strcat(dst, src, count_src, charset);
72 }
73
83 inline void strcat(
84 _Inout_ std::wstring& dst,
85 _In_ const std::string& src,
86 _In_ charset_id charset = charset_id::default)
87 {
88 strcat(dst, src.data(), src.size(), charset);
89 }
90
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)
95 {
96 strcat(dst, src, charset);
97 }
98
109 inline void strcpy(
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)
113 {
114 dst.clear();
115 strcat(dst, src, count_src, charset);
116 }
117
127 inline void strcpy(
128 _Inout_ std::wstring& dst,
129 _In_ const std::string& src,
130 _In_ charset_id charset = charset_id::default)
131 {
132 strcpy(dst, src.data(), src.size(), charset);
133 }
134
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)
147 {
148 std::wstring dst;
149 strcat(dst, src, count_src, charset);
150 return dst;
151 }
152
161 inline std::wstring str2wstr(
162 _In_ const std::string& src,
163 _In_ charset_id charset = charset_id::default)
164 {
165 return str2wstr(src.c_str(), src.size(), charset);
166 }
167
176 inline void strcat(
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)
180 {
181 assert(src || !count_src);
182#ifdef _WIN32
183 assert(count_src < INT_MAX || count_src == SIZE_MAX);
184 constexpr DWORD dwFlags = 0;
185 constexpr LPCCH lpDefaultChar = NULL;
186
187 // Try to convert to stack buffer first.
188 CHAR szStackBuffer[1024/sizeof(CHAR)];
189#pragma warning(suppress: 6387) // Testing indicates src may be NULL when count_src is also 0. Is SAL of the lpWideCharStr parameter wrong?
190 int cch = WideCharToMultiByte(static_cast<UINT>(charset), dwFlags, src, static_cast<int>(count_src), szStackBuffer, _countof(szStackBuffer), lpDefaultChar, NULL);
191 if (cch) {
192 // Copy from stack. Be careful not to include zero terminator.
193 dst.append(szStackBuffer, count_src != SIZE_MAX ? strnlen(szStackBuffer, cch) : (size_t)cch - 1);
194 } else if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
195 // Query the required output size. Allocate buffer. Then convert again.
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);
200 }
201#else
202 throw std::exception("not implemented");
203#endif
204 }
205
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)
210 {
211 strcat(dst, src, count_src, charset);
212 }
213
221 inline void strcat(
222 _Inout_ std::string& dst,
223 _In_ const std::wstring& src,
224 _In_ charset_id charset = charset_id::default)
225 {
226 strcat(dst, src.c_str(), src.size(), charset);
227 }
228
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)
233 {
234 strcat(dst, src, charset);
235 }
236
245 inline void strcpy(
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)
249 {
250 dst.clear();
251 strcat(dst, src, count_src, charset);
252 }
253
261 inline void strcpy(
262 _Inout_ std::string& dst,
263 _In_ const std::wstring& src,
264 _In_ charset_id charset = charset_id::default)
265 {
266 strcpy(dst, src.data(), src.size(), charset);
267 }
268
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)
281 {
282 std::string dst;
283 strcat(dst, src, count_src, charset);
284 return dst;
285 }
286
295 inline std::string wstr2str(
296 _In_ const std::wstring& src,
297 _In_ charset_id charset = charset_id::default)
298 {
299 return wstr2str(src.c_str(), src.size(), charset);
300 }
301}