simdutf 8.0.0
Unicode at GB/s.
Loading...
Searching...
No Matches
valid_utf32_to_utf16.h
1#ifndef SIMDUTF_VALID_UTF32_TO_UTF16_H
2#define SIMDUTF_VALID_UTF32_TO_UTF16_H
3
4namespace simdutf {
5namespace scalar {
6namespace {
7namespace utf32_to_utf16 {
8
9template <endianness big_endian>
10simdutf_constexpr23 size_t convert_valid(const char32_t *data, size_t len,
11 char16_t *utf16_output) {
12 size_t pos = 0;
13 char16_t *start{utf16_output};
14 while (pos < len) {
15 uint32_t word = data[pos];
16 if ((word & 0xFFFF0000) == 0) {
17 // will not generate a surrogate pair
18 *utf16_output++ = !match_system(big_endian)
19 ? char16_t(u16_swap_bytes(uint16_t(word)))
20 : char16_t(word);
21 pos++;
22 } else {
23 // will generate a surrogate pair
24 word -= 0x10000;
25 uint16_t high_surrogate = uint16_t(0xD800 + (word >> 10));
26 uint16_t low_surrogate = uint16_t(0xDC00 + (word & 0x3FF));
27 if simdutf_constexpr (!match_system(big_endian)) {
28 high_surrogate = u16_swap_bytes(high_surrogate);
29 low_surrogate = u16_swap_bytes(low_surrogate);
30 }
31 *utf16_output++ = char16_t(high_surrogate);
32 *utf16_output++ = char16_t(low_surrogate);
33 pos++;
34 }
35 }
36 return utf16_output - start;
37}
38
39} // namespace utf32_to_utf16
40} // unnamed namespace
41} // namespace scalar
42} // namespace simdutf
43
44#endif