blob: ded9ff81823b515472d6f2e8992b859242e00021 (
plain)
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
|
#ifndef SIMDUTF_UTF32_TO_UTF16_H
#define SIMDUTF_UTF32_TO_UTF16_H
namespace simdutf {
namespace scalar {
namespace {
namespace utf32_to_utf16 {
template <endianness big_endian>
inline size_t convert(const char32_t *buf, size_t len, char16_t *utf16_output) {
const uint32_t *data = reinterpret_cast<const uint32_t *>(buf);
size_t pos = 0;
char16_t *start{utf16_output};
while (pos < len) {
uint32_t word = data[pos];
if ((word & 0xFFFF0000) == 0) {
if (word >= 0xD800 && word <= 0xDFFF) {
return 0;
}
// will not generate a surrogate pair
*utf16_output++ = !match_system(big_endian)
? char16_t(utf16::swap_bytes(uint16_t(word)))
: char16_t(word);
} else {
// will generate a surrogate pair
if (word > 0x10FFFF) {
return 0;
}
word -= 0x10000;
uint16_t high_surrogate = uint16_t(0xD800 + (word >> 10));
uint16_t low_surrogate = uint16_t(0xDC00 + (word & 0x3FF));
if (!match_system(big_endian)) {
high_surrogate = utf16::swap_bytes(high_surrogate);
low_surrogate = utf16::swap_bytes(low_surrogate);
}
*utf16_output++ = char16_t(high_surrogate);
*utf16_output++ = char16_t(low_surrogate);
}
pos++;
}
return utf16_output - start;
}
template <endianness big_endian>
inline result convert_with_errors(const char32_t *buf, size_t len,
char16_t *utf16_output) {
const uint32_t *data = reinterpret_cast<const uint32_t *>(buf);
size_t pos = 0;
char16_t *start{utf16_output};
while (pos < len) {
uint32_t word = data[pos];
if ((word & 0xFFFF0000) == 0) {
if (word >= 0xD800 && word <= 0xDFFF) {
return result(error_code::SURROGATE, pos);
}
// will not generate a surrogate pair
*utf16_output++ = !match_system(big_endian)
? char16_t(utf16::swap_bytes(uint16_t(word)))
: char16_t(word);
} else {
// will generate a surrogate pair
if (word > 0x10FFFF) {
return result(error_code::TOO_LARGE, pos);
}
word -= 0x10000;
uint16_t high_surrogate = uint16_t(0xD800 + (word >> 10));
uint16_t low_surrogate = uint16_t(0xDC00 + (word & 0x3FF));
if (!match_system(big_endian)) {
high_surrogate = utf16::swap_bytes(high_surrogate);
low_surrogate = utf16::swap_bytes(low_surrogate);
}
*utf16_output++ = char16_t(high_surrogate);
*utf16_output++ = char16_t(low_surrogate);
}
pos++;
}
return result(error_code::SUCCESS, utf16_output - start);
}
} // namespace utf32_to_utf16
} // unnamed namespace
} // namespace scalar
} // namespace simdutf
#endif
|