-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathstrcvt.cpp
116 lines (102 loc) · 4 KB
/
strcvt.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
#include "strcvt.h"
#include <codecvt>
#include <cstdint>
#include <locale>
#ifdef _MSC_VER
#define vsprintf vsprintf_s
//#define fopen fopen_s
#include <windows.h>
// include windows.h before stringapiset.h
#include <stringapiset.h> //for MultiByteToWideChar() and WideCharToMultiByte()
#endif
#ifdef _MSC_VER
std::string strfunc::CvtStringToUTF8(const std::string& localstr)
{
int wlen = MultiByteToWideChar(CP_ACP, 0, localstr.c_str(), -1, nullptr, 0);
std::vector<wchar_t> wstr(wlen);
MultiByteToWideChar(CP_ACP, 0, localstr.c_str(), -1, wstr.data(), wlen);
int utf8len = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), -1, nullptr, 0, nullptr, nullptr);
std::vector<char> utf8str(utf8len);
WideCharToMultiByte(CP_UTF8, 0, wstr.data(), -1, utf8str.data(), utf8len, nullptr, nullptr);
std::string result(utf8str.data());
return result;
}
std::string strfunc::CvtUTF8ToLocal(const std::string& utf8str)
{
int wlen = MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), -1, nullptr, 0);
std::vector<wchar_t> wstr(wlen);
MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), -1, wstr.data(), wlen);
int locallen = WideCharToMultiByte(CP_ACP, 0, wstr.data(), -1, nullptr, 0, nullptr, nullptr);
std::vector<char> localstr(locallen);
WideCharToMultiByte(CP_ACP, 0, wstr.data(), -1, localstr.data(), locallen, nullptr, nullptr);
std::string result(localstr.data());
return result;
}
std::wstring strfunc::CvtUTF8ToWChar(const std::string& utf8str, int utf8strlen)
{
int wlen = MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), utf8strlen, nullptr, 0);
std::vector<wchar_t> wstr(wlen);
MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), utf8strlen, wstr.data(), wlen);
std::wstring ret(wstr.data());
return ret;
}
#endif // _WIN32
std::string strfunc::CvtStringToUTF8(const char16_t& src)
{
std::wstring_convert<std::codecvt_utf8_utf16<std::int16_t>, std::int16_t> convert;
auto p = reinterpret_cast<const std::int16_t*>(&src);
return convert.to_bytes(p, p + 1);
}
std::string strfunc::CvtStringToUTF8(const std::u16string& src)
{
std::wstring_convert<std::codecvt_utf8_utf16<std::int16_t>, std::int16_t> convert;
auto p = reinterpret_cast<const std::int16_t*>(src.data());
return convert.to_bytes(p, p + src.size());
}
std::string strfunc::CvtStringToUTF8(const wchar_t* start, std::uint64_t len)
{
std::wstring_convert<std::codecvt_utf8_utf16<std::int16_t>, std::int16_t> convert;
auto p = reinterpret_cast<const std::int16_t*>(start);
return convert.to_bytes(p, p + len);
}
std::string strfunc::CvtStringToUTF8(const std::wstring& str)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
return convert.to_bytes(str);
}
std::u16string strfunc::CvtStringToUTF16(const std::string& src)
{
std::wstring_convert<std::codecvt_utf8_utf16<std::int16_t>, std::int16_t> convert;
auto p = reinterpret_cast<const char*>(src.data());
auto str = convert.from_bytes(p, p + src.size());
return std::u16string(str.begin(), str.end());
}
std::u16string strfunc::CvtStringToUTF16(const char* start, int len)
{
std::wstring_convert<std::codecvt_utf8_utf16<std::int16_t>, std::int16_t> convert;
auto p = reinterpret_cast<const char*>(start);
auto str = convert.from_bytes(p, p + len);
return std::u16string(str.begin(), str.end());
}
std::wstring strfunc::CvtStringToWString(const std::string& src)
{
#ifdef _MSC_VER
return CvtUTF8ToWChar(src, -1);
#else
std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
auto p = reinterpret_cast<const char*>(src.data());
auto str = convert.from_bytes(p, p + src.size());
return std::wstring(str.begin(), str.end());
#endif
}
std::wstring strfunc::CvtStringToWString(const char* start, uint64_t len)
{
#ifdef _MSC_VER
return CvtUTF8ToWChar(start, len);
#else
std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
auto p = reinterpret_cast<const char*>(start);
auto str = convert.from_bytes(p, p + len);
return std::wstring(str.begin(), str.end());
#endif
}