1#ifndef SIMDUTF_VALID_UTF32_TO_UTF8_H
2#define SIMDUTF_VALID_UTF32_TO_UTF8_H
7namespace utf32_to_utf8 {
9template <
typename InputPtr,
typename OutputPtr>
10#if SIMDUTF_CPLUSPLUS20
11 requires(simdutf::detail::indexes_into_utf32<InputPtr> &&
12 simdutf::detail::index_assignable_from_char<OutputPtr>)
14simdutf_constexpr23
size_t convert_valid(InputPtr data,
size_t len,
15 OutputPtr utf8_output) {
17 auto start = utf8_output;
19#if SIMDUTF_CPLUSPLUS23
26 ::memcpy(&v, data + pos,
sizeof(uint64_t));
27 if ((v & 0xFFFFFF80FFFFFF80) == 0) {
28 *utf8_output++ = char(data[pos]);
29 *utf8_output++ = char(data[pos + 1]);
36 uint32_t word = data[pos];
37 if ((word & 0xFFFFFF80) == 0) {
39 *utf8_output++ = char(word);
41 }
else if ((word & 0xFFFFF800) == 0) {
44 *utf8_output++ = char((word >> 6) | 0b11000000);
45 *utf8_output++ = char((word & 0b111111) | 0b10000000);
47 }
else if ((word & 0xFFFF0000) == 0) {
50 *utf8_output++ = char((word >> 12) | 0b11100000);
51 *utf8_output++ = char(((word >> 6) & 0b111111) | 0b10000000);
52 *utf8_output++ = char((word & 0b111111) | 0b10000000);
57 *utf8_output++ = char((word >> 18) | 0b11110000);
58 *utf8_output++ = char(((word >> 12) & 0b111111) | 0b10000000);
59 *utf8_output++ = char(((word >> 6) & 0b111111) | 0b10000000);
60 *utf8_output++ = char((word & 0b111111) | 0b10000000);
64 return utf8_output - start;