-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversion.cpp
More file actions
202 lines (175 loc) · 4.71 KB
/
Copy pathConversion.cpp
File metadata and controls
202 lines (175 loc) · 4.71 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "conversion.h"
static const wchar_t WHITE_SPACE_CHARACTERS[] = L" \t\n\v\f\r\u00A0";
int Conversion::ToInt(const wstring& _buff, int _base)
{
return (int)_tcstol(_buff.c_str(), nullptr, _base);
}
wstring Conversion::LeftTrimString(const wstring& input, const wstring& chars)
{
const size_t pos = input.find_first_not_of(chars);
if (pos == wstring::npos)
return wstring();
return input.substr(pos);
}
wstring Conversion::RightTrimString(const wstring& input, const wstring& chars)
{
const size_t pos = input.find_last_not_of(chars);
if (pos == wstring::npos)
return wstring();
return input.substr(0, pos + 1);
}
wstring Conversion::TrimString(const wstring& input, const wstring& chars)
{
const size_t end = input.find_last_not_of(chars);
if (end == wstring::npos)
return wstring();
const size_t begin = input.find_first_not_of(chars);
return input.substr(begin, end - begin + 1);
}
wstring Conversion::TrimWhiteChar(const wstring& _val)
{
return TrimString(_val, WHITE_SPACE_CHARACTERS);
}
wstring Conversion::ToLower(const wstring& input)
{
if (input.empty())
return {};
wstring result = input;
CharLowerBuffW(result.data(), static_cast<DWORD>(result.size()));
return result;
}
void Conversion::StringReplaceAll(wstring& _mess, const wstring& _oldStr, const wstring& _newStr)
{
const size_t oldLen = _oldStr.length();
const size_t newLen = _newStr.length();
size_t position = 0;
while ((position = _mess.find(_oldStr, position)) != wstring::npos)
{
_mess.replace(position, oldLen, _newStr);
position += newLen;
}
}
wstring Conversion::Utf32ToUtf16(char32_t ch)
{
// BMP character (without surrogate pair)
if (ch <= 0xFFFF)
{
// We will omit the surrogate range (D800–DFFF), as these should not appear here.
if (ch >= 0xD800 && ch <= 0xDFFF)
return L"";
wchar_t w = static_cast<wchar_t>(ch);
return wstring(1, w);
}
// character outside BMP → surrogate pair
ch -= 0x10000;
wchar_t high = static_cast<wchar_t>(0xD800 + (ch >> 10));
wchar_t low = static_cast<wchar_t>(0xDC00 + (ch & 0x3FF));
wchar_t buff[2] = { high, low };
return wstring(buff, 2);
}
void Conversion::UnicodeCodeConverter(wstring& _mess)
{
size_t startPos = 0, endPos = 0;
bool unicodeProcess = true;
while (unicodeProcess)
{
startPos = _mess.find(_T("\\u{"), startPos);
if (startPos == wstring::npos) break;
if (startPos != 0) {
if (_mess[startPos - 1] == '\\') {
wstring left = _mess.substr(0, startPos);
wstring right = _mess.substr(startPos + 1, _mess.length() - startPos - 1);
_mess = left + right;
continue;
}
}
endPos = _mess.find(_T("}"), startPos);
if (endPos == wstring::npos) break;
size_t tmpPos = _mess.find(_T("\\u{"), startPos + 1);
if (tmpPos != wstring::npos && tmpPos < endPos) {
startPos = endPos;
}
else {
if (startPos < endPos) {
wstring unicode = Conversion::TrimWhiteChar(_mess.substr(startPos + 3, endPos - startPos - 3));
if (unicode.empty() == false) {
if (unicode.length() % 2 != 0)
unicode = _T("0") + unicode;
char32_t ch32 = (char32_t)_tcstol(unicode.c_str(), nullptr, 16);
wstring utf16String = Utf32ToUtf16(ch32);
// replace the entire sequence \u{XXXX}
_mess.replace(startPos, endPos - startPos + 1, utf16String);
}
else {
startPos = endPos;
}
}
else {
startPos = endPos;
}
}
}
}
wstring Conversion::ParseEscapeString(const wstring& _str)
{
wstring out;
for (size_t i = 0; i < _str.size(); ++i) {
wchar_t c = _str[i];
if (c != L'\\') {
out.push_back(c);
continue;
}
if (i + 1 >= _str.size()) {
out.push_back(L'\\');
break;
}
wchar_t n = _str[++i];
// \n, \r, \t, \xHH, \uHHHH
switch (n) {
case L'n': out.push_back(L'\n'); break;
case L'r': out.push_back(L'\r'); break;
case L't': out.push_back(L'\t'); break;
case L'\\': out.push_back(L'\\'); break;
case L'\'': out.push_back(L'\''); break;
case L'"': out.push_back(L'"'); break;
case L'x':
case L'u':
{
int maxDigits = (n == L'x') ? 2 : 4;
int val = 0;
int digits = 0;
while (i + 1 < _str.size() && digits < maxDigits) {
wchar_t h = _str[i + 1];
int d;
if (h >= L'0' && h <= L'9') d = h - L'0';
else if (h >= L'a' && h <= L'f') d = 10 + (h - L'a');
else if (h >= L'A' && h <= L'F') d = 10 + (h - L'A');
else break;
val = (val << 4) | d;
++i;
++digits;
}
if (digits == 0) {
out.push_back(L'\\');
out.push_back(n);
}
else {
if (val >= 0xD800 && val <= 0xDFFF) {
out.push_back(L'\\');
out.push_back(n);
for (int k = 0; k < digits; ++k)
out.push_back(_str[i - digits + 1 + k]);
}
else {
out.push_back((wchar_t)val);
}
}
break;
}
default:
out.push_back(n);
break;
}
}
return out;
}