20 using char_type = char16_t;
22 [[nodiscard]] std::endian guess_endian(
void const *ptr,
size_t size, std::endian endian)
const noexcept
24 hi_axiom(ptr !=
nullptr);
25 auto *ptr_ =
reinterpret_cast<uint8_t
const *
>(ptr);
28 return std::endian::native;
31 if (ptr_[0] == 0xfe and ptr_[1] == 0xff) {
32 return std::endian::big;
33 }
else if (ptr_[0] == 0xff and ptr_[1] == 0xfe) {
34 return std::endian::little;
39 for (
auto i = 0; i != size; ++i) {
40 count[i % 2] = ptr_[i] == 0 ? count[i % 2] + 1 : 0;
41 if (count[i % 2] >= 8) {
42 return i % 2 == 0 ? std::endian::big : std::endian::little;
50 template<
typename It,
typename EndIt>
55 if (
auto cu = *it++; cu < 0xd800) {
56 return {char_cast<char32_t>(cu),
true};
58 }
else if (cu < 0xdc00) {
61 return {0xfffd,
false};
64 auto cp = char_cast<char32_t>(cu & 0x03ff);
66 if (cu >= 0xdc00 and cu < 0xe000) {
75 return {0xfffd,
false};
79 }
else if (cu < 0xe000) {
81 return {0xfffd,
false};
90 hi_axiom(code_point < 0x11'0000);
91 hi_axiom(not(code_point >= 0xd800 and code_point < 0xe000));
92 return {truncate<uint8_t>((code_point >= 0x01'0000) + 1),
true};
96 constexpr void write(
char32_t code_point, It& dst)
const noexcept
98 hi_axiom(code_point <= 0x10'ffff);
99 hi_axiom(not(code_point >= 0xd800 and code_point < 0xe000));
101 if (
auto tmp = truncate<int32_t>(code_point) - 0x1'0000; tmp >= 0) {
102 *dst++ = char_cast<char16_t>((tmp >> 10) + 0xd800);
103 *dst++ = char_cast<char16_t>((tmp & 0x3ff) + 0xdc00);
106 *dst++ = char_cast<char16_t>(code_point);
110#if defined(HI_HAS_SSE2)
111 template<
typename It>
112 hi_force_inline __m128i read_ascii_chunk16(It it)
const noexcept
115 auto lo = _mm_loadu_si128(
reinterpret_cast<__m128i
const *
>(
std::addressof(*it)));
117 auto hi = _mm_loadu_si128(
reinterpret_cast<__m128i
const *
>(
std::addressof(*it)));
125 auto sign_lo = _mm_srai_epi16(lo, 15);
126 auto sign_hi = _mm_srai_epi16(
hi, 15);
127 auto sign = _mm_packs_epi16(sign_lo, sign_hi);
132 auto chunk = _mm_packus_epi16(lo,
hi);
137 return _mm_or_si128(chunk, sign);
140 template<
typename It>
141 hi_force_inline
void write_ascii_chunk16(__m128i chunk, It dst)
const noexcept
143 auto zero = _mm_setzero_si128();
144 auto lo = _mm_unpacklo_epi8(chunk, zero);
145 auto hi = _mm_unpackhi_epi8(chunk, zero);
147 _mm_storeu_si128(
reinterpret_cast<__m128i *
>(
std::addressof(*dst)), lo);