diff --git a/src/common/WindowsUtils.cpp b/src/common/WindowsUtils.cpp index 0f9b9852fc..f4aef7cb3d 100644 --- a/src/common/WindowsUtils.cpp +++ b/src/common/WindowsUtils.cpp @@ -30,3 +30,16 @@ std::string WCharToUTF8(const wchar_t* input) { // This will allocate the returned std::string and then destroy result. return std::string(result.get(), result.get() + (requiredSize - 1)); } + +std::wstring UTF8ToWStr(const char* input) { + // The -1 argument asks MultiByteToWideChar to use the null terminator to know the size of + // input. It will return a size that includes the null terminator. + int requiredSize = MultiByteToWideChar(CP_UTF8, 0, input, -1, nullptr, 0); + + // When we can use C++17 this can be changed to use string.data() instead. + std::unique_ptr result = std::make_unique(requiredSize); + MultiByteToWideChar(CP_UTF8, 0, input, -1, result.get(), requiredSize); + + // This will allocate the returned std::string and then destroy result. + return std::wstring(result.get(), result.get() + (requiredSize - 1)); +} diff --git a/src/common/WindowsUtils.h b/src/common/WindowsUtils.h index 0c43d08aa4..3ab916bbcd 100644 --- a/src/common/WindowsUtils.h +++ b/src/common/WindowsUtils.h @@ -19,4 +19,6 @@ std::string WCharToUTF8(const wchar_t* input); +std::wstring UTF8ToWStr(const char* input); + #endif // COMMON_WINDOWSUTILS_H_ diff --git a/src/tests/unittests/WindowsUtilsTests.cpp b/src/tests/unittests/WindowsUtilsTests.cpp index 7bc4cd75b9..fcf6b69419 100644 --- a/src/tests/unittests/WindowsUtilsTests.cpp +++ b/src/tests/unittests/WindowsUtilsTests.cpp @@ -31,4 +31,21 @@ TEST(WindowsUtilsTests, WCharToUTF8) { // Test three-byte utf8 codepoint ASSERT_EQ("\xe1\x81\x90", WCharToUTF8(L"\x1050")); +} + +TEST(WindowsUtilsTests, UTF8ToWStr) { + // Test the empty string + ASSERT_EQ(L"", UTF8ToWStr("")); + + // Test ASCII characters + ASSERT_EQ(L"abc", UTF8ToWStr("abc")); + + // Test ASCII characters + ASSERT_EQ(L"abc", UTF8ToWStr("abc")); + + // Test two-byte utf8 character + ASSERT_EQ(L"\x450", UTF8ToWStr("\xd1\x90")); + + // Test three-byte utf8 codepoint + ASSERT_EQ(L"\x1050", UTF8ToWStr("\xe1\x81\x90")); } \ No newline at end of file