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