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_USE_CHAR
72  out = stringView;
73 #elif defined(QX_CONF_USE_WCHAR)
74  to_wstring(out, stringView, locale);
75 #endif
76 }
77 
78 inline string to_string(cstring_view stringView, const std::locale& locale)
79 {
80 #if QX_CONF_USE_CHAR
81  return stringView;
82 #elif defined(QX_CONF_USE_WCHAR)
83  return to_wstring(stringView, locale);
84 #endif
85 }
86 
87 inline void to_string(string& out, wstring_view stringView, const std::locale& locale)
88 {
89 #if QX_CONF_USE_CHAR
90  to_cstring(out, stringView, locale);
91 #elif defined(QX_CONF_USE_WCHAR)
92  out = stringView;
93 #endif
94 }
95 
96 inline string to_string(wstring_view stringView, const std::locale& locale)
97 {
98 #if QX_CONF_USE_CHAR
99  return to_cstring(stringView, locale);
100 #elif defined(QX_CONF_USE_WCHAR)
101  return stringView;
102 #endif
103 }
104 
105 inline void utf8_to_string(string& out, cstring_view pszUtf8)
106 {
107  QX_PERF_SCOPE();
108 
109 #if QX_CONF_USE_WCHAR
110  #if QX_WIN
111 
112  // much faster on windows
113  const int nLength = MultiByteToWideChar(CP_UTF8, 0, pszUtf8.data(), static_cast<int>(pszUtf8.size()), nullptr, 0);
114  out.assign(nLength, L'\n');
115  MultiByteToWideChar(CP_UTF8, 0, pszUtf8.data(), static_cast<int>(pszUtf8.size()), out.data(), nLength);
116 
117  #else
118 
119  QX_PUSH_SUPPRESS_ALL_WARNINGS();
120  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
121  out = converter.from_bytes(pszUtf8.data(), pszUtf8.data() + pszUtf8.size());
122  QX_POP_SUPPRESS_WARNINGS();
123 
124  #endif
125 #endif
126 }
127 
128 inline string utf8_to_string(cstring_view pszUtf8)
129 {
130  string sResult;
131  utf8_to_string(sResult, pszUtf8);
132  return sResult;
133 }
134 
135 namespace details
136 {
137 
138 template<class char_t, size_t N>
139 constexpr std::array<char_t, N> to_char_array(std::span<const char, N> svChar)
140 {
141  std::array<char_t, N> result;
142  for (size_t i = 0; i < N; ++i)
143  result[i] = svChar[i];
144  return result;
145 }
146 
147 template<class char_t, string_literal array>
149 {
150  static constexpr auto char_array =
151  to_char_array<char_t>(std::span<const char, array.size()>(array.data(), array.data() + array.size()));
152 };
153 
154 } // namespace details
155 
156 template<class char_t, string_literal array>
157 constexpr basic_string_view<char_t> convert_string_literal()
158 {
159  return basic_string_view<char_t>(details::char_array_helper<char_t, array>::char_array.data());
160 }
161 
162 } // 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:77
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.