qxLib
string_converters.inl
Go to the documentation of this file.
1 /**
2 
3  @file string_converters.inl
4  @author Khrapov
5  @date 11.01.2026
6  @copyright (c) Nick Khrapov, 2026. All right reserved.
7 
8 **/
9 
10 QX_SET_FILE_CATEGORY(CatQxConverters);
11 
12 namespace qx
13 {
14 
15 inline void to_wstring(wstring& out, cstring_view stringView, const std::locale& locale)
16 {
17  QX_PERF_SCOPE();
18 
19  if (stringView.empty())
20  {
21  out.clear();
22  return;
23  }
24 
25  out.assign(stringView.size(), L'\0');
26 
27 #if QX_STRING_USE_CRT_LOCALE
28  if (_locale_t crtLocale = &locale == &get_locale() ? details::get_locale_state().crtLocale.get() : nullptr)
29  {
30  for (size_t i = 0; i < stringView.size(); ++i)
31  {
32  wchar_t ch = static_cast<wchar_t>(WEOF);
33  if (static_cast<int>(stringView[i]) != EOF && _mbtowc_l(&ch, stringView.data() + i, 1, crtLocale) < 0)
34  ch = static_cast<wchar_t>(WEOF);
35 
36  out[i] = ch;
37  }
38 
39  return;
40  }
41 #endif
42 
43  const std::ctype<wchar_t>& facet =
44  &locale == &get_locale() ? get_locale_facet() : std::use_facet<std::ctype<wchar_t>>(locale);
45  facet.widen(stringView.data(), stringView.data() + stringView.size(), out.data());
46 }
47 
48 inline wstring to_wstring(cstring_view stringView, const std::locale& locale)
49 {
50  wstring result;
51  to_wstring(result, stringView, locale);
52  return result;
53 }
54 
55 inline void to_wstring(wstring& out, wstring_view stringView, const std::locale& locale)
56 {
57  out = stringView;
58 }
59 
60 inline wstring_view to_wstring(wstring_view stringView, const std::locale& locale)
61 {
62  return stringView;
63 }
64 
65 inline void to_cstring(cstring& out, wstring_view stringView, const std::locale& locale)
66 {
67  QX_PERF_SCOPE();
68 
69  if (stringView.empty())
70  {
71  out.clear();
72  return;
73  }
74 
75  out.assign(stringView.size(), '\0');
76 
77 #if QX_STRING_USE_CRT_LOCALE
78  if (_locale_t crtLocale = &locale == &get_locale() ? details::get_locale_state().crtLocale.get() : nullptr)
79  {
80  for (size_t i = 0; i < stringView.size(); ++i)
81  {
82  char buffer[MB_LEN_MAX];
83  const wchar_t ch = stringView[i];
84  const int nLength = ch == static_cast<wchar_t>(WEOF) ? -1 : _wctomb_l(buffer, ch, crtLocale);
85  out[i] = nLength == 1 && static_cast<int>(buffer[0]) != EOF ? buffer[0] : '?';
86  }
87 
88  return;
89  }
90 #endif
91 
92  const std::ctype<wchar_t>& facet =
93  &locale == &get_locale() ? get_locale_facet() : std::use_facet<std::ctype<wchar_t>>(locale);
94  facet.narrow(stringView.data(), stringView.data() + stringView.size(), '?', out.data());
95 }
96 
97 inline cstring to_cstring(wstring_view stringView, const std::locale& locale)
98 {
99  cstring result;
100  to_cstring(result, stringView, locale);
101  return result;
102 }
103 
104 inline void to_cstring(cstring& out, cstring_view stringView, const std::locale& locale)
105 {
106  out = stringView;
107 }
108 
109 inline cstring_view to_cstring(cstring_view stringView, const std::locale& locale)
110 {
111  return stringView;
112 }
113 
114 inline void to_string(string& out, cstring_view stringView, const std::locale& locale)
115 {
116 #if QX_CONF_CHAR == QX_CHAR_T_CHAR
117  out = stringView;
118 #elif QX_CONF_CHAR == QX_CHAR_T_WCHAR_T
119  to_wstring(out, stringView, locale);
120 #else
121  #error Unsupported char type
122 #endif
123 }
124 
125 inline string to_string(cstring_view stringView, const std::locale& locale)
126 {
127 #if QX_CONF_CHAR == QX_CHAR_T_CHAR
128  return stringView;
129 #elif QX_CONF_CHAR == QX_CHAR_T_WCHAR_T
130  return to_wstring(stringView, locale);
131 #else
132  #error Unsupported char type
133 #endif
134 }
135 
136 inline void to_string(string& out, wstring_view stringView, const std::locale& locale)
137 {
138 #if QX_CONF_CHAR == QX_CHAR_T_CHAR
139  to_cstring(out, stringView, locale);
140 #elif QX_CONF_CHAR == QX_CHAR_T_WCHAR_T
141  out = stringView;
142 #else
143  #error Unsupported char type
144 #endif
145 }
146 
147 inline string to_string(wstring_view stringView, const std::locale& locale)
148 {
149 #if QX_CONF_CHAR == QX_CHAR_T_CHAR
150  return to_cstring(stringView, locale);
151 #elif QX_CONF_CHAR == QX_CHAR_T_WCHAR_T
152  return stringView;
153 #else
154  #error Unsupported char type
155 #endif
156 }
157 
158 inline void utf8_to_string(string& out, cstring_view utf8)
159 {
160  QX_PERF_SCOPE();
161 
162 #if QX_CONF_CHAR == QX_CHAR_T_CHAR
163  out = utf8;
164 #elif QX_CONF_CHAR == QX_CHAR_T_WCHAR_T
165  #if QX_WIN
166 
167  // much faster on windows
168  // Windows accepts only int-sized input; keep the empty-result failure policy.
169  if (!std::in_range<int>(utf8.size()))
170  {
171  out.clear();
172  return;
173  }
174 
175  const int nInputLength = static_cast<int>(utf8.size()); //-V202
176  const int nLength = MultiByteToWideChar(CP_UTF8, 0, utf8.data(), nInputLength, nullptr, 0);
177  out.assign(nLength, L'\n');
178  MultiByteToWideChar(CP_UTF8, 0, utf8.data(), nInputLength, out.data(), nLength);
179 
180  #else
181 
183  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
184  out = converter.from_bytes(utf8.data(), utf8.data() + utf8.size());
186 
187  #endif
188 #else
189  #error Unsupported char type
190 #endif
191 }
192 
193 inline string utf8_to_string(cstring_view utf8)
194 {
195  string sResult;
196  utf8_to_string(sResult, utf8);
197  return sResult;
198 }
199 
200 inline void string_to_uft8(cstring& out, string_view svText)
201 {
202 #if QX_CONF_CHAR == QX_CHAR_T_CHAR
203  out = svText;
204 #elif QX_CONF_CHAR == QX_CHAR_T_WCHAR_T
205  if (svText.empty() || !std::in_range<int>(svText.size()))
206  {
207  out.clear();
208  return;
209  }
210 
211  #if QX_WIN
212  const int nInputLength = static_cast<int>(svText.size());
213  const int nLength = WideCharToMultiByte(CP_UTF8, 0, svText.data(), nInputLength, nullptr, 0, nullptr, nullptr);
214  out.assign(nLength, '\0');
215  if (nLength != 0)
216  WideCharToMultiByte(CP_UTF8, 0, svText.data(), nInputLength, out.data(), nLength, nullptr, nullptr);
217  #else
219  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
220  out = converter.to_bytes(svText.data(), svText.data() + svText.size());
222  #endif
223 #else
224  #error Unsupported char type
225 #endif
226 }
227 
228 inline cstring string_to_uft8(string_view svText)
229 {
230  cstring sResult;
231  string_to_uft8(sResult, svText);
232  return sResult;
233 }
234 
235 namespace details
236 {
237 
238 template<class char_t, size_t N>
239 constexpr std::array<char_t, N> to_char_array(std::span<const char, N> svChar)
240 {
241  std::array<char_t, N> result;
242  for (size_t i = 0; i < N; ++i)
243  result[i] = svChar[i];
244  return result;
245 }
246 
247 template<class char_t, string_literal array>
249 {
250  static constexpr auto k_CharArray =
251  to_char_array<char_t>(std::span<const char, array.size()>(array.data(), array.data() + array.size()));
252 };
253 
254 } // namespace details
255 
256 template<class char_t, string_literal array>
257 constexpr basic_string_view<char_t> convert_string_literal()
258 {
260  return basic_string_view<char_t>(chars.data(), chars.size() - 1);
261 }
262 
263 } // namespace qx
#define QX_SET_FILE_CATEGORY(_category)
Set the file category You can access this value via QX_GET_FILE_CATEGORY() This category will not be ...
Definition: category.h:32
String class.
Definition: string.h:83
void assign(size_type nSymbols, value_type chSymbol) noexcept
Assign by filling.
Definition: string.inl:58
const std::locale & get_locale()
Get the default locale used by string converters.
Definition: locale.inl:77
const std::ctype< wchar_t > & get_locale_facet()
Get the cached character conversion facet for the default locale.
Definition: locale.inl:82
constexpr basic_string_view< char_t > convert_string_literal()
Convert a constexpr string literal to the wider or equal char type string view.
void utf8_to_string(string &out, cstring_view pszUtf8)
Convert const char* representing UTF8 to wstring.
void to_cstring(cstring &out, wstring_view stringView, const std::locale &locale=get_locale())
Convert wstring to cstring.
void to_wstring(wstring &out, cstring_view stringView, const std::locale &locale=get_locale())
convert cstring to wstring
void to_string(string &out, cstring_view stringView, const std::locale &locale=get_locale())
Convert a char string to common string type.
void string_to_uft8(cstring &out, string_view svText)
Convert the engine string representation to UTF-8 bytes.
#define QX_PUSH_SUPPRESS_ALL_WARNINGS()
Disable all warnings.
#define QX_POP_SUPPRESS_WARNINGS()
Enable all warnings.