C++ Library Extensions 2022.12.09
To help learn modern C++ programming
cpg_conversion.hpp
Go to the documentation of this file.
1
12#ifndef _CPG_CONVERSION_HPP
13#define _CPG_CONVERSION_HPP
14
15#ifndef TBB_SUPPRESS_DEPRECATED_MESSAGES
16 #define TBB_SUPPRESS_DEPRECATED_MESSAGES 1
17#endif // end of TBB_SUPPRESS_DEPRECATED_MESSAGES
18
19#ifndef NOMINMAX
20#define NOMINMAX
21#endif
22
23#ifdef _MSVC_LANG
24 #if _MSVC_LANG < 201703L
25 #error This libary requires C++17 Standard (Visual Studio 2017).
26 #endif
27#else
28
29 #if __cplusplus < 201703
30 #error This library requires C++17 Standard (GNU g++ version 8.0 or clang++ version 8.0 above)
31 #endif // end of __cplusplus
32
33#endif // end of _MSVC_LANG
34
35#include <string>
36#include <iostream>
37#include <clocale>
38#include <cstring>
39#include <type_traits>
40#include <windows.h>
41
46namespace cpg
47{
52 namespace conversion
53 {
59 inline void load_default_locale(bool ShowLocaleName = false)
60 {
61 // GetSystemDefaultLocaleName function :
62 // https://goo.gl/WLLSG3
63
64 // allocate buffer to hold locale name
65 std::wstring locale_name(LOCALE_NAME_MAX_LENGTH, L'\0');
66
67 int locale_legnth =
68 GetSystemDefaultLocaleName(
69 &locale_name[0],
70 LOCALE_NAME_MAX_LENGTH);
71
72 // if failed to get locale name, then just return
73 if (locale_legnth == 0) return;
74
75 // trim trailing buffer
76 if(locale_name[locale_legnth] == L'\0')
77 locale_name = locale_name.substr(0, locale_legnth-1);
78
79 // https://goo.gl/1A2Hh4
80 // set locale of the process or thread
81 _wsetlocale(LC_ALL, locale_name.c_str());
82
83 if(ShowLocaleName)
84 std::wcout << L"locale name: "
85 << locale_name << std::endl;
86 }
87
100 inline std::string wstring_to_string(const std::wstring& wstr,
101 unsigned int codepage = CP_UTF8)
102 {
103 if (wstr.empty()) return "";
104
105 // https://goo.gl/acoQBt
106 int str_len = WideCharToMultiByte(
107 codepage,
108 0, // dwFlag
109 wstr.c_str(),
110 (int)wstr.size(), NULL, 0, NULL, NULL);
111
112 // if failed to compute the byte count of the string
113 if (str_len == 0) return "";
114
115 // prepare a string buffer to
116 // hold the converted string
117 // do not +1 to str_len,
118 // because std::string manages terminating null
119 std::string str(str_len, '\0');
120
121 int converted = WideCharToMultiByte(
122 codepage, 0, wstr.c_str(), (int)wstr.size(),
123 &str[0], str_len, NULL, NULL);
124
125 return (converted == 0 ? "": str);
126 }
127
146 inline std::wstring string_to_wstring(const std::string& str,
147 unsigned int codepage = CP_UTF8)
148 {
149 if (str.empty()) return L"";
150
151 // https://goo.gl/upmy99
152 // Compute the character length of the string
153 int wstr_len = MultiByteToWideChar(
154 codepage, 0, str.c_str(), (int)str.size(), NULL, 0);
155
156 // if failed to compute the character length
157 if (wstr_len == 0) return L"";
158
159 // prepare buffer to hold converted string
160 // do not +1 to utf16_len, because std::wstring
161 // manages terminating null internally
162 std::wstring wstr(wstr_len, L'\0');
163
164 int converted = MultiByteToWideChar(
165 codepage, 0, str.c_str(), (int)str.size(),
166 &wstr[0], wstr_len);
167
168 return (converted == 0 ? L"": wstr);
169 }
170
181 inline std::string utf16_to_utf8(const std::wstring& utf16str)
182 {
183 if (utf16str.empty()) return "";
184 return wstring_to_string(utf16str, CP_UTF8);
185 }
186
197 inline std::wstring utf8_to_utf16(const std::string& utf8str)
198 {
199 if (utf8str.empty())
200 return L"";
201 return
202 string_to_wstring(utf8str, CP_UTF8);
203 }
204
216 inline std::string utf16_to_utf8(const wchar_t* utf16str)
217 {
218 if (utf16str == nullptr) return "";
219
220 // count of character of UTF16 string
221 int utf16_len = (int)wcslen(utf16str);
222 if (utf16_len < 1) return "";
223
224 return utf16_to_utf8(std::wstring(utf16str));
225 }
226
237 inline std::wstring utf8_to_utf16(const char* utf8str)
238 {
239 // if utf8str is nullptr, return empty string L""
240 if (utf8str == nullptr) return L"";
241
242 // count of byte of the string to be converted
243 int utf8str_len = (int)strlen(utf8str);
244 if (utf8str_len < 1) return L"";
245
246 return utf8_to_utf16(std::string(utf8str));
247 }
248
259 inline char utf16_to_utf8(const wchar_t utf16_char)
260 {
261 wchar_t src_str[] = {utf16_char, L'\0'};
262 auto rlt = utf16_to_utf8(std::wstring(src_str));
263 return (rlt.empty() ? '\0': rlt[0]);
264 }
265
276 inline wchar_t utf8_to_utf16(const char utf8_char)
277 {
278 char src_str[] = {utf8_char, '\0'};
279 auto rlt = utf8_to_utf16(std::string(src_str));
280 return (rlt.empty() ? L'\0': rlt[0]);
281 }
282
284
295 inline std::wstring windows_codepage_to_utf16(const std::string& codepage_string)
296 {
297 return string_to_wstring(codepage_string, CP_ACP);
298 }
299
309 inline std::string utf16_to_windows_codepage(const std::wstring& utf16_string)
310 {
311 return wstring_to_string(utf16_string, CP_ACP);
312 }
313
323 inline std::wstring windows_codepage_to_utf16(const char* codepage_string)
324 {
325 if(codepage_string==nullptr) return L"";
326 if(strlen(codepage_string)<1) return L"";
327
328 return string_to_wstring(std::string(codepage_string), CP_ACP);
329 }
330
340 inline std::string utf16_to_windows_codepage(const wchar_t* utf16_string)
341 {
342 if(utf16_string==nullptr) return "";
343 if(wcslen(utf16_string)<1) return "";
344
345 return wstring_to_string(std::wstring(utf16_string), CP_ACP);
346 }
347
358 inline std::string windows_codepage_to_utf8(const std::string& codepage_string)
359 {
360 auto utf16_str = windows_codepage_to_utf16(codepage_string);
361
362 return (utf16_str.empty() ? "": utf16_to_utf8(utf16_str));
363 }
364
375 inline std::string windows_codepage_to_utf8(const char* codepage_string)
376 {
377 if(codepage_string == nullptr) return "";
378 if(strlen(codepage_string) < 1) return "";
379
380 return windows_codepage_to_utf8(std::string(codepage_string));
381 }
382
393 inline std::string utf8_to_windows_codepage(const std::string& utf8_string)
394 {
395 auto utf16_str = utf8_to_utf16(utf8_string);
396
397 return (utf16_str.empty() ? "": utf16_to_windows_codepage(utf16_str));
398 }
399
410 inline std::string utf8_to_windows_codepage(const char* utf8_string)
411 {
412 if(utf8_string==nullptr) return "";
413 if(strlen(utf8_string) < 1) return "";
414
415 return utf8_to_windows_codepage(std::string(utf8_string));
416 }
417
426 template<typename TargetCharType, typename SourceCharType>
427 decltype(auto)
428 source_to_target(const std::basic_string<SourceCharType>& str)
429 {
430 if constexpr(std::is_same_v<TargetCharType, SourceCharType>)
431 {
432 return str;
433 }
434 else if constexpr(std::is_same_v<SourceCharType, char>)
435 {
436 return utf8_to_utf16(str);
437 }
438 else
439 {
440 return utf16_to_utf8(str);
441 }
442 }
443
444 inline std::string smart_encode(const char* arg)
445 {
446 #ifdef _MSC_VER
447 return { arg };
448 #else
450 #endif
451 }
452
453 inline std::string smart_encode(const wchar_t* arg)
454 {
456 }
457
458 } // end of namespace conversion
459
460} // end of namespace cpg
461
462#endif // end of file _CPG_CONVERSION_HPP
auto & endl
std::string utf16_to_windows_codepage(const std::wstring &utf16_string)
Converts UTF16 std::wstring to Windows codepage std::string.
std::string windows_codepage_to_utf8(const std::string &codepage_string)
Converts Windows codepage std::string to UTF8 std::string.
void load_default_locale(bool ShowLocaleName=false)
Load system default locale for string conversion.
std::wstring utf8_to_utf16(const std::string &utf8str)
Converts UTF8 std::string to UTF16 std::wstring.
std::string utf16_to_utf8(const std::wstring &utf16str)
Converts UTF16 std::wstring to UTF8 std::string.
std::string wstring_to_string(const std::wstring &wstr, unsigned int codepage=CP_UTF8)
Converts from std::wstring to std::string with codepage.
std::string smart_encode(const char *arg)
std::wstring windows_codepage_to_utf16(const std::string &codepage_string)
Converts Windows codepage std::string to utf16 std::wstring.
decltype(auto) source_to_target(const std::basic_string< SourceCharType > &str)
Converts from source to target.
std::wstring string_to_wstring(const std::string &str, unsigned int codepage=CP_UTF8)
Converts std::string to std::wstring using codepage.
std::string utf8_to_windows_codepage(const std::string &utf8_string)
Converts UTF8 std::string to Windows codepage string.
Includes subnamespace conversion.
String conversions are implemented.