Fixed bug where StrToWstr and WstrToStr may contain invalid characters from a previous call

This commit is contained in:
Kah Wei 2022-12-22 15:06:52 +08:00
parent 360b362b7b
commit 861e47812f
1 changed files with 23 additions and 21 deletions

View File

@ -30,16 +30,18 @@ namespace SHADE
std::string SHStringUtilities::WstrToStr(const std::wstring& wstr)
{
static std::vector<char> buffer;
buffer.clear();
const int STR_SIZE = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), static_cast<int>(wstr.size()), nullptr, 0, nullptr, nullptr) + 1 /* Null Terminator */;
buffer.resize(STR_SIZE);
buffer.resize(STR_SIZE, '\0');
WideCharToMultiByte(CP_UTF8, 0, wstr.data(), static_cast<int>(wstr.size()), buffer.data(), MAX_PATH, nullptr, nullptr);
return std::string(buffer.data());
}
std::wstring SHStringUtilities::StrToWstr(const std::string& str)
{
static std::vector<wchar_t> buffer;
buffer.clear();
const int WSTR_SIZE = MultiByteToWideChar(CP_UTF8, 0, str.data(), static_cast<int>(str.size()), nullptr, 0) + 1 /* Null Terminator */;
buffer.resize(WSTR_SIZE);
buffer.resize(WSTR_SIZE, '\0');
MultiByteToWideChar(CP_UTF8, 0, str.data(), static_cast<int>(str.size()), buffer.data(), WSTR_SIZE);
return std::wstring(buffer.data());
}