common : extract shared unicode path/string helpers (#29415)

Signed-off-by: Adrien Gallouët <[email protected]>
This commit is contained in:
Adrien Gallouët
2026-09-25 11:40:41 +02:00
committed by GitHub
parent e351231c4f
commit 27b20ba8b1
6 changed files with 48 additions and 58 deletions
+2 -21
View File
@@ -483,25 +483,6 @@ static void unset_reserved_args(common_preset & preset, bool unset_model_args) {
}
}
#ifdef _WIN32
static std::string wide_to_utf8(const wchar_t * ws) {
if (!ws || !*ws) {
return {};
}
const int len = static_cast<int>(std::wcslen(ws));
const int bytes = WideCharToMultiByte(CP_UTF8, 0, ws, len, nullptr, 0, nullptr, nullptr);
if (bytes == 0) {
return {};
}
std::string utf8(bytes, '\0');
WideCharToMultiByte(CP_UTF8, 0, ws, len, utf8.data(), bytes, nullptr, nullptr);
return utf8;
}
#endif
static std::vector<std::string> get_environment() {
std::vector<std::string> env;
@@ -511,7 +492,7 @@ static std::vector<std::string> get_environment() {
return env;
}
for (LPWCH e = env_block; *e; e += wcslen(e) + 1) {
env.emplace_back(wide_to_utf8(e));
env.emplace_back(wstring_to_utf8(e));
}
FreeEnvironmentStringsW(env_block);
#else
@@ -592,7 +573,7 @@ server_models::server_models(
// set binary path
try {
bin_path = get_server_exec_path().string();
bin_path = fs_path_to_utf8(get_server_exec_path());
} catch (const std::exception & e) {
bin_path = argv[0];
LOG_WRN("failed to get server executable path: %s\n", e.what());