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 © 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  out.assign(stringView.size(), L'\0');
20  std::use_facet<std::ctype<wchar_t>>(locale).widen(
21  stringView.data(),
22  stringView.data() + stringView.size(),
23  out.data());
24 }
25 
26 inline wstring to_wstring(cstring_view stringView, const std::locale& locale)
27 {
28  wstring result;
29  to_wstring(result, stringView, locale);
30  return result;
31 }
32 
33 inline void to_wstring(wstring& out, wstring_view stringView, const std::locale& locale)
34 {
35  out = stringView;
36 }
37 
38 inline wstring_view to_wstring(wstring_view stringView, const std::locale& locale)
39 {
40  return stringView;
41 }
42 
43 inline void to_cstring(cstring& out, wstring_view stringView, const std::locale& locale)
44 {
45  QX_PERF_SCOPE();
46 
47  out.assign(stringView.size(), '\0');
48  std::use_facet<std::ctype<wchar_t>>(locale)
49  .narrow(stringView.data(), stringView.data() + stringView.size(), '?', out.data());
50 }
51 
52 inline cstring to_cstring(wstring_view stringView, const std::locale& locale)
53 {
54  cstring result;
55  to_cstring(result, stringView, locale);
56  return result;
57 }
58 
59 inline void to_cstring(cstring& out, cstring_view stringView, const std::locale& locale)
60 {
61  out = stringView;
62 }
63 
64 inline cstring_view to_cstring(cstring_view stringView, const std::locale& locale)
65 {
66  return stringView;
67 }
68 
69 inline void to_string(string& out, cstring_view stringView, const std::locale& locale)
70 {
71 #if QX_CONF_CHAR == QX_CHAR_T_CHAR
72  out = stringView;
73 #elif QX_CONF_CHAR == QX_CHAR_T_WCHAR_T
74  to_wstring(out, stringView, locale);
75 #else
76  #error Unsupported char type
77 #endif
78 }
79 
80 inline string to_string(cstring_view stringView, const std::locale& locale)
81 {
82 #if QX_CONF_CHAR == QX_CHAR_T_CHAR
83  return stringView;
84 #elif QX_CONF_CHAR == QX_CHAR_T_WCHAR_T
85  return to_wstring(stringView, locale);
86 #else
87  #error Unsupported char type
88 #endif
89 }
90 
91 inline void to_string(string& out, wstring_view stringView, const std::locale& locale)
92 {
93 #if QX_CONF_CHAR == QX_CHAR_T_CHAR
94  to_cstring(out, stringView, locale);
95 #elif QX_CONF_CHAR == QX_CHAR_T_WCHAR_T
96  out = stringView;
97 #else
98  #error Unsupported char type
99 #endif
100 }
101 
102 inline string to_string(wstring_view stringView, const std::locale& locale)
103 {
104 #if QX_CONF_CHAR == QX_CHAR_T_CHAR
105  return to_cstring(stringView, locale);
106 #elif QX_CONF_CHAR == QX_CHAR_T_WCHAR_T
107  return stringView;
108 #else
109  #error Unsupported char type
110 #endif
111 }
112 
113 inline void utf8_to_string(string& out, cstring_view utf8)
114 {
115  QX_PERF_SCOPE();
116 
117 #if QX_CONF_CHAR == QX_CHAR_T_CHAR
118  out = utf8;
119 #elif QX_CONF_CHAR == QX_CHAR_T_WCHAR_T
120  #if QX_WIN
121 
122  // much faster on windows
123  const int nLength = MultiByteToWideChar(CP_UTF8, 0, utf8.data(), static_cast<int>(utf8.size()), nullptr, 0);
124  out.assign(nLength, L'\n');
125  MultiByteToWideChar(CP_UTF8, 0, utf8.data(), static_cast<int>(utf8.size()), out.data(), nLength);
126 
127  #else
128 
129  QX_PUSH_SUPPRESS_ALL_WARNINGS();
130  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
131  out = converter.from_bytes(utf8.data(), utf8.data() + utf8.size());
132  QX_POP_SUPPRESS_WARNINGS();
133 
134  #endif
135 #else
136  #error Unsupported char type
137 #endif
138 }
139 
140 inline string utf8_to_string(cstring_view utf8)
141 {
142  string sResult;
143  utf8_to_string(sResult, utf8);
144  return sResult;
145 }
146 
147 namespace details
148 {
149 
150 template<class char_t, size_t N>
151 constexpr std::array<char_t, N> to_char_array(std::span<const char, N> svChar)
152 {
153  std::array<char_t, N> result;
154  for (size_t i = 0; i < N; ++i)
155  result[i] = svChar[i];
156  return result;
157 }
158 
159 template<class char_t, string_literal array>
161 {
162  static constexpr auto char_array =
163  to_char_array<char_t>(std::span<const char, array.size()>(array.data(), array.data() + array.size()));
164 };
165 
166 } // namespace details
167 
168 template<class char_t, string_literal array>
169 constexpr basic_string_view<char_t> convert_string_literal()
170 {
172  return basic_string_view<char_t>(chars.data(), chars.size() - 1);
173 }
174 
175 } // 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:29
String class.
Definition: string.h:83
void assign(size_type nSymbols, value_type chSymbol) noexcept
Assign by filling.
Definition: string.inl:58
constexpr basic_string_view< char_t > convert_string_literal()
Convert a constexpr string literal to the wider or equal char type string view.
void to_wstring(wstring &out, cstring_view stringView, const std::locale &locale=std::locale())
convert cstring to wstring
void to_string(string &out, cstring_view stringView, const std::locale &locale=std::locale())
Convert a char string to common string type.
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=std::locale())
Convert wstring to cstring.