27 using char_type = char;
28 using fallback_encoder_type =
char_map<
"cp-1252">;
29 using fallback_char_type = fallback_encoder_type::char_type;
31 [[nodiscard]]
constexpr std::endian guess_endian(
void const *ptr,
size_t size, std::endian endian)
const noexcept
33 return std::endian::native;
38 hilet str = std::string_view(&cu, 1_uz);
39 auto first = str.begin();
40 hilet[code_point, valid] = fallback_encoder_type{}.read(first, str.end());
41 return {code_point,
false};
44 template<
typename It,
typename EndIt>
47 if (it == last or (first_cu & 0xc0) == 0x80) {
50 return read_fallback(char_cast<char>(first_cu));
53 hilet length = narrow_cast<uint8_t>(std::countl_one(char_cast<uint8_t>(first_cu)));
60 auto cp = char_cast<char32_t>(cu);
63 cu = char_cast<char8_t>(*it);
67 if ((cu & 0xc0) != 0x80) {
70 return read_fallback(char_cast<char>(first_cu));
77 for (uint8_t actual_length = 2; actual_length != length; ++actual_length) {
80 return {0xfffd,
false};
83 cu = char_cast<char8_t>(*it);
84 if ((cu & 0b11'000000) != 0b10'000000) {
86 return {0xfffd,
false};
93 cp |= cu & 0b00'111111;
98 valid &= cp < 0x11'0000;
100 valid &= cp < 0xd800 or cp >= 0xe000;
102 valid &= length == narrow_cast<uint8_t>((cp > 0x7f) + (cp > 0x7ff) + (cp > 0xffff) + 1);
104 return {0xfffd,
false};
110 template<
typename It,
typename EndIt>
115 auto cu = char_cast<char8_t>(*it);
117 if (not to_bool(cu & 0x80)) [[likely]] {
119 return {char_cast<char32_t>(cu),
true};
122 return read(it, last, cu);
129 hi_axiom(not(code_point >= 0xd800 and code_point < 0xe000));
131 return {narrow_cast<uint8_t>((code_point > 0x7f) + (code_point > 0x7ff) + (code_point > 0xffff) + 1),
true};
134 template<
typename It>
135 constexpr void write(
char32_t code_point, It& dst)
const noexcept
138 hi_axiom(not(code_point >= 0xd800 and code_point < 0xe000));
140 hilet num_cu = truncate<uint8_t>((code_point > 0x7f) + (code_point > 0x7ff) + (code_point > 0xffff));
142 auto leading_ones = char_cast<int8_t>(uint8_t{0x80});
143 leading_ones >>= num_cu;
148 auto shift = num_cu * 6;
150 auto cu = truncate<uint8_t>(code_point >> shift);
151 cu |= truncate<uint8_t>(leading_ones);
162 cu = truncate<uint8_t>(code_point >> shift);
174#if defined(HI_HAS_SSE2)
175 template<
typename It>
176 hi_force_inline __m128i read_ascii_chunk16(It it)
const noexcept
178 return _mm_loadu_si128(
reinterpret_cast<__m128i
const *
>(
std::addressof(*it)));
181 template<
typename It>
182 hi_force_inline
void write_ascii_chunk16(__m128i chunk, It dst)
const noexcept
184 _mm_storeu_si128(
reinterpret_cast<__m128i *
>(
std::addressof(*dst)), chunk);
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253