[REFACT] Improved strings conversion UTF8/WCHAR

This commit is contained in:
hasherezade
2026-05-29 15:54:23 -08:00
parent 227d0e4fcb
commit 9022c33105
4 changed files with 77 additions and 10 deletions
+1 -1
View File
@@ -112,7 +112,7 @@ namespace util {
stream << ", ";
}
}
return std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(stream.str());
return hh::util::wstring_to_utf8(stream.str());
}
}; //namespace util
+2 -3
View File
@@ -84,8 +84,7 @@ void print_version(const std::string &version , WORD info_color = HILIGHTED_COLO
std::wstring to_wstring(const std::string& stringToConvert)
{
std::wstring wideString =
std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(stringToConvert);
const std::wstring wideString = hh::util::utf8_to_wstring(stringToConvert);
return wideString;
}
@@ -204,7 +203,7 @@ public:
if (enumParam) {
this->addParam(enumParam);
this->setInfo(PARAM_RESULTS_FILTER, "Define what type of results are reported.");
for (size_t i = SHOW_SUSPICIOUS; i <= SHOW_ALL; i++) {
for (DWORD i = SHOW_SUSPICIOUS; i <= SHOW_ALL; i++) {
t_results_filter mode = (t_results_filter)(i);
std::string info = translate_results_filter(mode);
if (info.empty()) continue;
+63
View File
@@ -36,3 +36,66 @@ void hh::util::print_in_color(WORD color, const std::string &text)
const std::lock_guard<std::mutex> stdOutLock(g_stdOutMutex);
paramkit::print_in_color(color, text);
}
std::string hh::util::wstring_to_utf8(const std::wstring& wstr)
{
if (wstr.empty())
return {};
int size = WideCharToMultiByte(
CP_UTF8,
0,
wstr.data(),
static_cast<int>(wstr.size()),
nullptr,
0,
nullptr,
nullptr);
std::string result(size, '\0');
WideCharToMultiByte(
CP_UTF8,
0,
wstr.data(),
static_cast<int>(wstr.size()),
result.data(),
size,
nullptr,
nullptr);
return result;
}
std::wstring hh::util::utf8_to_wstring(const std::string& utf8)
{
if (utf8.empty())
return {};
int size = MultiByteToWideChar(
CP_UTF8,
0,
utf8.data(),
static_cast<int>(utf8.size()),
nullptr,
0);
if (size == 0)
throw std::runtime_error("MultiByteToWideChar failed");
std::wstring result(size, L'\0');
int converted = MultiByteToWideChar(
CP_UTF8,
0,
utf8.data(),
static_cast<int>(utf8.size()),
result.data(),
size);
if (converted == 0)
throw std::runtime_error("MultiByteToWideChar failed");
return result;
}
+11 -6
View File
@@ -3,6 +3,7 @@
#include <iostream>
#include <windows.h>
#include <mutex>
#include <string>
extern std::mutex g_stdOutMutex;
@@ -31,14 +32,18 @@ extern std::mutex g_stdOutMutex;
namespace hh {
namespace util {
bool get_current_color(int descriptor, WORD& color);
namespace util {
bool get_current_color(int descriptor, WORD& color);
WORD set_color(WORD color);
WORD set_color(WORD color);
// sets a color and returns the previous color:
void print_in_color(WORD color, const std::string& text);
// sets a color and returns the previous color:
void print_in_color(WORD color, const std::string& text);
}; // util
std::string wstring_to_utf8(const std::wstring& wstr);
std::wstring utf8_to_wstring(const std::string& utf8);
}; // util
}; //hh