qxLib
string_converters.h
Go to the documentation of this file.
1 /**
2 
3  @file string_converters.h
4  @author Khrapov
5  @date 24.11.2021
6  @copyright (c) Nick Khrapov, 2021. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <qx/category.h>
16 #include <qx/internal/perf_scope.h>
17 #include <qx/windows.h>
18 
19 #include <codecvt>
20 #include <locale>
21 #include <utility>
22 
23 QX_DEFINE_CATEGORY(CatQxConverters);
24 
25 namespace qx
26 {
27 
28 /**
29  @brief convert cstring to wstring
30  @param out - output wchar_t string
31  @param stringView - char string view
32  @param locale - locale to use
33 **/
34 void to_wstring(wstring& out, cstring_view stringView, const std::locale& locale = get_locale());
35 
36 /**
37  @brief convert cstring to wstring
38  @param stringView - char string view
39  @param locale - locale to use
40  @return - wchar_t string
41 **/
42 [[nodiscard]] wstring to_wstring(cstring_view stringView, const std::locale& locale = get_locale());
43 
44 /**
45  @brief Convert wstring to wstring
46  @param out - output wchar_t string
47  @param stringView - wchar_t string view
48  @param locale - locale to use
49 **/
50 void to_wstring(wstring& out, wstring_view stringView, const std::locale& locale = get_locale());
51 
52 /**
53  @brief Convert wstring to wstring (stub)
54  @param stringView - wchar_t string view
55  @param locale - locale to use
56  @return - wchar_t string
57 **/
58 [[nodiscard]] wstring_view to_wstring(wstring_view stringView, const std::locale& locale = get_locale());
59 
60 /**
61  @brief Convert wstring to cstring
62  @details '?' is a default character
63  @param out - output char string
64  @param stringView - wchar_t string view
65  @param locale - locale to use
66 **/
67 void to_cstring(cstring& out, wstring_view stringView, const std::locale& locale = get_locale());
68 
69 /**
70  @brief Convert wstring to cstring
71  @details '?' is a default character
72  @param stringView - wchar_t string view
73  @param locale - locale to use
74  @return - char string
75 **/
76 [[nodiscard]] cstring to_cstring(wstring_view stringView, const std::locale& locale = get_locale());
77 
78 /**
79  @brief Convert string to string
80  @param out - output char string
81  @param stringView - char string view
82  @param locale - locale to use
83 **/
84 void to_cstring(cstring& out, cstring_view stringView, const std::locale& locale = get_locale());
85 
86 /**
87  @brief Convert string to string (stub)
88  @param stringView - char string view
89  @param locale - locale to use
90  @return - char string
91 **/
92 [[nodiscard]] cstring_view to_cstring(cstring_view stringView, const std::locale& locale = get_locale());
93 
94 /**
95  @brief Convert a char string to common string type
96  @param out - output common string
97  @param stringView - char string
98  @param locale - locale to use
99 **/
100 void to_string(string& out, cstring_view stringView, const std::locale& locale = get_locale());
101 
102 /**
103  @brief Convert a char string to common string type
104  @param stringView - char string
105  @param locale - locale to use
106  @return - common string
107 **/
108 [[nodiscard]] string to_string(cstring_view stringView, const std::locale& locale = get_locale());
109 
110 /**
111  @brief Convert a wchar_t string to common string type
112  @param out - output common string
113  @param stringView - wchar_t string
114  @param locale - locale to use
115 **/
116 void to_string(string& out, wstring_view stringView, const std::locale& locale = get_locale());
117 
118 /**
119  @brief Convert a wchar_t string to common string type
120  @param stringView - wchar_t string
121  @param locale - locale to use
122  @return - common string
123 **/
124 [[nodiscard]] string to_string(wstring_view stringView, const std::locale& locale = get_locale());
125 
126 /**
127  @brief Convert const char* representing UTF8 to wstring
128  @param out - output wchar_t string
129  @param pszUtf8 - UTF8 string
130 **/
131 void utf8_to_string(string& out, cstring_view pszUtf8);
132 
133 /**
134  @brief Convert const char* representing UTF8 to wstring
135  @param pszUtf8 - UTF8 string
136  @return - wchar_t string
137 **/
138 [[nodiscard]] string utf8_to_string(cstring_view pszUtf8);
139 
140 /**
141  @brief Convert the engine string representation to UTF-8 bytes.
142  @param out - destination UTF-8 string; cleared for empty or unrepresentable input
143  @param svText - source string, with embedded nulls preserved
144 **/
145 void string_to_uft8(cstring& out, string_view svText);
146 
147 /**
148  @brief Convert the engine string representation to UTF-8 bytes.
149  @param svText - source string, with embedded nulls preserved
150  @return - UTF-8 bytes, or an empty string for empty or unrepresentable input
151 **/
152 [[nodiscard]] cstring string_to_uft8(string_view svText);
153 
154 /**
155  @brief Convert a constexpr string literal to the wider or equal char type string view
156  @tparam char_t - target string view type
157  @tparam sLiteral - constexpr string literal
158  @return - constexpr string view of a wider or equal char type
159 **/
160 template<class char_t, string_literal sLiteral>
161 constexpr basic_string_view<char_t> convert_string_literal();
162 
163 } // namespace qx
164 
#define QX_DEFINE_CATEGORY(name,...)
Define a category.
Definition: category.h:24
const std::locale & get_locale()
Get the default locale used by string converters.
Definition: locale.inl:77
Contains perf scope macros for profiler (for internal usage only, but user may override them)
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.