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 © Nick Khrapov, 2021. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <qx/category.h>
15 #include <qx/internal/perf_scope.h>
16 #include <qx/windows.h>
17 
18 #include <codecvt>
19 #include <locale>
20 
21 QX_DEFINE_CATEGORY(CatQxConverters);
22 
23 namespace qx
24 {
25 
26 /**
27  @brief convert cstring to wstring
28  @param out - output wchar_t string
29  @param stringView - char string view
30  @param locale - locale to use
31 **/
32 void to_wstring(wstring& out, cstring_view stringView, const std::locale& locale = std::locale());
33 
34 /**
35  @brief convert cstring to wstring
36  @param stringView - char string view
37  @param locale - locale to use
38  @retval - wchar_t string
39 **/
40 [[nodiscard]] wstring to_wstring(cstring_view stringView, const std::locale& locale = std::locale());
41 
42 /**
43  @brief Convert wstring to wstring
44  @param out - output wchar_t string
45  @param stringView - wchar_t string view
46  @param locale - locale to use
47 **/
48 void to_wstring(wstring& out, wstring_view stringView, const std::locale& locale = std::locale());
49 
50 /**
51  @brief Convert wstring to wstring (stub)
52  @param stringView - wchar_t string view
53  @param locale - locale to use
54  @retval - wchar_t string
55 **/
56 [[nodiscard]] wstring_view to_wstring(wstring_view stringView, const std::locale& locale = std::locale());
57 
58 /**
59  @brief Convert wstring to cstring
60  @details '?' is a default character
61  @param out - output char string
62  @param stringView - wchar_t string view
63  @param locale - locale to use
64 **/
65 void to_cstring(cstring& out, wstring_view stringView, const std::locale& locale = std::locale());
66 
67 /**
68  @brief Convert wstring to cstring
69  @details '?' is a default character
70  @param stringView - wchar_t string view
71  @param locale - locale to use
72  @retval - char string
73 **/
74 [[nodiscard]] cstring to_cstring(wstring_view stringView, const std::locale& locale = std::locale());
75 
76 /**
77  @brief Convert string to string
78  @param out - output char string
79  @param stringView - char string view
80  @param locale - locale to use
81 **/
82 void to_cstring(cstring& out, cstring_view stringView, const std::locale& locale = std::locale());
83 
84 /**
85  @brief Convert string to string (stub)
86  @param stringView - char string view
87  @param locale - locale to use
88  @retval - char string
89 **/
90 [[nodiscard]] cstring_view to_cstring(cstring_view stringView, const std::locale& locale = std::locale());
91 
92 /**
93  @brief Convert a char string to common string type
94  @param out - output common string
95  @param stringView - char string
96  @param locale - locale to use
97 **/
98 void to_string(string& out, cstring_view stringView, const std::locale& locale = std::locale());
99 
100 /**
101  @brief Convert a char string to common string type
102  @param stringView - char string
103  @param locale - locale to use
104  @retval - common string
105 **/
106 [[nodiscard]] string to_string(cstring_view stringView, const std::locale& locale = std::locale());
107 
108 /**
109  @brief Convert a wchar_t string to common string type
110  @param out - output common string
111  @param stringView - wchar_t string
112  @param locale - locale to use
113 **/
114 void to_string(string& out, wstring_view stringView, const std::locale& locale = std::locale());
115 
116 /**
117  @brief Convert a wchar_t string to common string type
118  @param stringView - wchar_t string
119  @param locale - locale to use
120  @retval - common string
121 **/
122 [[nodiscard]] string to_string(wstring_view stringView, const std::locale& locale = std::locale());
123 
124 /**
125  @brief Convert const char* representing UTF8 to wstring
126  @param out - output wchar_t string
127  @param pszUtf8 - UTF8 string
128 **/
129 void utf8_to_string(string& out, cstring_view pszUtf8);
130 
131 /**
132  @brief Convert const char* representing UTF8 to wstring
133  @param pszUtf8 - UTF8 string
134  @retval - wchar_t string
135 **/
136 [[nodiscard]] string utf8_to_string(cstring_view pszUtf8);
137 
138 /**
139  @brief Convert a constexpr string literal to the wider or equal char type string view
140  @tparam char_t - target string view type
141  @tparam sLiteral - constexpr string literal
142  @retval - constexpr string view of a wider or equal char type
143 **/
144 template<class char_t, string_literal sLiteral>
145 constexpr basic_string_view<char_t> convert_string_literal();
146 
147 } // namespace qx
148 
#define QX_DEFINE_CATEGORY(name,...)
Define a category.
Definition: category.h:21
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 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.