Support UTF8 to UTF16 LE

This commit is contained in:
2026-03-31 14:10:21 +11:00
parent 0c24096c80
commit 70be68d0fc
14 changed files with 790 additions and 11 deletions

View File

@@ -1,4 +1,7 @@
#include "ANSUtilities.h"
#ifdef _WIN32
#include <windows.h>
#endif
#include <iostream>
#include <CkFileAccess.h>
#include <CkAuthGoogle.h>
@@ -949,5 +952,55 @@ namespace ANSCENTER
return false;
}
}
std::string ANSUtilities::DecodeJsonUnicodeToUTF16LE(const std::string& escapedStr) {
std::string result;
result.reserve(escapedStr.size() * 2);
size_t i = 0;
while (i < escapedStr.size()) {
if (i + 5 < escapedStr.size() && escapedStr[i] == '\\' && escapedStr[i + 1] == 'u') {
char hex[5] = { escapedStr[i + 2], escapedStr[i + 3], escapedStr[i + 4], escapedStr[i + 5], 0 };
uint16_t codepoint = (uint16_t)strtoul(hex, nullptr, 16);
result += static_cast<char>(codepoint & 0xFF);
result += static_cast<char>((codepoint >> 8) & 0xFF);
i += 6;
} else {
result += escapedStr[i];
result += '\0';
i++;
}
}
return result;
}
std::string ANSUtilities::ConvertUTF16LEToUTF8(const char* utf16leBytes, int byteLen) {
#ifdef _WIN32
if (!utf16leBytes || byteLen <= 0) return "";
int wideLen = byteLen / (int)sizeof(wchar_t);
const wchar_t* wideStr = reinterpret_cast<const wchar_t*>(utf16leBytes);
// First call: get required UTF-8 buffer size
int utf8Len = WideCharToMultiByte(CP_UTF8, 0, wideStr, wideLen, nullptr, 0, nullptr, nullptr);
if (utf8Len <= 0) return "";
std::string utf8Str(utf8Len, 0);
WideCharToMultiByte(CP_UTF8, 0, wideStr, wideLen, &utf8Str[0], utf8Len, nullptr, nullptr);
return utf8Str;
#else
return std::string(utf16leBytes, byteLen);
#endif
}
std::string ANSUtilities::ConvertUTF8ToUTF16LE(const std::string& utf8Str) {
#ifdef _WIN32
if (utf8Str.empty()) return "";
int wideLen = MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), (int)utf8Str.size(), nullptr, 0);
if (wideLen <= 0) return "";
std::wstring wideStr(wideLen, 0);
MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), (int)utf8Str.size(), &wideStr[0], wideLen);
const char* rawBytes = reinterpret_cast<const char*>(wideStr.data());
return std::string(rawBytes, wideLen * sizeof(wchar_t));
#else
return utf8Str;
#endif
}
}