1#ifndef SIMDUTF_VALID_UTF8_TO_UTF16_H
2#define SIMDUTF_VALID_UTF8_TO_UTF16_H
9namespace utf8_to_utf16 {
11template <endianness big_endian,
typename InputPtr>
12#if SIMDUTF_CPLUSPLUS20
13 requires simdutf::detail::indexes_into_byte_like<InputPtr>
15simdutf_constexpr23
size_t convert_valid(InputPtr data,
size_t len,
16 char16_t *utf16_output) {
18 char16_t *start{utf16_output};
20#if SIMDUTF_CPLUSPLUS23
27 ::memcpy(&v, data + pos,
sizeof(uint64_t));
28 if ((v & 0x8080808080808080) == 0) {
29 size_t final_pos = pos + 8;
30 while (pos < final_pos) {
31 const char16_t byte = uint8_t(data[pos]);
33 !match_system(big_endian) ? u16_swap_bytes(
byte) : byte;
41 auto leading_byte = uint8_t(data[pos]);
42 if (leading_byte < 0b10000000) {
44 *utf16_output++ = !match_system(big_endian)
45 ? char16_t(u16_swap_bytes(leading_byte))
46 : char16_t(leading_byte);
48 }
else if ((leading_byte & 0b11100000) == 0b11000000) {
54 uint16_t code_point = uint16_t(((leading_byte & 0b00011111) << 6) |
55 (uint8_t(data[pos + 1]) & 0b00111111));
56 if constexpr (!match_system(big_endian)) {
57 code_point = u16_swap_bytes(uint16_t(code_point));
59 *utf16_output++ = char16_t(code_point);
61 }
else if ((leading_byte & 0b11110000) == 0b11100000) {
68 uint16_t(((leading_byte & 0b00001111) << 12) |
69 ((uint8_t(data[pos + 1]) & 0b00111111) << 6) |
70 (uint8_t(data[pos + 2]) & 0b00111111));
71 if constexpr (!match_system(big_endian)) {
72 code_point = u16_swap_bytes(uint16_t(code_point));
74 *utf16_output++ = char16_t(code_point);
76 }
else if ((leading_byte & 0b11111000) == 0b11110000) {
81 uint32_t code_point = ((leading_byte & 0b00000111) << 18) |
82 ((uint8_t(data[pos + 1]) & 0b00111111) << 12) |
83 ((uint8_t(data[pos + 2]) & 0b00111111) << 6) |
84 (uint8_t(data[pos + 3]) & 0b00111111);
85 code_point -= 0x10000;
86 uint16_t high_surrogate = uint16_t(0xD800 + (code_point >> 10));
87 uint16_t low_surrogate = uint16_t(0xDC00 + (code_point & 0x3FF));
88 if constexpr (!match_system(big_endian)) {
89 high_surrogate = u16_swap_bytes(high_surrogate);
90 low_surrogate = u16_swap_bytes(low_surrogate);
92 *utf16_output++ = char16_t(high_surrogate);
93 *utf16_output++ = char16_t(low_surrogate);
100 return utf16_output - start;
helpers placed in namespace detail are not a part of the public API