1#include <c10/util/Unicode.h>
2
3namespace c10 {
4#if defined(_WIN32)
5std::wstring u8u16(const std::string& str) {
6 if (str.empty()) {
7 return std::wstring();
8 }
9 int size_needed = MultiByteToWideChar(
10 CP_UTF8, 0, str.c_str(), static_cast<int>(str.size()), NULL, 0);
11 TORCH_CHECK(size_needed > 0, "Error converting the content to Unicode");
12 std::wstring wstr(size_needed, 0);
13 MultiByteToWideChar(
14 CP_UTF8,
15 0,
16 str.c_str(),
17 static_cast<int>(str.size()),
18 &wstr[0],
19 size_needed);
20 return wstr;
21}
22std::string u16u8(const std::wstring& wstr) {
23 if (wstr.empty()) {
24 return std::string();
25 }
26 int size_needed = WideCharToMultiByte(
27 CP_UTF8,
28 0,
29 wstr.c_str(),
30 static_cast<int>(wstr.size()),
31 NULL,
32 0,
33 NULL,
34 NULL);
35 TORCH_CHECK(size_needed > 0, "Error converting the content to UTF8");
36 std::string str(size_needed, 0);
37 WideCharToMultiByte(
38 CP_UTF8,
39 0,
40 wstr.c_str(),
41 static_cast<int>(wstr.size()),
42 &str[0],
43 size_needed,
44 NULL,
45 NULL);
46 return str;
47}
48#endif
49} // namespace c10
50