| 29 | } |
| 30 | |
| 31 | std::string to_utf8(const std::wstring_view &string) { |
| 32 | // No conversion needed if the string is empty |
| 33 | if (string.empty()) { |
| 34 | return {}; |
| 35 | } |
| 36 | |
| 37 | // Get the output size required to store the string |
| 38 | auto output_size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, string.data(), string.size(), nullptr, 0, nullptr, nullptr); |
| 39 | if (output_size == 0) { |
| 40 | // auto winerr = GetLastError(); |
| 41 | // BOOST_LOG(error) << "Failed to get UTF-8 buffer size: "sv << winerr; |
| 42 | return {}; |
| 43 | } |
| 44 | |
| 45 | // Perform the conversion |
| 46 | std::string output(output_size, '\0'); |
| 47 | output_size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, string.data(), string.size(), output.data(), output.size(), nullptr, nullptr); |
| 48 | if (output_size == 0) { |
| 49 | // auto winerr = GetLastError(); |
| 50 | // BOOST_LOG(error) << "Failed to convert string to UTF-8: "sv << winerr; |
| 51 | return {}; |
| 52 | } |
| 53 | |
| 54 | return output; |
| 55 | } |