Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions StringUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
#ifdef _WIN32
#include <windows.h>
#else
#include <locale>
#include <codecvt>
#include <cwchar>
#include <clocale>
#endif

/**
Expand All @@ -62,9 +62,15 @@ std::string WstringToString(const std::wstring &wstr)
return res;
#else
// Linux / macOS
std::wstring ws(wstr);
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
return conv.to_bytes(ws);
std::size_t len = std::wcstombs(nullptr, wstr.c_str(), 0);

if (len == static_cast<std::size_t>(-1))
return "Invalid wide-character sequence or invalid locale";

std::string result(len, '\0');
std::wcstombs(result.data(), wstr.c_str(), len);

return result;
#endif // _WIN32
}

Expand All @@ -89,7 +95,14 @@ std::wstring StringToWstring(const std::string &str)
return res;
#else
// Linux / macOS
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
return conv.from_bytes(str);
std::size_t len = std::mbstowcs(nullptr, str.c_str(), 0);

if (len == static_cast<std::size_t>(-1))
return L"Invalid UTF-8 string or invalid locale";

std::wstring result(len, L'\0');
std::mbstowcs(result.data(), str.c_str(), len);

return result;
#endif // _WIN32
}
4 changes: 2 additions & 2 deletions StringUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
@brief Declaration of global string utility functions
*/

#ifndef TimeUtil_h
#define TimeUtil_h
#ifndef StringUtil_h
#define StringUtil_h

#include <string>

Expand Down