19 if (stringView.empty())
25 out.
assign(stringView.size(), L
'\0');
27 #if QX_STRING_USE_CRT_LOCALE
28 if (_locale_t crtLocale = &locale == &
get_locale() ? details::get_locale_state().crtLocale.get() :
nullptr)
30 for (
size_t i = 0; i < stringView.size(); ++i)
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);
43 const std::ctype<wchar_t>& facet =
45 facet.widen(stringView.data(), stringView.data() + stringView.size(), out.data());
60 inline wstring_view
to_wstring(wstring_view stringView,
const std::locale& locale)
69 if (stringView.empty())
75 out.
assign(stringView.size(),
'\0');
77 #if QX_STRING_USE_CRT_LOCALE
78 if (_locale_t crtLocale = &locale == &
get_locale() ? details::get_locale_state().crtLocale.get() :
nullptr)
80 for (
size_t i = 0; i < stringView.size(); ++i)
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] :
'?';
92 const std::ctype<wchar_t>& facet =
94 facet.narrow(stringView.data(), stringView.data() + stringView.size(),
'?', out.data());
109 inline cstring_view
to_cstring(cstring_view stringView,
const std::locale& locale)
114 inline void to_string(
string& out, cstring_view stringView,
const std::locale& locale)
116 #if QX_CONF_CHAR == QX_CHAR_T_CHAR
118 #elif QX_CONF_CHAR == QX_CHAR_T_WCHAR_T
121 #error Unsupported char type
125 inline string to_string(cstring_view stringView,
const std::locale& locale)
127 #if QX_CONF_CHAR == QX_CHAR_T_CHAR
129 #elif QX_CONF_CHAR == QX_CHAR_T_WCHAR_T
132 #error Unsupported char type
136 inline void to_string(
string& out, wstring_view stringView,
const std::locale& locale)
138 #if QX_CONF_CHAR == QX_CHAR_T_CHAR
140 #elif QX_CONF_CHAR == QX_CHAR_T_WCHAR_T
143 #error Unsupported char type
147 inline string to_string(wstring_view stringView,
const std::locale& locale)
149 #if QX_CONF_CHAR == QX_CHAR_T_CHAR
151 #elif QX_CONF_CHAR == QX_CHAR_T_WCHAR_T
154 #error Unsupported char type
162 #if QX_CONF_CHAR == QX_CHAR_T_CHAR
164 #elif QX_CONF_CHAR == QX_CHAR_T_WCHAR_T
169 if (!std::in_range<int>(utf8.size()))
175 const int nInputLength =
static_cast<int>(utf8.size());
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);
183 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
184 out = converter.from_bytes(utf8.data(), utf8.data() + utf8.size());
189 #error Unsupported char type
202 #if QX_CONF_CHAR == QX_CHAR_T_CHAR
204 #elif QX_CONF_CHAR == QX_CHAR_T_WCHAR_T
205 if (svText.empty() || !std::in_range<int>(svText.size()))
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');
216 WideCharToMultiByte(CP_UTF8, 0, svText.data(), nInputLength, out.data(), nLength,
nullptr,
nullptr);
219 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
220 out = converter.to_bytes(svText.data(), svText.data() + svText.size());
224 #error Unsupported char type
238 template<
class char_t,
size_t N>
239 constexpr std::array<char_t, N> to_char_array(std::span<const char, N> svChar)
241 std::array<char_t, N> result;
242 for (
size_t i = 0; i < N; ++i)
243 result[i] = svChar[i];
247 template<
class char_t,
string_literal array>
250 static constexpr
auto k_CharArray =
251 to_char_array<char_t>(std::span<
const char, array.size()>(array.data(), array.data() + array.size()));
256 template<
class char_t,
string_literal array>
260 return basic_string_view<char_t>(chars.data(), chars.size() - 1);
#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 ...
void assign(size_type nSymbols, value_type chSymbol) noexcept
Assign by filling.
const std::locale & get_locale()
Get the default locale used by string converters.
const std::ctype< wchar_t > & get_locale_facet()
Get the cached character conversion facet for the default locale.
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.