-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstrutil.cpp
119 lines (107 loc) · 4.29 KB
/
strutil.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Ref: https://nekko1119.hatenablog.com/entry/2017/01/02/054629
// Special thanks to the original author(s)
#include <codecvt>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <locale>
#include <string>
#include <system_error>
#include <vector>
#include <Windows.h>
std::wstring multi_to_wide_capi(std::string const& src)
{
std::size_t converted{};
std::vector<wchar_t> dest(src.size(), L'\0');
if (::_mbstowcs_s_l(&converted, dest.data(), dest.size(), src.data(), _TRUNCATE, ::_create_locale(LC_ALL, "jpn")) != 0) {
throw std::system_error{ errno, std::system_category() };
}
dest.resize(std::char_traits<wchar_t>::length(dest.data()));
dest.shrink_to_fit();
return std::wstring(dest.begin(), dest.end());
}
std::wstring multi_to_wide_winapi(std::string const& src)
{
auto const dest_size = ::MultiByteToWideChar(CP_ACP, 0U, src.data(), -1, nullptr, 0U);
std::vector<wchar_t> dest(dest_size, L'\0');
if (::MultiByteToWideChar(CP_ACP, 0U, src.data(), -1, dest.data(), dest.size()) == 0) {
throw std::system_error{ static_cast<int>(::GetLastError()), std::system_category() };
}
dest.resize(std::char_traits<wchar_t>::length(dest.data()));
dest.shrink_to_fit();
return std::wstring(dest.begin(), dest.end());
}
std::string wide_to_multi_capi(std::wstring const& src)
{
std::size_t converted{};
std::vector<char> dest(src.size() * sizeof(wchar_t) + 1, '\0');
if (::_wcstombs_s_l(&converted, dest.data(), dest.size(), src.data(), _TRUNCATE, ::_create_locale(LC_ALL, "jpn")) != 0) {
throw std::system_error{ errno, std::system_category() };
}
dest.resize(std::char_traits<char>::length(dest.data()));
dest.shrink_to_fit();
return std::string(dest.begin(), dest.end());
}
std::string wide_to_multi_winapi(std::wstring const& src)
{
auto const dest_size = ::WideCharToMultiByte(CP_ACP, 0U, src.data(), -1, nullptr, 0, nullptr, nullptr);
std::vector<char> dest(dest_size, '\0');
if (::WideCharToMultiByte(CP_ACP, 0U, src.data(), -1, dest.data(), dest.size(), nullptr, nullptr) == 0) {
throw std::system_error{ static_cast<int>(::GetLastError()), std::system_category() };
}
dest.resize(std::char_traits<char>::length(dest.data()));
dest.shrink_to_fit();
return std::string(dest.begin(), dest.end());
}
std::string wide_to_utf8_cppapi(std::wstring const& src)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.to_bytes(src);
}
std::string wide_to_utf8_winapi(std::wstring const& src)
{
auto const dest_size = ::WideCharToMultiByte(CP_UTF8, 0U, src.data(), -1, nullptr, 0, nullptr, nullptr);
std::vector<char> dest(dest_size, '\0');
if (::WideCharToMultiByte(CP_UTF8, 0U, src.data(), -1, dest.data(), dest.size(), nullptr, nullptr) == 0) {
throw std::system_error{ static_cast<int>(::GetLastError()), std::system_category() };
}
dest.resize(std::char_traits<char>::length(dest.data()));
dest.shrink_to_fit();
return std::string(dest.begin(), dest.end());
}
std::wstring utf8_to_wide_cppapi(std::string const& src)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.from_bytes(src);
}
std::wstring utf8_to_wide_winapi(std::string const& src)
{
auto const dest_size = ::MultiByteToWideChar(CP_UTF8, 0U, src.data(), -1, nullptr, 0U);
std::vector<wchar_t> dest(dest_size, L'\0');
if (::MultiByteToWideChar(CP_UTF8, 0U, src.data(), -1, dest.data(), dest.size()) == 0) {
throw std::system_error{ static_cast<int>(::GetLastError()), std::system_category() };
}
dest.resize(std::char_traits<wchar_t>::length(dest.data()));
dest.shrink_to_fit();
return std::wstring(dest.begin(), dest.end());
}
std::string multi_to_utf8_cppapi(std::string const& src)
{
auto const wide = multi_to_wide_capi(src);
return wide_to_utf8_cppapi(wide);
}
std::string multi_to_utf8_winapi(std::string const& src)
{
auto const wide = multi_to_wide_winapi(src);
return wide_to_utf8_winapi(wide);
}
std::string utf8_to_multi_cppapi(std::string const& src)
{
auto const wide = utf8_to_wide_cppapi(src);
return wide_to_multi_capi(wide);
}
std::string utf8_to_multi_winapi(std::string const& src)
{
auto const wide = utf8_to_wide_winapi(src);
return wide_to_multi_winapi(wide);
}