| 1 |
/* |
| 2 |
* Copyright (c) 2025 The Khronos Group Inc. |
| 3 |
* Copyright (c) 2025 Valve Corporation |
| 4 |
* Copyright (c) 2025 LunarG, Inc. |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and/or associated documentation files (the "Materials"), to |
| 8 |
* deal in the Materials without restriction, including without limitation the |
| 9 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 |
* sell copies of the Materials, and to permit persons to whom the Materials are |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice(s) and this permission notice shall be included in |
| 14 |
* all copies or substantial portions of the Materials. |
| 15 |
* |
| 16 |
* THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 |
* |
| 20 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 21 |
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 22 |
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE |
| 23 |
* USE OR OTHER DEALINGS IN THE MATERIALS. |
| 24 |
* |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "wide_char_handling.h" |
| 28 |
|
| 29 |
#if defined(_WIN32) |
| 30 |
|
| 31 |
#include "Windows.h" |
| 32 |
|
| 33 |
std::string narrow(const std::wstring& utf16) { |
| 34 |
if (utf16.empty()) { |
| 35 |
return {}; |
| 36 |
} |
| 37 |
int size = WideCharToMultiByte(CP_UTF8, 0, utf16.data(), static_cast<int>(utf16.size()), nullptr, 0, nullptr, nullptr); |
| 38 |
if (size <= 0) { |
| 39 |
return {}; |
| 40 |
} |
| 41 |
std::string utf8(size, '\0'); |
| 42 |
if (WideCharToMultiByte(CP_UTF8, 0, utf16.data(), static_cast<int>(utf16.size()), &utf8[0], size, nullptr, nullptr) != size) { |
| 43 |
return {}; |
| 44 |
} |
| 45 |
return utf8; |
| 46 |
} |
| 47 |
|
| 48 |
std::wstring widen(const std::string& utf8) { |
| 49 |
if (utf8.empty()) { |
| 50 |
return {}; |
| 51 |
} |
| 52 |
int size = MultiByteToWideChar(CP_UTF8, 0, utf8.data(), static_cast<int>(utf8.size()), nullptr, 0); |
| 53 |
if (size <= 0) { |
| 54 |
return {}; |
| 55 |
} |
| 56 |
std::wstring utf16(size, L'\0'); |
| 57 |
if (MultiByteToWideChar(CP_UTF8, 0, utf8.data(), static_cast<int>(utf8.size()), &utf16[0], size) != size) { |
| 58 |
return {}; |
| 59 |
} |
| 60 |
return utf16; |
| 61 |
} |
| 62 |
#else |
| 63 |
std::string narrow(const std::string& utf16) { return utf16; } |
| 64 |
std::string widen(const std::string& utf8) { return utf8; } |
| 65 |
#endif |