-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodecvt.cpp
More file actions
1177 lines (1043 loc) · 36.3 KB
/
codecvt.cpp
File metadata and controls
1177 lines (1043 loc) · 36.3 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
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <concepts>
#include <cstdint>
#include <iterator>
#include <ranges>
#include <string_view>
#include <type_traits>
#include <utility>
namespace typeutil
{
// Select first of First, Rest... that is a baseclass of T
template<typename T, typename First, typename... Rest>
struct first_baseclass
{
using type = std::conditional_t<std::is_base_of_v<First, T>, First, typename first_baseclass<T, Rest...>::type>;
};
template<typename T, typename First>
struct first_baseclass<T, First>
{
using type = std::enable_if_t<std::is_base_of_v<First, T>, First>;
};
template<typename... Args>
using first_baseclass_t = first_baseclass<Args...>::type;
}
namespace codecvt
{
using unit_count = std::int_least8_t;
static constexpr char16_t replacement_char = u'\uFFFD';
// Iterators
template<std::input_iterator Input>
requires std::is_same_v<typename std::iterator_traits<Input>::value_type, char32_t>
class to_utf16_iterator
{
public:
using iterator_category = typeutil::first_baseclass_t<typename std::iterator_traits<Input>::iterator_category,
std::bidirectional_iterator_tag,
std::forward_iterator_tag,
std::input_iterator_tag>;
using difference_type = std::ptrdiff_t;
using value_type = char16_t;
using pointer = value_type const*;
using reference = value_type const&;
private:
static constexpr unit_count first_unit = 0; // magic values for length and remaining
static constexpr unit_count last_unit = -1;
Input pos = {}; // underlying position
mutable unit_count length = first_unit; // number of UTF-16 units for this pos (<=0 means unknown)
mutable unit_count remaining = first_unit; // number of UTF-16 units left (0 means all, -1 means last)
mutable value_type current = 0; // result of * operator
auto compare_key() const
{ return std::tuple{ pos, remaining == length ? 0 : remaining }; }
public:
to_utf16_iterator() = default;
explicit to_utf16_iterator(Input pos)
: pos{pos}
{}
bool operator==(const to_utf16_iterator& other) const
{
return compare_key() == other.compare_key();
}
reference operator*() const
{
if (current) {
return current;
}
auto c = *pos;
if (c <= 0xFFFF) {
remaining = length = 1;
if (0xD800 <= c && c <= 0xDFFF) {
// non-character UTF-32 units
return current = replacement_char;
}
return current = static_cast<value_type>(c);
}
if (c > U'\U0010ffff') {
// out of range
remaining = length = 1;
return current = replacement_char;
}
if (length <= 0) {
length = 2;
if (remaining == first_unit) {
remaining = length;
} else {
remaining = 1;
}
}
c -= 0x10000;
if (remaining == 2) {
return current = static_cast<value_type>(c >> 10) | 0xD800;
} else {
return current = static_cast<value_type>(c & 0x3FF) | 0xDC00;
}
}
pointer operator->() const
{
return &**this;
}
to_utf16_iterator& operator++()
{
if (length == first_unit) {
// compute the length
operator*();
}
if (length == last_unit || --remaining == 0) {
++pos;
remaining = length = first_unit;
}
current = 0;
return *this;
}
to_utf16_iterator operator++(int)
{
if (!current) {
// ensure current is valid in copy
operator*();
}
auto const it = *this;
++*this;
return it;
}
to_utf16_iterator& operator--()
requires std::is_base_of_v<std::bidirectional_iterator_tag, iterator_category>
{
if (length == last_unit) {
// compute the length
operator*();
}
if (length == first_unit || remaining++ == length) {
--pos;
remaining = length = last_unit;
}
current = 0;
return *this;
}
to_utf16_iterator operator--(int)
requires std::is_base_of_v<std::bidirectional_iterator_tag, iterator_category>
{
auto const it = *this;
--*this;
return it;
}
};
template<std::input_iterator Input>
requires std::is_same_v<typename std::iterator_traits<Input>::value_type, char32_t>
class to_utf8_iterator
{
public:
using iterator_category = typeutil::first_baseclass_t<typename std::iterator_traits<Input>::iterator_category,
std::bidirectional_iterator_tag,
std::forward_iterator_tag,
std::input_iterator_tag>;
using difference_type = std::ptrdiff_t;
using value_type = char8_t;
using pointer = value_type const*;
using reference = value_type;
private:
static constexpr unit_count first_unit = 0; // magic values for length and remaining
static constexpr unit_count last_unit = -1;
Input pos = {}; // current read position
mutable unit_count length = first_unit; // number of UTF-8 units for this pos (<=0 means unknown)
mutable unit_count remaining = first_unit; // number of UTF-8 units left (0 means all, -1 means last)
mutable value_type current = 0; // result of * operator
auto compare_key() const
{ return std::tuple{ pos, remaining == length ? 0 : remaining }; }
public:
to_utf8_iterator() = default;
explicit to_utf8_iterator(Input pos)
: pos{pos}
{}
bool operator==(const to_utf8_iterator& other) const
{
return compare_key() == other.compare_key();
}
reference operator*() const
{
if (current) {
return current;
}
auto c = *pos;
if (c <= 0x7F) {
// ascii
remaining = length = 1;
return current = static_cast<value_type>(c);
}
if (0xD800 <= c && c <= 0xDFFF) {
// reject UTF-16 surrogates
c = replacement_char;
}
if (length <= 0) {
auto const backwards = length == last_unit;
// compute the length
auto mask = ~0x3Fu;
length = 1;
for (auto ch = c; ch & mask; ch >>= 6, mask >>= 1) {
++length;
}
remaining = backwards ? 1 : length;
}
auto const shift = remaining - 1;
if (remaining == length) {
return current = static_cast<value_type>(c >> (6 * shift) | ~(0x7Fu >> shift) & 0xFF);
} else {
// continuation byte
return current = (c >> (6 * shift)) & 0x3F | 0x80;
}
}
to_utf8_iterator& operator++()
{
if (length == 0) {
// compute the length
operator*();
}
if (--remaining == 0) {
++pos;
length = 0;
}
current = 0;
return *this;
}
to_utf8_iterator operator++(int)
{
if (!current) {
// ensure current is valid in copy
operator*();
}
auto it = *this;
++*this;
return it;
}
to_utf8_iterator& operator--()
requires std::is_base_of_v<std::bidirectional_iterator_tag, iterator_category>
{
if (length == last_unit) {
// compute the length
operator*();
}
if (length == first_unit || remaining++ == length) {
--pos;
remaining = length = last_unit;
}
current = 0;
return *this;
}
to_utf8_iterator operator--(int)
requires std::is_base_of_v<std::bidirectional_iterator_tag, iterator_category>
{
auto const it = *this;
--*this;
return it;
}
};
template<std::input_iterator Input>
requires std::is_same_v<typename std::iterator_traits<Input>::value_type, char16_t>
class from_utf16_iterator
{
public:
using iterator_category = typeutil::first_baseclass_t<typename std::iterator_traits<Input>::iterator_category,
std::bidirectional_iterator_tag,
std::forward_iterator_tag,
std::input_iterator_tag>;
using difference_type = std::ptrdiff_t;
using value_type = char32_t;
using pointer = value_type const*;
using reference = value_type&;
private:
static constexpr bool is_bidirectional = std::is_base_of_v<std::bidirectional_iterator_tag, iterator_category>;
mutable Input pos = {}; // current read position
mutable value_type current = 0;
mutable enum class charpos { before, only, first, second, last, after} state = charpos::before;
public:
from_utf16_iterator() = default;
explicit from_utf16_iterator(Input pos)
: pos{pos}
{}
bool operator==(const from_utf16_iterator& other) const { return pos == other.pos; }
reference operator*() const
{
switch (state) {
case charpos::only:
case charpos::first:
case charpos::second:
case charpos::after:
// we've already read the codepoint here
return current;
case charpos::before:
current = static_cast<char16_t>(*pos);
if ((current & 0xF800) != 0xD800) {
state = charpos::only;
current = *pos;
break;
}
if ((current & 0xFC00) == 0xD800) {
// it's an initial surrogate
value_type const c = static_cast<char16_t>(*++pos);
if ((c & 0xFC00) != 0xDC00) {
// not a continuation surrogate
state = charpos::after;
return current = replacement_char;
}
state = charpos::second;
return current = 0x10000 + ((current & 0x3FF) << 10) + (c & 0x3FF);
}
// else, an unexpected continuation surrogate
state = charpos::only;
return current = replacement_char;
case charpos::last:
if constexpr (is_bidirectional) {
current = static_cast<char16_t>(*pos);
if ((current & 0xF800) != 0xD800) {
state = charpos::only;
current = *pos;
break;
}
if ((*pos & 0xFC00) == 0xDC00) {
// it's a continuation surrogate
value_type const c = static_cast<char16_t>(*--pos);
if ((c & 0xFC00) != 0xD800) {
// not an initial surrogate
++pos;
state = charpos::only;
return current = replacement_char;
}
state = charpos::first;
return current = 0x10000 + ((c & 0x3FF) << 10) + (current & 0x3FF);
}
// else, an unexpected initial surrogate
state = charpos::only;
return current = replacement_char;
}
std::unreachable();
}
if ((current & 0xFFFE) == 0xFFFE) {
// reject problematic non-characters
current = replacement_char;
}
return current;
}
from_utf16_iterator& operator++()
{
switch (state) {
case charpos::before: operator*(); operator++(); break;
case charpos::only: ++pos; break;
case charpos::first: ++pos; ++pos; break;
case charpos::second: ++pos; break;
case charpos::last: ++pos; break;
case charpos::after: break;
}
state = charpos::before;
current = 0;
return *this;
}
from_utf16_iterator operator++(int)
{
if (!current) {
// ensure current is valid in copy
operator*();
}
auto it = *this;
++*this;
return it;
}
from_utf16_iterator& operator--()
requires is_bidirectional
{
switch (state) {
case charpos::before: --pos; break;
case charpos::only: --pos; break;
case charpos::first: --pos; break;
case charpos::second: --pos; --pos; break;
case charpos::last: operator*(); operator--(); break;
case charpos::after: --pos; break;
}
state = charpos::last;
current = 0;
return *this;
}
from_utf16_iterator operator--(int)
requires is_bidirectional
{
auto const it = *this;
--*this;
return it;
}
};
template<std::input_iterator Input>
requires std::is_same_v<typename std::iterator_traits<Input>::value_type, char8_t>
class from_utf8_iterator
{
public:
using iterator_category = typeutil::first_baseclass_t<typename std::iterator_traits<Input>::iterator_category,
std::bidirectional_iterator_tag,
std::forward_iterator_tag,
std::input_iterator_tag>;
using difference_type = std::ptrdiff_t;
using value_type = char32_t;
using pointer = value_type const*;
using reference = value_type;
private:
static constexpr bool is_bidirectional = std::is_base_of_v<std::bidirectional_iterator_tag, iterator_category>;
Input pos = {}; // current read position
mutable value_type current = 0;
public:
from_utf8_iterator() = default;
explicit from_utf8_iterator(Input pos)
: pos{pos}
{}
bool operator==(const from_utf8_iterator&) const = default;
reference operator*() const
{
if (current) {
return current;
}
auto p = pos;
char32_t c = static_cast<char8_t>(*p);
auto count = 0u;
for (auto mask = 0x80u; mask & c; mask >>= 1) {
c &= ~mask;
++count;
}
if (count == 0) {
// ASCII
return current = c;
}
if (count == 1 || count > 6) {
return current = replacement_char;
}
// minimum code-point value for given length
constexpr char32_t minima[7]{ 0, 0, 0x80, 0x800, 0x1000, 0x20000, 0x4000000 };
char32_t const minval = minima[count];
while (--count > 0 && (*++p & 0xC0u) == 0x80u) {
c <<= 6;
c |= static_cast<char8_t>(*p) & 0x3F;
}
if (count > 0) {
// missing continuation
return current = replacement_char;
}
if (c < minval) {
// reject overlong encodings
return current = replacement_char;
}
if (0xD800 <= c && c <= 0xDFFF) {
// reject UTF-16 surrogates
return current = replacement_char;
}
if ((c & 0xFFFE) == 0xFFFE) {
// reject problematic non-characters
return current = replacement_char;
}
return current = c;
}
from_utf8_iterator& operator++()
{
auto count = 0;
for (auto mask = 0x80u; mask & *pos; mask >>= 1) {
++count;
}
if (count == 0 || count > 6) {
// ASCII or invalid - just advance by a single byte
count = 1;
}
while (count-- > 0 && (*++pos & 0xC0) == 0x80) {
; // skip continuation bytes (only)
}
current = 0;
return *this;
}
from_utf8_iterator operator++(int)
{
if (!current) {
// ensure current is valid in copy
operator*();
}
auto it = *this;
++*this;
return it;
}
from_utf8_iterator& operator--()
requires is_bidirectional
{
while ((*--pos & 0xC0) == 0x80) {
// just decrement some more
}
current = 0;
return *this;
}
from_utf8_iterator operator--(int)
requires is_bidirectional
{
if (!current) {
// ensure current is valid in copy
operator*();
}
auto const it = *this;
--*this;
return it;
}
};
// Views
template<template<typename> class T>
struct wrapped_iterator_view_factory
: std::ranges::range_adaptor_closure<wrapped_iterator_view_factory<T>>
{
template<std::ranges::input_range Range>
auto operator()(Range const &range) const
{
//using iterator = T<std::ranges::iterator_t<Range const>>;
return std::ranges::subrange{T{std::ranges::begin(range)},
T{std::ranges::end(range)}};
}
};
constexpr wrapped_iterator_view_factory<to_utf16_iterator> as_utf16;
constexpr wrapped_iterator_view_factory<to_utf8_iterator> as_utf8;
constexpr wrapped_iterator_view_factory<from_utf8_iterator> from_utf8;
// Functions
template<std::ranges::input_range R>
requires std::convertible_to<typename std::ranges::range_value_t<R>, char32_t>
and (!std::is_array_v<std::remove_reference_t<R>>)
auto to_u16string(R&& range) {
return range | as_utf16 | std::ranges::to<std::u16string>();
}
// For string literals, we don't want to include the trailing \0
template<std::size_t N>
auto to_u16string(const char32_t(&s)[N]) {
return to_u16string(std::u32string_view(s, N-1));
}
template<std::ranges::input_range R>
requires std::convertible_to<typename std::ranges::range_value_t<R>, char32_t>
and (!std::is_array_v<std::remove_reference_t<R>>)
auto to_u8string(R&& range) {
return range | as_utf8 | std::ranges::to<std::u8string>();
}
// For string literals, we don't want to include the trailing \0
template<std::size_t N>
auto to_u8string(const char32_t(&s)[N]) {
return to_u8string(std::u32string_view(s, N-1));
}
template<std::ranges::input_range R>
requires std::is_same_v<std::decay_t<typename std::ranges::range_value_t<R>>, char8_t>
and (!std::is_array_v<std::remove_reference_t<R>>)
auto to_u32string(R&& range) {
auto s = std::ranges::subrange(from_utf8_iterator{std::ranges::begin(range)},
from_utf8_iterator{std::ranges::end(range)});
return s | std::ranges::to<std::u32string>();
}
// For string literals, we don't want to include the trailing \0
template<std::size_t N>
auto to_u32string(const char8_t(&s)[N]) {
return to_u32string(std::u8string_view(s, N-1));
}
template<std::ranges::input_range R>
requires std::is_same_v<std::decay_t<typename std::ranges::range_value_t<R>>, char16_t>
and (not std::is_array_v<std::remove_reference_t<R>>)
auto to_u32string(R&& range) {
auto s = std::ranges::subrange(from_utf16_iterator{std::ranges::begin(range)},
from_utf16_iterator{std::ranges::end(range)});
return s | std::ranges::to<std::u32string>();
}
// For string literals, we don't want to include the trailing \0
template<std::size_t N>
auto to_u32string(const char16_t(&s)[N]) {
return to_u32string(std::u16string_view(s, N-1));
}
}
#ifdef USING_GTEST
#include <gtest/gtest.h>
#include <cstdint>
#include <sstream>
#include <string>
// To UTF-16
TEST(to_utf16_iterator, bmp)
{
auto *s = U"abà£";
codecvt::to_utf16_iterator i{s};
EXPECT_EQ(*i, u'a');
EXPECT_EQ(*i++, u'a');
EXPECT_EQ(*i++, u'b');
EXPECT_EQ(*i++, u'à');
EXPECT_EQ(*i++, u'£');
EXPECT_EQ(*i, u'\0');
}
TEST(to_utf16_iterator, supplementary)
{
auto *s = U"\U00024B62";
codecvt::to_utf16_iterator i{s};
EXPECT_EQ(*i, 0xD852);
EXPECT_EQ(*i++, 0xD852);
EXPECT_EQ(*i++, 0xDF62);
EXPECT_EQ(*i, u'\0');
// skip first surrogate
i = codecvt::to_utf16_iterator{s};
EXPECT_EQ(*++i, 0xDF62);
EXPECT_EQ(*++i, u'\0');
}
TEST(to_utf16_iterator, too_big)
{
char32_t s[]{ 0x110000, 0 };
codecvt::to_utf16_iterator i{s};
EXPECT_EQ(*i, 0xFFFD);
EXPECT_EQ(*i++, 0xFFFD);
EXPECT_EQ(*i, u'\0');
}
TEST(to_utf16_iterator, surrogates)
{
char32_t s[]{ 0xD800, 0xDFFF, 0 };
codecvt::to_utf16_iterator i{s};
EXPECT_EQ(*i++, 0xFFFD);
EXPECT_EQ(*i++, 0xFFFD);
EXPECT_EQ(*i, u'\0');
}
TEST(to_utf16_iterator, equality)
{
// iterator to the same UTF-16 unit should be equal whether or not
// it's dereferenced.
auto *s = U"a\U00024B62";
codecvt::to_utf16_iterator i0{s}; // advance only
auto i1 = i0; // advance and dereference
#define EXPECT_NEXT(ch) do { \
EXPECT_TRUE(i0 == i1); \
EXPECT_EQ(*i1, ch); \
EXPECT_TRUE(i0 == i1); \
++i0, ++i1; \
} while (0)
EXPECT_NEXT(u'a');
EXPECT_NEXT(0xD852);
EXPECT_NEXT(0xDF62);
EXPECT_NEXT(0);
}
TEST(to_utf16_iterator, decrement)
{
auto const& s = U"\U00024B62" "abà£";
{
auto i = codecvt::to_utf16_iterator{std::ranges::cend(s)};
EXPECT_EQ(*--i, u'\0');
EXPECT_EQ(*--i, u'£');
EXPECT_EQ(*--i, u'à');
EXPECT_EQ(*--i, u'b');
EXPECT_EQ(*--i, u'a');
EXPECT_EQ(*--i, 0xDF62);
EXPECT_EQ(*--i, 0xD852);
}
}
TEST(to_utf16_iterator, input_stream_preincrement)
{
auto s = std::basic_istringstream<char32_t>{U"\U00024B62" "abà£"};
{
auto i = codecvt::to_utf16_iterator{std::istreambuf_iterator{s}};
EXPECT_EQ(*i, 0xD852);
EXPECT_EQ(*++i, 0xDF62);
EXPECT_EQ(*++i, u'a');
EXPECT_EQ(*++i, u'b');
EXPECT_EQ(*++i, u'à');
EXPECT_EQ(*++i, u'£');
}
}
TEST(to_utf16_iterator, input_stream_postincrement)
{
auto s = std::basic_istringstream<char32_t>{U"\U00024B62" "abà£"};
{
auto i = codecvt::to_utf16_iterator{std::istreambuf_iterator{s}};
auto c = *i++;
EXPECT_EQ(c, 0xD852);
c = *i++;
EXPECT_EQ(c, 0xDF62);
EXPECT_EQ(*i++, u'a');
EXPECT_EQ(*i++, u'b');
EXPECT_EQ(*i++, u'à');
EXPECT_EQ(*i++, u'£');
}
}
// To UTF-8
TEST(to_utf8_iterator, ascii)
{
auto *s = U"ab";
codecvt::to_utf8_iterator i{s};
EXPECT_EQ(*i, u8'a');
EXPECT_EQ(*i, u8'a');
EXPECT_EQ(*++i, u8'b');
EXPECT_EQ(*++i, u8'\0');
}
TEST(to_utf8_iterator, latin1)
{
auto *s = U"©";
codecvt::to_utf8_iterator i{s};
EXPECT_EQ(*i, 0xC2);
EXPECT_EQ(*i, 0xC2);
++i;
EXPECT_EQ(*i, 0xA9);
EXPECT_EQ(*i, 0xA9);
++i;
EXPECT_EQ(*i, 0x00);
// prove it works even when we don't dereference
codecvt::to_utf8_iterator j{s};
++j;
EXPECT_EQ(*j, 0xA9);
EXPECT_EQ(codecvt::to_u8string(U"à£"), u8"à£");
}
TEST(to_utf8_iterator, general)
{
EXPECT_EQ(codecvt::to_u8string(U"你好 👋 ᜃᜓᜋᜓᜐ᜔ᜆ"), u8"你好 👋 ᜃᜓᜋᜓᜐ᜔ᜆ");
}
TEST(to_utf8_iterator, surrogates)
{
char32_t const s[]{ 0xD800, 0xDFFF, 0 };
EXPECT_EQ(codecvt::to_u8string(s), u8"\uFFFD\uFFFD");
}
TEST(to_utf8_iterator, equality)
{
auto *const s = U"a©👋";
codecvt::to_utf8_iterator i0{s}; // advance only
auto i1 = i0; // advance and dereference
#define EXPECT_NEXT(ch) do { \
EXPECT_TRUE(i0 == i1); \
EXPECT_EQ(*i1, ch); \
EXPECT_TRUE(i0 == i1); \
++i0, ++i1; \
} while (0)
EXPECT_NEXT(0x61);
EXPECT_NEXT(0xC2);
EXPECT_NEXT(0xA9);
EXPECT_NEXT(0xF0);
EXPECT_NEXT(0x9F);
EXPECT_NEXT(0x91);
EXPECT_NEXT(0x8B);
EXPECT_NEXT(0);
#undef EXPECT_NEXT
}
TEST(to_utf8_iterator, decrement)
{
auto const& s = U"a©👋";
{
auto i = codecvt::to_utf8_iterator{std::ranges::cend(s)};
EXPECT_EQ(*--i, u'\0');
EXPECT_EQ(*--i, 0x8B);
EXPECT_EQ(*--i, 0x91);
EXPECT_EQ(*--i, 0x9F);
EXPECT_EQ(*--i, 0xF0);
EXPECT_EQ(*--i, 0xA9);
EXPECT_EQ(*--i, 0xC2);
EXPECT_EQ(*--i, 0x61);
}
}
TEST(to_utf8_iterator, input_stream)
{
auto s = std::basic_istringstream<char32_t>{U"a©👋b"};
{
auto i = codecvt::to_utf8_iterator{std::istreambuf_iterator{s}};
EXPECT_EQ(*i++, u8'a');
EXPECT_EQ(*i++, 0xC2);
EXPECT_EQ(*i++, 0xA9);
EXPECT_EQ(*i++, 0xF0);
EXPECT_EQ(*i++, 0x9F);
EXPECT_EQ(*i++, 0x91);
EXPECT_EQ(*i++, 0x8B);
EXPECT_EQ(*i++, u8'b');
}
}
// From UTF-16
// helper for making invalid UTF-16 sequences from hex codes
static std::u16string u16string_from_hex(std::string in)
{
std::istringstream is{in};
std::u16string us;
us.reserve(in.length() / 3 + 1);
for (unsigned c; is >> std::hex >> c; ) {
us.push_back(static_cast<char16_t>(c));
}
return us;
}
TEST(from_utf16_iterator, ascii)
{
auto *s = u"ab";
codecvt::from_utf16_iterator i{s};
EXPECT_EQ(*i, U'a');
EXPECT_EQ(*i, U'a');
EXPECT_EQ(*++i, U'b');
EXPECT_EQ(*++i, U'\0');
}
TEST(from_utf16_iterator, latin1)
{
auto *s = u"à£";
codecvt::from_utf16_iterator i{s};
EXPECT_EQ(*i, U'à');
EXPECT_EQ(*i, U'à');
EXPECT_EQ(*++i, U'£');
EXPECT_EQ(*++i, U'\0');
// prove it works even when we don't dereference
i = codecvt::from_utf16_iterator{s};
++i;
EXPECT_EQ(*i, U'£');
}
TEST(from_utf16_iterator, greek)
{
auto *s = u"κόσμε";
codecvt::from_utf16_iterator i{s};
EXPECT_EQ(*i++, U'κ');
EXPECT_EQ(*i++, U'ό');
EXPECT_EQ(*i++, U'σ');
EXPECT_EQ(*i++, U'μ');
EXPECT_EQ(*i++, U'ε');
EXPECT_EQ(*i, U'\0');
}
TEST(from_utf16_iterator, emoji)
{
auto *s = u"👋";
codecvt::from_utf16_iterator i{s};
EXPECT_EQ(*i, U'👋');
EXPECT_EQ(*++i, U'\0');
}
TEST(from_utf16_iterator, general)
{
EXPECT_EQ(codecvt::to_u32string(u"你好 👋 ᜃᜓᜋᜓᜐ᜔ᜆ"), U"你好 👋 ᜃᜓᜋᜓᜐ᜔ᜆ");
}
TEST(from_utf16_iterator, decrement)
{
auto const& s = u"a©👋";
{
auto i = codecvt::from_utf16_iterator{std::ranges::cend(s)};
EXPECT_EQ(*--i, U'\0');
EXPECT_EQ(*--i, U'👋');
EXPECT_EQ(*--i, U'©');
EXPECT_EQ(*--i, U'a');
}
}
TEST(from_utf16_iterator, first_per_len)
{
// We don't use 0xffff because that should be rejected
char32_t s[] = { 0, 0x1000, 0 };
EXPECT_EQ(codecvt::to_u32string(codecvt::to_u16string(s)), std::u32string_view(s, std::size(s) - 1));
}
TEST(from_utf16_iterator, last_per_len)
{
// We don't use 0xffff because that should be rejected
char32_t s[] = { 0xfffc, 0x10FFFF, 0 };
EXPECT_EQ(codecvt::to_u32string(codecvt::to_u16string(s)), s);
}
TEST(from_utf16_iterator, unexpected_continuation)
{
EXPECT_EQ(codecvt::to_u32string(u16string_from_hex("dc00 dfff")), U"\uFFFD\uFFFD");
}
TEST(from_utf16_iterator, missing_continuation)
{
EXPECT_EQ(codecvt::to_u32string(u16string_from_hex("d800 20")), U"\uFFFD ");
EXPECT_EQ(codecvt::to_u32string(u16string_from_hex("dbff 20")), U"\uFFFD ");
}
TEST(from_utf16_iterator, input_stream)
{
auto s = std::basic_istringstream<char16_t>{u"aà£👋b"};
{
auto i = codecvt::from_utf16_iterator{std::istreambuf_iterator{s}};
EXPECT_EQ(*i++, U'a');
EXPECT_EQ(*i++, U'à');
EXPECT_EQ(*i++, U'£');
EXPECT_EQ(*i++, U'👋');
EXPECT_EQ(*i++, U'b');
}
}
// From UTF-8
// helper for making invalid UTF-8 sequences from hex codes
static std::u8string u8string_from_hex(std::string in)
{
std::istringstream is{in};
std::u8string us;
us.reserve(in.length() / 3 + 1);
for (unsigned c; is >> std::hex >> c; ) {
us.push_back(static_cast<char8_t>(c));
}
return us;
}
TEST(from_utf8_iterator, ascii)
{
auto *s = u8"ab";
codecvt::from_utf8_iterator i{s};
EXPECT_EQ(*i, U'a');
EXPECT_EQ(*i, U'a');
EXPECT_EQ(*++i, U'b');
EXPECT_EQ(*++i, U'\0');
}
TEST(from_utf8_iterator, latin1)
{
auto *s = u8"à£";
codecvt::from_utf8_iterator i{s};
EXPECT_EQ(*i, U'à');
EXPECT_EQ(*i, U'à');
EXPECT_EQ(*++i, U'£');
EXPECT_EQ(*++i, U'\0');
// prove it works even when we don't dereference