2#ifndef _CONSOLIX_ENCODING_UTILS_HPP_INCLUDED
3#define _CONSOLIX_ENCODING_UTILS_HPP_INCLUDED
9#if defined(_WIN32) || defined(_WIN64)
18 std::string utf8_to_ansi(
const std::string& utf8)
noexcept {
19 int n_len = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0);
20 if (n_len == 0)
return {};
22 std::wstring wide_string(n_len + 1, L
'\0');
23 MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, wide_string.data(), n_len);
25 n_len = WideCharToMultiByte(CP_ACP, 0, wide_string.c_str(), -1, NULL, 0, NULL, NULL);
26 if (n_len == 0)
return {};
28 std::string ansi_string(n_len - 1,
'\0');
29 WideCharToMultiByte(CP_ACP, 0, wide_string.c_str(), -1, ansi_string.data(), n_len, NULL, NULL);
36 std::string ansi_to_utf8(
const std::string& ansi)
noexcept {
37 int n_len = MultiByteToWideChar(CP_ACP, 0, ansi.c_str(), -1, NULL, 0);
38 if (n_len == 0)
return {};
40 std::wstring wide_string(n_len, L
'\0');
41 MultiByteToWideChar(CP_ACP, 0, ansi.c_str(), -1, wide_string.data(), n_len);
43 n_len = WideCharToMultiByte(CP_UTF8, 0, wide_string.c_str(), -1, NULL, 0, NULL, NULL);
44 if (n_len == 0)
return {};
46 std::string utf8_string(n_len - 1,
'\0');
47 WideCharToMultiByte(CP_UTF8, 0, wide_string.c_str(), -1, utf8_string.data(), n_len, NULL, NULL);
54 std::string utf8_to_cp866(
const std::string& utf8)
noexcept {
55 std::string temp = utf8_to_ansi(utf8);
56 CharToOem((LPSTR)temp.c_str(), temp.data());
63 bool is_valid_utf8(
const char* message) {
64 if (!message)
return true;
65 const unsigned char* bytes =
reinterpret_cast<const unsigned char*
>(message);
66 while (*bytes != 0x00) {
69 if ((*bytes & 0x80) == 0x00) { cp = (*bytes & 0x7F); num = 1; }
70 else if ((*bytes & 0xE0) == 0xC0) { cp = (*bytes & 0x1F); num = 2; }
71 else if ((*bytes & 0xF0) == 0xE0) { cp = (*bytes & 0x0F); num = 3; }
72 else if ((*bytes & 0xF8) == 0xF0) { cp = (*bytes & 0x07); num = 4; }
76 for (
int i = 1; i < num; ++i) {
77 if ((*bytes & 0xC0) != 0x80)
return false;
78 cp = (cp << 6) | (*bytes & 0x3F);
82 if (cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF))
return false;
90 std::string cp1251_to_utf8(
const std::string& cp1251) {
91 int len = MultiByteToWideChar(1251, 0, cp1251.c_str(), -1, NULL, 0);
92 if (len == 0)
return {};
94 std::wstring wideString(len, L
'\0');
95 MultiByteToWideChar(1251, 0, cp1251.c_str(), -1, wideString.data(), len);
97 len = WideCharToMultiByte(CP_UTF8, 0, wideString.c_str(), -1, NULL, 0, NULL, NULL);
98 if (len == 0)
return {};
100 std::string utf8String(len,
'\0');
101 WideCharToMultiByte(CP_UTF8, 0, wideString.c_str(), -1, utf8String.data(), len, NULL, NULL);
108 std::string utf16_to_utf8(LPWSTR utf16String) {
109 int bufferSize = WideCharToMultiByte(CP_UTF8, 0, utf16String, -1,
nullptr, 0,
nullptr,
nullptr);
110 std::string utf8String(bufferSize,
'\0');
111 WideCharToMultiByte(CP_UTF8, 0, utf16String, -1, utf8String.data(), bufferSize,
nullptr,
nullptr);
118 std::wstring utf8_to_utf16(
const std::string& utf8)
noexcept {
119 int n_len = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0);
120 if (n_len == 0)
return {};
122 std::wstring utf16_string(n_len, L
'\0');
123 MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, utf16_string.data(), n_len);
< Utility modules and helpers.