| 3 | #include <windows.h> |
| 4 | |
| 5 | std::wstring from_utf8(const std::string_view &string) { |
| 6 | // No conversion needed if the string is empty |
| 7 | if (string.empty()) { |
| 8 | return {}; |
| 9 | } |
| 10 | |
| 11 | // Get the output size required to store the string |
| 12 | auto output_size = MultiByteToWideChar(CP_UTF8, 0, string.data(), string.size(), nullptr, 0); |
| 13 | if (output_size == 0) { |
| 14 | // auto winerr = GetLastError(); |
| 15 | // BOOST_LOG(error) << "Failed to get UTF-16 buffer size: "sv << winerr; |
| 16 | return {}; |
| 17 | } |
| 18 | |
| 19 | // Perform the conversion |
| 20 | std::wstring output(output_size, L'\0'); |
| 21 | output_size = MultiByteToWideChar(CP_UTF8, 0, string.data(), string.size(), output.data(), output.size()); |
| 22 | if (output_size == 0) { |
| 23 | // auto winerr = GetLastError(); |
| 24 | // BOOST_LOG(error) << "Failed to convert string to UTF-16: "sv << winerr; |
| 25 | return {}; |
| 26 | } |
| 27 | |
| 28 | return output; |
| 29 | } |
| 30 | |
| 31 | std::string to_utf8(const std::wstring_view &string) { |
| 32 | // No conversion needed if the string is empty |
no test coverage detected