| 18 | } // namespace |
| 19 | |
| 20 | std::string WideToUtf8(std::wstring_view value) { |
| 21 | if (value.empty()) return {}; |
| 22 | if (!FitsWindowsInt(value.size(), "WideToUtf8")) return {}; |
| 23 | |
| 24 | const int size = WideCharToMultiByte(CP_UTF8, 0, value.data(), static_cast<int>(value.size()), |
| 25 | nullptr, 0, nullptr, nullptr); |
| 26 | if (size <= 0) { |
| 27 | OSTP_LOG_DEBUG("WideCharToMultiByte(size query) failed (error={})", GetLastError()); |
| 28 | return {}; |
| 29 | } |
| 30 | |
| 31 | std::string result(static_cast<size_t>(size), '\0'); |
| 32 | const int written = WideCharToMultiByte(CP_UTF8, 0, value.data(), static_cast<int>(value.size()), |
| 33 | result.data(), size, nullptr, nullptr); |
| 34 | if (written != size) { |
| 35 | OSTP_LOG_DEBUG("WideCharToMultiByte(convert) failed (written={}, expected={}, error={})", |
| 36 | written, size, GetLastError()); |
| 37 | return {}; |
| 38 | } |
| 39 | return result; |
| 40 | } |
| 41 | |
| 42 | std::wstring Utf8ToWide(std::string_view value) { |
| 43 | if (value.empty()) return {}; |
no test coverage detected