-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUnicodeConv.cpp
332 lines (296 loc) · 8.55 KB
/
UnicodeConv.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "UnicodeConv.h"
namespace {
int GetU8ByteCount(char ch) {
if (0 <= uint8_t(ch) && uint8_t(ch) < 0x80) {
return 1;
}
if (0xC2 <= uint8_t(ch) && uint8_t(ch) < 0xE0) {
return 2;
}
if (0xE0 <= uint8_t(ch) && uint8_t(ch) < 0xF0) {
return 3;
}
if (0xF0 <= uint8_t(ch) && uint8_t(ch) < 0xF8) {
return 4;
}
return 0;
}
bool IsU8LaterByte(char ch) {
return 0x80 <= uint8_t(ch) && uint8_t(ch) < 0xC0;
}
bool IsU16HighSurrogate(char16_t ch) { return 0xD800 <= ch && ch < 0xDC00; }
bool IsU16LowSurrogate(char16_t ch) { return 0xDC00 <= ch && ch < 0xE000; }
} // namespace
bool ConvChU8ToU16(const std::array<char, 4>& u8Ch,
std::array<char16_t, 2>& u16Ch) {
char32_t u32Ch;
if (!ConvChU8ToU32(u8Ch, u32Ch)) {
return false;
}
if (!ConvChU32ToU16(u32Ch, u16Ch)) {
return false;
}
return true;
}
bool ConvChU8ToU32(const std::array<char, 4>& u8Ch, char32_t& u32Ch) {
int numBytes = GetU8ByteCount(u8Ch[0]);
if (numBytes == 0) {
return false;
}
switch (numBytes) {
case 1:
u32Ch = char32_t(uint8_t(u8Ch[0]));
break;
case 2:
if (!IsU8LaterByte(u8Ch[1])) {
return false;
}
if ((uint8_t(u8Ch[0]) & 0x1E) == 0) {
return false;
}
u32Ch = char32_t(u8Ch[0] & 0x1F) << 6;
u32Ch |= char32_t(u8Ch[1] & 0x3F);
break;
case 3:
if (!IsU8LaterByte(u8Ch[1]) || !IsU8LaterByte(u8Ch[2])) {
return false;
}
if ((uint8_t(u8Ch[0]) & 0x0F) == 0 &&
(uint8_t(u8Ch[1]) & 0x20) == 0) {
return false;
}
u32Ch = char32_t(u8Ch[0] & 0x0F) << 12;
u32Ch |= char32_t(u8Ch[1] & 0x3F) << 6;
u32Ch |= char32_t(u8Ch[2] & 0x3F);
break;
case 4:
if (!IsU8LaterByte(u8Ch[1]) || !IsU8LaterByte(u8Ch[2]) ||
!IsU8LaterByte(u8Ch[3])) {
return false;
}
if ((uint8_t(u8Ch[0]) & 0x07) == 0 &&
(uint8_t(u8Ch[1]) & 0x30) == 0) {
return false;
}
u32Ch = char32_t(u8Ch[0] & 0x07) << 18;
u32Ch |= char32_t(u8Ch[1] & 0x3F) << 12;
u32Ch |= char32_t(u8Ch[2] & 0x3F) << 6;
u32Ch |= char32_t(u8Ch[3] & 0x3F);
break;
default:
return false;
}
return true;
}
bool ConvChU16ToU8(const std::array<char16_t, 2>& u16Ch,
std::array<char, 4>& u8Ch) {
char32_t u32Ch;
if (!ConvChU16ToU32(u16Ch, u32Ch)) {
return false;
}
if (!ConvChU32ToU8(u32Ch, u8Ch)) {
return false;
}
return true;
}
bool ConvChU16ToU32(const std::array<char16_t, 2>& u16Ch, char32_t& u32Ch) {
if (IsU16HighSurrogate(u16Ch[0])) {
if (IsU16LowSurrogate(u16Ch[1])) {
u32Ch = 0x10000 + (char32_t(u16Ch[0]) - 0xD800) * 0x400 +
(char32_t(u16Ch[1]) - 0xDC00);
} else if (u16Ch[1] == 0) {
u32Ch = u16Ch[0];
} else {
return false;
}
} else if (IsU16LowSurrogate(u16Ch[0])) {
if (u16Ch[1] == 0) {
u32Ch = u16Ch[0];
} else {
return false;
}
} else {
u32Ch = u16Ch[0];
}
return true;
}
bool ConvChU32ToU8(const char32_t u32Ch, std::array<char, 4>& u8Ch) {
if (u32Ch > 0x10FFFF) {
return false;
}
if (u32Ch < 128) {
u8Ch[0] = char(u32Ch);
u8Ch[1] = 0;
u8Ch[2] = 0;
u8Ch[3] = 0;
} else if (u32Ch < 2048) {
u8Ch[0] = 0xC0 | char(u32Ch >> 6);
u8Ch[1] = 0x80 | (char(u32Ch) & 0x3F);
u8Ch[2] = 0;
u8Ch[3] = 0;
} else if (u32Ch < 65536) {
u8Ch[0] = 0xE0 | char(u32Ch >> 12);
u8Ch[1] = 0x80 | (char(u32Ch >> 6) & 0x3F);
u8Ch[2] = 0x80 | (char(u32Ch) & 0x3F);
u8Ch[3] = 0;
} else {
u8Ch[0] = 0xF0 | char(u32Ch >> 18);
u8Ch[1] = 0x80 | (char(u32Ch >> 12) & 0x3F);
u8Ch[2] = 0x80 | (char(u32Ch >> 6) & 0x3F);
u8Ch[3] = 0x80 | (char(u32Ch) & 0x3F);
}
return true;
}
bool ConvChU32ToU16(const char32_t u32Ch, std::array<char16_t, 2>& u16Ch) {
if (u32Ch > 0x10FFFF) {
return false;
}
if (u32Ch < 0x10000) {
u16Ch[0] = char16_t(u32Ch);
u16Ch[1] = 0;
} else {
u16Ch[0] = char16_t((u32Ch - 0x10000) / 0x400 + 0xD800);
u16Ch[1] = char16_t((u32Ch - 0x10000) % 0x400 + 0xDC00);
}
return true;
}
bool ConvU8ToU16(const std::string& u8Str, std::u16string& u16Str) {
for (auto u8It = u8Str.begin(); u8It != u8Str.end(); ++u8It) {
auto numBytes = GetU8ByteCount((*u8It));
if (numBytes == 0) {
return false;
}
std::array<char, 4> u8Ch;
u8Ch[0] = (*u8It);
for (int i = 1; i < numBytes; i++) {
++u8It;
if (u8It == u8Str.end()) {
return false;
}
u8Ch[i] = (*u8It);
}
std::array<char16_t, 2> u16Ch;
if (!ConvChU8ToU16(u8Ch, u16Ch)) {
return false;
}
u16Str.push_back(u16Ch[0]);
if (u16Ch[1] != 0) {
u16Str.push_back(u16Ch[1]);
}
}
return true;
}
bool ConvU8ToU32(const std::string& u8Str, std::u32string& u32Str) {
for (auto u8It = u8Str.begin(); u8It != u8Str.end(); ++u8It) {
auto numBytes = GetU8ByteCount((*u8It));
if (numBytes == 0) {
return false;
}
std::array<char, 4> u8Ch;
u8Ch[0] = (*u8It);
for (int i = 1; i < numBytes; i++) {
++u8It;
if (u8It == u8Str.end()) {
return false;
}
u8Ch[i] = (*u8It);
}
char32_t u32Ch;
if (!ConvChU8ToU32(u8Ch, u32Ch)) {
return false;
}
u32Str.push_back(u32Ch);
}
return true;
}
bool ConvU16ToU8(const std::u16string& u16Str, std::string& u8Str) {
for (auto u16It = u16Str.begin(); u16It != u16Str.end(); ++u16It) {
std::array<char16_t, 2> u16Ch;
if (IsU16HighSurrogate((*u16It))) {
u16Ch[0] = (*u16It);
++u16It;
if (u16It == u16Str.end()) {
return false;
}
u16Ch[1] = (*u16It);
} else {
u16Ch[0] = (*u16It);
u16Ch[1] = 0;
}
std::array<char, 4> u8Ch;
if (!ConvChU16ToU8(u16Ch, u8Ch)) {
return false;
}
if (u8Ch[0] != 0) {
u8Str.push_back(u8Ch[0]);
}
if (u8Ch[1] != 0) {
u8Str.push_back(u8Ch[1]);
}
if (u8Ch[2] != 0) {
u8Str.push_back(u8Ch[2]);
}
if (u8Ch[3] != 0) {
u8Str.push_back(u8Ch[3]);
}
}
return true;
}
bool ConvU16ToU32(const std::u16string& u16Str, std::u32string& u32Str) {
for (auto u16It = u16Str.begin(); u16It != u16Str.end(); ++u16It) {
std::array<char16_t, 2> u16Ch;
if (IsU16HighSurrogate((*u16It))) {
u16Ch[0] = (*u16It);
++u16It;
if (u16It == u16Str.end()) {
return false;
}
u16Ch[1] = (*u16It);
} else {
u16Ch[0] = (*u16It);
u16Ch[1] = 0;
}
char32_t u32Ch;
if (!ConvChU16ToU32(u16Ch, u32Ch)) {
return false;
}
u32Str.push_back(u32Ch);
}
return true;
}
bool ConvU32ToU8(const std::u32string& u32Str, std::string& u8Str) {
for (auto u32It = u32Str.begin(); u32It != u32Str.end(); ++u32It) {
std::array<char, 4> u8Ch;
if (!ConvChU32ToU8((*u32It), u8Ch)) {
return false;
}
if (u8Ch[0] != 0) {
u8Str.push_back(u8Ch[0]);
}
if (u8Ch[1] != 0) {
u8Str.push_back(u8Ch[1]);
}
if (u8Ch[2] != 0) {
u8Str.push_back(u8Ch[2]);
}
if (u8Ch[3] != 0) {
u8Str.push_back(u8Ch[3]);
}
}
return true;
}
bool ConvU32ToU16(const std::u32string& u32Str, std::u16string& u16Str) {
for (auto u32It = u32Str.begin(); u32It != u32Str.end(); ++u32It) {
std::array<char16_t, 2> u16Ch;
if (!ConvChU32ToU16((*u32It), u16Ch)) {
return false;
}
if (u16Ch[0] != 0) {
u16Str.push_back(u16Ch[0]);
}
if (u16Ch[1] != 0) {
u16Str.push_back(u16Ch[1]);
}
}
return true;
}