forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidechar_conv.h
More file actions
49 lines (37 loc) · 1.43 KB
/
widechar_conv.h
File metadata and controls
49 lines (37 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#pragma once
#if defined(_WIN32)
#include <windows.h>
#include <string>
namespace cortex::wc {
inline std::string WstringToUtf8(const std::wstring& wstr) {
if (wstr.empty()) {
return std::string();
}
int size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), NULL, 0, NULL, NULL);
if (size_needed <= 0) {
throw std::runtime_error("WideCharToMultiByte() failed: " + std::to_string(GetLastError()));
}
std::string result(size_needed, 0);
int bytes_written = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), &result[0], size_needed, NULL, NULL);
if (bytes_written <= 0) {
throw std::runtime_error("WideCharToMultiByte() failed: " + std::to_string(GetLastError()));
}
return result;
}
inline std::wstring Utf8ToWstring(const std::string& str) {
if (str.empty()) {
return std::wstring();
}
int size_needed = MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), NULL, 0);
if (size_needed <= 0) {
throw std::runtime_error("MultiByteToWideChar() failed: " + std::to_string(GetLastError()));
}
std::wstring result(size_needed, 0);
int chars_written = MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), &result[0], size_needed);
if (chars_written <= 0) {
throw std::runtime_error("MultiByteToWideChar() failed: " + std::to_string(GetLastError()));
}
return result;
}
};
#endif