12#ifndef _CPG_CONVERSION_HPP
13#define _CPG_CONVERSION_HPP
15#ifndef TBB_SUPPRESS_DEPRECATED_MESSAGES
16 #define TBB_SUPPRESS_DEPRECATED_MESSAGES 1
24 #if _MSVC_LANG < 201703L
25 #error This libary requires C++17 Standard (Visual Studio 2017).
29 #if __cplusplus < 201703
30 #error This library requires C++17 Standard (GNU g++ version 8.0 or clang++ version 8.0 above)
65 std::wstring locale_name(LOCALE_NAME_MAX_LENGTH, L
'\0');
68 GetSystemDefaultLocaleName(
70 LOCALE_NAME_MAX_LENGTH);
73 if (locale_legnth == 0)
return;
76 if(locale_name[locale_legnth] == L
'\0')
77 locale_name = locale_name.substr(0, locale_legnth-1);
81 _wsetlocale(LC_ALL, locale_name.c_str());
84 std::wcout << L
"locale name: "
101 unsigned int codepage = CP_UTF8)
103 if (wstr.empty())
return "";
106 int str_len = WideCharToMultiByte(
110 (
int)wstr.size(), NULL, 0, NULL, NULL);
113 if (str_len == 0)
return "";
119 std::string str(str_len,
'\0');
121 int converted = WideCharToMultiByte(
122 codepage, 0, wstr.c_str(), (
int)wstr.size(),
123 &str[0], str_len, NULL, NULL);
125 return (converted == 0 ?
"": str);
147 unsigned int codepage = CP_UTF8)
149 if (str.empty())
return L
"";
153 int wstr_len = MultiByteToWideChar(
154 codepage, 0, str.c_str(), (
int)str.size(), NULL, 0);
157 if (wstr_len == 0)
return L
"";
162 std::wstring wstr(wstr_len, L
'\0');
164 int converted = MultiByteToWideChar(
165 codepage, 0, str.c_str(), (
int)str.size(),
168 return (converted == 0 ? L
"": wstr);
183 if (utf16str.empty())
return "";
218 if (utf16str ==
nullptr)
return "";
221 int utf16_len = (int)wcslen(utf16str);
222 if (utf16_len < 1)
return "";
240 if (utf8str ==
nullptr)
return L
"";
243 int utf8str_len = (int)strlen(utf8str);
244 if (utf8str_len < 1)
return L
"";
261 wchar_t src_str[] = {utf16_char, L
'\0'};
263 return (rlt.empty() ?
'\0': rlt[0]);
278 char src_str[] = {utf8_char,
'\0'};
280 return (rlt.empty() ? L
'\0': rlt[0]);
325 if(codepage_string==
nullptr)
return L
"";
326 if(strlen(codepage_string)<1)
return L
"";
342 if(utf16_string==
nullptr)
return "";
343 if(wcslen(utf16_string)<1)
return "";
377 if(codepage_string ==
nullptr)
return "";
378 if(strlen(codepage_string) < 1)
return "";
412 if(utf8_string==
nullptr)
return "";
413 if(strlen(utf8_string) < 1)
return "";
426 template<
typename TargetCharType,
typename SourceCharType>
430 if constexpr(std::is_same_v<TargetCharType, SourceCharType>)
434 else if constexpr(std::is_same_v<SourceCharType, char>)
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.