-
Notifications
You must be signed in to change notification settings - Fork 19
/
unicodelib_encodings.h
616 lines (511 loc) · 15.8 KB
/
unicodelib_encodings.h
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
//
// unicodelib_encodings.h
//
// Copyright (c) 2020 Yuji Hirose. All rights reserved.
// MIT License
//
#pragma once
#include <cstdlib>
#include <string>
#if !defined(__cplusplus) || __cplusplus < 201703L
#error "Requires complete C++17 support"
#endif
/*
namespace utf8 {
size_t codepoint_length(char32_t cp);
size_t codepoint_length(const char *s8, size_t l);
size_t codepoint_count(const char *s8, size_t l);
size_t encode_codepoint(char32_t cp, std::string &out);
void encode(const char32_t *s32, size_t l, std::string &out);
size_t decode_codepoint(const char *s8, size_t l, char32_t &out);
void decode(const char *s8, size_t l, std::u32string &out);
} // namespace utf8
namespace utf16 {
size_t codepoint_length(char32_t cp);
size_t codepoint_length(const char16_t *s16, size_t l);
size_t codepoint_count(const char16_t *s16, size_t l);
size_t encode_codepoint(char32_t cp, std::u16string &out);
void encode(const char32_t *s32, size_t l, std::u16string &out);
size_t decode_codepoint(const char16_t *s16, size_t l, char32_t &out);
void decode(const char16_t *s16, size_t l, std::u32string &out);
} // namespace utf16
std::string to_utf8(const char16_t *s16, size_t l);
std::u16string to_utf16(const char *s8, size_t l);
std::wstring to_wstring(const char *s8, size_t l);
std::wstring to_wstring(const char16_t *s16, size_t l);
std::wstring to_wstring(const char32_t *s32, size_t l);
std::string to_utf8(const wchar_t *sw, size_t l);
std::u16string to_utf16(const wchar_t *sw, size_t l);
std::u32string to_utf32(const wchar_t *sw, size_t l);
*/
namespace unicode {
//-----------------------------------------------------------------------------
// UTF8 encoding
//-----------------------------------------------------------------------------
namespace utf8 {
inline size_t codepoint_length(char32_t cp) {
if (cp < 0x0080) {
return 1;
} else if (cp < 0x0800) {
return 2;
} else if (cp < 0xD800) {
return 3;
} else if (cp < 0xe000) {
// D800 - DFFF is invalid...
return 0;
} else if (cp < 0x10000) {
return 3;
} else if (cp < 0x110000) {
return 4;
}
return 0;
}
inline size_t codepoint_length(const char *s8, size_t l) {
if (l) {
uint8_t b = s8[0];
if ((b & 0x80) == 0) {
return 1;
} else if ((b & 0xE0) == 0xC0) {
return 2;
} else if ((b & 0xF0) == 0xE0) {
return 3;
} else if ((b & 0xF8) == 0xF0) {
return 4;
}
}
return 0;
}
inline size_t codepoint_count(const char *s8, size_t l) {
size_t count = 0;
for (size_t i = 0; i < l; i += codepoint_length(s8 + i, l - i)) {
count++;
}
return count;
}
inline size_t encode_codepoint(char32_t cp, char *buff) {
if (cp < 0x0080) {
buff[0] = static_cast<uint8_t>(cp & 0x7F);
return 1;
} else if (cp < 0x0800) {
buff[0] = static_cast<uint8_t>(0xC0 | ((cp >> 6) & 0x1F));
buff[1] = static_cast<uint8_t>(0x80 | (cp & 0x3F));
return 2;
} else if (cp < 0xD800) {
buff[0] = static_cast<uint8_t>(0xE0 | ((cp >> 12) & 0xF));
buff[1] = static_cast<uint8_t>(0x80 | ((cp >> 6) & 0x3F));
buff[2] = static_cast<uint8_t>(0x80 | (cp & 0x3F));
return 3;
} else if (cp < 0xE000) {
// D800 - DFFF is invalid...
return 0;
} else if (cp < 0x10000) {
buff[0] = static_cast<uint8_t>(0xE0 | ((cp >> 12) & 0xF));
buff[1] = static_cast<uint8_t>(0x80 | ((cp >> 6) & 0x3F));
buff[2] = static_cast<uint8_t>(0x80 | (cp & 0x3F));
return 3;
} else if (cp < 0x110000) {
buff[0] = static_cast<uint8_t>(0xF0 | ((cp >> 18) & 0x7));
buff[1] = static_cast<uint8_t>(0x80 | ((cp >> 12) & 0x3F));
buff[2] = static_cast<uint8_t>(0x80 | ((cp >> 6) & 0x3F));
buff[3] = static_cast<uint8_t>(0x80 | (cp & 0x3F));
return 4;
}
return 0;
}
inline size_t encode_codepoint(char32_t cp, std::string &out) {
char buff[4];
auto l = encode_codepoint(cp, buff);
out.append(buff, l);
return l;
}
inline void encode(const char32_t *s32, size_t l, std::string &out) {
for (size_t i = 0; i < l; i++) {
encode_codepoint(s32[i], out);
}
}
inline bool decode_codepoint(const char *s8, size_t l, size_t &bytes,
char32_t &cp) {
if (l) {
uint8_t b = s8[0];
if ((b & 0x80) == 0) {
bytes = 1;
cp = b;
return true;
} else if ((b & 0xE0) == 0xC0) {
if (l >= 2) {
bytes = 2;
cp = ((static_cast<char32_t>(s8[0] & 0x1F)) << 6) |
(static_cast<char32_t>(s8[1] & 0x3F));
return true;
}
} else if ((b & 0xF0) == 0xE0) {
if (l >= 3) {
bytes = 3;
cp = ((static_cast<char32_t>(s8[0] & 0x0F)) << 12) |
((static_cast<char32_t>(s8[1] & 0x3F)) << 6) |
(static_cast<char32_t>(s8[2] & 0x3F));
return true;
}
} else if ((b & 0xF8) == 0xF0) {
if (l >= 4) {
bytes = 4;
cp = ((static_cast<char32_t>(s8[0] & 0x07)) << 18) |
((static_cast<char32_t>(s8[1] & 0x3F)) << 12) |
((static_cast<char32_t>(s8[2] & 0x3F)) << 6) |
(static_cast<char32_t>(s8[3] & 0x3F));
return true;
}
}
}
return false;
}
inline size_t decode_codepoint(const char *s8, size_t l, char32_t &out) {
size_t bytes;
if (decode_codepoint(s8, l, bytes, out)) {
return bytes;
}
return 0;
}
template <typename T>
inline void for_each(const char *s8, size_t l, T callback) {
size_t id = 0;
size_t i = 0;
while (i < l) {
auto beg = i++;
while (i < l && (s8[i] & 0xc0) == 0x80) {
i++;
}
callback(s8, l, beg, i, id++);
}
}
inline void decode(const char *s8, size_t l, std::u32string &out) {
for_each(
s8, l,
[&](const char *s, size_t /*l*/, size_t beg, size_t end, size_t /*i*/) {
size_t bytes;
char32_t cp;
decode_codepoint(&s[beg], (end - beg), bytes, cp);
out += cp;
});
}
} // namespace utf8
//-----------------------------------------------------------------------------
// UTF16 encoding
//-----------------------------------------------------------------------------
namespace utf16 {
inline bool is_surrogate_pair(const char16_t *s16, size_t l) {
if (l > 0) {
auto first = s16[0];
if (0xD800 <= first && first < 0xDC00) {
auto second = s16[1];
if (0xDC00 <= second && second < 0xE000) {
return true;
}
}
}
return false;
}
inline size_t codepoint_length(char32_t cp) { return cp <= 0xFFFF ? 1 : 2; }
inline size_t codepoint_length(const char16_t *s16, size_t l) {
if (l > 0) {
if (is_surrogate_pair(s16, l)) {
return 2;
}
return 1;
}
return 0;
}
inline size_t codepoint_count(const char16_t *s16, size_t l) {
size_t count = 0;
for (size_t i = 0; i < l; i += codepoint_length(s16 + i, l - i)) {
count++;
}
return count;
}
inline size_t encode_codepoint(char32_t cp, char16_t *buff) {
if (cp < 0xD800) {
buff[0] = static_cast<char16_t>(cp);
return 1;
} else if (cp < 0xE000) {
// D800 - DFFF is invalid...
return 0;
} else if (cp < 0x10000) {
buff[0] = static_cast<char16_t>(cp);
return 1;
} else if (cp < 0x110000) {
// high surrogate
buff[0] = static_cast<char16_t>(0xD800 + (((cp - 0x10000) >> 10) & 0x3FF));
// low surrogate
buff[1] = static_cast<char16_t>(0xDC00 + ((cp - 0x10000) & 0x3FF));
return 2;
}
return 0;
}
inline size_t encode_codepoint(char32_t cp, std::u16string &out) {
char16_t buff[2];
auto l = encode_codepoint(cp, buff);
out.append(buff, l);
return l;
}
inline void encode(const char32_t *s32, size_t l, std::u16string &out) {
for (size_t i = 0; i < l; i++) {
encode_codepoint(s32[i], out);
}
}
inline bool decode_codepoint(const char16_t *s16, size_t l, size_t &length,
char32_t &cp) {
if (l) {
// Surrogate
auto first = s16[0];
if (0xD800 <= first && first < 0xDC00) {
if (l >= 2) {
auto second = s16[1];
if (0xDC00 <= second && second < 0xE000) {
cp = (((first - 0xD800) << 10) | (second - 0xDC00)) + 0x10000;
length = 2;
return true;
}
}
}
// Non surrogate
else {
cp = first;
length = 1;
return true;
}
}
return false;
}
inline size_t decode_codepoint(const char16_t *s16, size_t l, char32_t &out) {
size_t length;
if (decode_codepoint(s16, l, length, out)) {
return length;
}
return 0;
}
template <typename T>
inline void for_each(const char16_t *s16, size_t l, T callback) {
size_t id = 0;
size_t i = 0;
while (i < l) {
auto beg = i++;
if (is_surrogate_pair(&s16[beg], l - beg)) {
i++;
}
callback(s16, l, beg, i, id++);
}
}
inline void decode(const char16_t *s16, size_t l, std::u32string &out) {
for_each(s16, l,
[&](const char16_t *s, size_t /*l*/, size_t beg, size_t end,
size_t /*i*/) {
size_t length;
char32_t cp;
decode_codepoint(&s[beg], (end - beg), length, cp);
out += cp;
});
}
} // namespace utf16
//-----------------------------------------------------------------------------
// Inline Wrapper functions
//-----------------------------------------------------------------------------
namespace utf8 {
inline size_t codepoint_length(std::string_view s8) {
return codepoint_length(s8.data(), s8.length());
}
inline size_t codepoint_count(std::string_view s8) {
return codepoint_count(s8.data(), s8.length());
}
inline std::string encode_codepoint(char32_t cp) {
std::string out;
encode_codepoint(cp, out);
return out;
}
inline void encode(std::u32string_view s32, std::string &out) {
encode(s32.data(), s32.length(), out);
}
inline std::string encode(const char32_t *s32, size_t l) {
std::string out;
encode(s32, l, out);
return out;
}
inline std::string encode(std::u32string_view s32) {
return encode(s32.data(), s32.length());
}
inline size_t decode_codepoint(std::string_view s8, char32_t &cp) {
return decode_codepoint(s8.data(), s8.length(), cp);
}
inline char32_t decode_codepoint(const char *s8, size_t l) {
char32_t out = 0;
decode_codepoint(s8, l, out);
return out;
}
inline char32_t decode_codepoint(std::string_view s8) {
return decode_codepoint(s8.data(), s8.length());
}
inline void decode(std::string_view s8, std::u32string &out) {
decode(s8.data(), s8.length(), out);
}
inline std::u32string decode(const char *s8, size_t l) {
std::u32string out;
decode(s8, l, out);
return out;
}
inline std::u32string decode(std::string_view s8) {
return decode(s8.data(), s8.length());
}
} // namespace utf8
namespace utf16 {
inline size_t codepoint_length(std::u16string_view s16) {
return codepoint_length(s16.data(), s16.length());
}
inline size_t codepoint_count(std::u16string_view s16) {
return codepoint_count(s16.data(), s16.length());
}
inline std::u16string encode_codepoint(char32_t cp) {
std::u16string out;
encode_codepoint(cp, out);
return out;
}
inline void encode(std::u32string_view s32, std::u16string &out) {
encode(s32.data(), s32.length(), out);
}
inline std::u16string encode(const char32_t *s32, size_t l) {
std::u16string out;
encode(s32, l, out);
return out;
}
inline std::u16string encode(std::u32string_view s32) {
return encode(s32.data(), s32.length());
}
inline size_t decode_codepoint(std::u16string_view s16, char32_t &cp) {
return decode_codepoint(s16.data(), s16.length(), cp);
}
inline char32_t decode_codepoint(const char16_t *s16, size_t l) {
char32_t out = 0;
decode_codepoint(s16, l, out);
return out;
}
inline char32_t decode_codepoint(std::u16string_view s16) {
return decode_codepoint(s16.data(), s16.length());
}
inline void decode(std::u16string_view s16, std::u32string &out) {
decode(s16.data(), s16.length(), out);
}
inline std::u32string decode(const char16_t *s16, size_t l) {
std::u32string out;
decode(s16, l, out);
return out;
}
inline std::u32string decode(std::u16string_view s16) {
return decode(s16.data(), s16.length());
}
} // namespace utf16
//-----------------------------------------------------------------------------
// utf8/utf16 conversion
//-----------------------------------------------------------------------------
inline std::string to_utf8(const char16_t *s16, size_t l) {
return utf8::encode(utf16::decode(s16, l));
}
inline std::string to_utf8(std::u16string_view s16) {
return to_utf8(s16.data(), s16.length());
}
inline std::u16string to_utf16(const char *s8, size_t l) {
return utf16::encode(utf8::decode(s8, l));
}
inline std::u16string to_utf16(std::string_view s8) {
return to_utf16(s8.data(), s8.length());
}
//-----------------------------------------------------------------------------
// std::wstring conversion
//-----------------------------------------------------------------------------
namespace detail {
inline std::wstring to_wstring_core(const char *s8, size_t l) {
if constexpr (sizeof(wchar_t) == 2) {
auto s16 = utf16::encode(utf8::decode(s8, l));
return std::wstring(s16.begin(), s16.end());
} else if constexpr (sizeof(wchar_t) == 4) {
auto s32 = utf8::decode(s8, l);
return std::wstring(s32.begin(), s32.end());
}
}
inline std::wstring to_wstring_core(const char16_t *s16, size_t l) {
if constexpr (sizeof(wchar_t) == 2) {
return std::wstring(s16, s16 + l);
} else if constexpr (sizeof(wchar_t) == 4) {
auto s32 = utf16::decode(s16, l);
return std::wstring(s32.begin(), s32.end());
}
}
inline std::wstring to_wstring_core(const char32_t *s32, size_t l) {
if constexpr (sizeof(wchar_t) == 2) {
auto s16 = utf16::encode(s32, l);
return std::wstring(s16.begin(), s16.end());
} else if constexpr (sizeof(wchar_t) == 4) {
return std::wstring(s32, s32 + l);
}
}
inline std::string to_utf8_core(const wchar_t *sw, size_t l) {
if constexpr (sizeof(wchar_t) == 2) {
std::u16string buf(sw, sw + l);
return utf8::encode(utf16::decode(buf.data(), l));
} else if constexpr (sizeof(wchar_t) == 4) {
std::u32string buf(sw, sw + l);
return utf8::encode(buf.data(), l);
}
}
inline std::u16string to_utf16_core(const wchar_t *sw, size_t l) {
if constexpr (sizeof(wchar_t) == 2) {
return std::u16string(sw, sw + l);
} else if constexpr (sizeof(wchar_t) == 4) {
std::u32string buf(sw, sw + l);
return utf16::encode(buf.data(), l);
}
}
inline std::u32string to_utf32_core(const wchar_t *sw, size_t l) {
if constexpr (sizeof(wchar_t) == 2) {
std::u16string buf(sw, sw + l);
return utf16::decode(buf.data(), l);
} else if constexpr (sizeof(wchar_t) == 4) {
return std::u32string(sw, sw + l);
}
}
} // namespace detail
inline std::wstring to_wstring(const char *s8, size_t l) {
return detail::to_wstring_core(s8, l);
}
inline std::wstring to_wstring(std::string_view s8) {
return to_wstring(s8.data(), s8.length());
}
inline std::wstring to_wstring(const char16_t *s16, size_t l) {
return detail::to_wstring_core(s16, l);
}
inline std::wstring to_wstring(std::u16string_view s16) {
return to_wstring(s16.data(), s16.length());
}
inline std::wstring to_wstring(const char32_t *s32, size_t l) {
return detail::to_wstring_core(s32, l);
}
inline std::wstring to_wstring(std::u32string_view s32) {
return to_wstring(s32.data(), s32.length());
}
inline std::string to_utf8(const wchar_t *sw, size_t l) {
return detail::to_utf8_core(sw, l);
}
inline std::string to_utf8(std::wstring_view sw) {
return to_utf8(sw.data(), sw.length());
}
inline std::u16string to_utf16(const wchar_t *sw, size_t l) {
return detail::to_utf16_core(sw, l);
}
inline std::u16string to_utf16(std::wstring_view sw) {
return to_utf16(sw.data(), sw.length());
}
inline std::u32string to_utf32(const wchar_t *sw, size_t l) {
return detail::to_utf32_core(sw, l);
}
inline std::u32string to_utf32(std::wstring_view sw) {
return to_utf32(sw.data(), sw.length());
}
} // namespace unicode
// vim: et ts=2 sw=2 cin cino=\:0 ff=unix