98 using from_char_type = from_encoder_type::char_type;
99 using to_char_type = to_encoder_type::char_type;
109 template<
typename OutRange,
typename InRange>
110 [[nodiscard]]
constexpr OutRange
convert(InRange&& src)
const noexcept
117 hilet[size, valid] = _size(cbegin(src), cend(src));
120 if constexpr (From == To and std::is_same_v<InRange, OutRange>) {
122 r = std::forward<InRange>(src);
133 if (From == To and valid) {
139 _convert(cbegin(src), cend(src), begin(r));
151 template<
typename OutRange,
typename It,
typename EndIt>
152 [[nodiscard]]
constexpr OutRange
convert(It first, EndIt last)
const noexcept
156 hilet[size, valid] = _size(first, last);
163 if (From == To and valid) {
168 _convert(first, last, begin(r));
181 template<
typename OutRange = std::basic_
string<to_
char_type>>
182 [[nodiscard]] OutRange
read(
void const *ptr,
size_t size, std::endian endian = std::endian::native)
noexcept
184 hi_axiom(ptr !=
nullptr);
186 hilet num_chars = size /
sizeof(from_char_type);
189 if (endian == std::endian::native) {
190 if (floor(ptr,
sizeof(from_char_type)) == ptr) {
191 return convert<OutRange>(
192 reinterpret_cast<from_char_type
const *
>(ptr),
reinterpret_cast<from_char_type
const *
>(ptr) + num_chars);
197 return convert<OutRange>(
std::move(tmp));
203 for (
auto& c : tmp) {
206 return convert<OutRange>(
std::move(tmp));
215 template<
typename InRange>
218 return convert<to_string_type>(std::forward<InRange>(src));
222#if defined(HI_HAS_SSE2)
223 using chunk16_type = __m128i;
225 using chunk16_type = void;
228 constexpr static bool _has_read_ascii_chunk16 =
true;
229 constexpr static bool _has_write_ascii_chunk16 =
true;
231 template<
typename It,
typename EndIt>
232 [[nodiscard]]
constexpr void _size_ascii(It& it, EndIt last,
size_t& count)
const noexcept
234 if (not std::is_constant_evaluated()) {
235#if defined(HI_HAS_SSE2)
236 if constexpr (_has_read_ascii_chunk16 and _has_write_ascii_chunk16) {
238 hilet chunk = from_encoder_type{}.read_ascii_chunk16(it);
239 hilet ascii_mask = _mm_movemask_epi8(chunk);
242 auto partial_count = std::countr_zero(truncate<uint16_t>(ascii_mask));
244 count += partial_count;
255 template<
typename SrcIt,
typename SrcEndIt,
typename DstIt>
256 void _convert_ascii(SrcIt& src, SrcEndIt src_last, DstIt& dst)
const noexcept
258 if (not std::is_constant_evaluated()) {
259#if defined(HI_HAS_SSE2)
260 if constexpr (_has_read_ascii_chunk16 and _has_write_ascii_chunk16) {
262 hilet chunk = from_encoder_type{}.read_ascii_chunk16(src);
263 hilet ascii_mask = _mm_movemask_epi8(chunk);
269 to_encoder_type{}.write_ascii_chunk16(chunk, dst);
278 template<
typename It,
typename EndIt>
286 _size_ascii(it, last, count);
292 hilet[code_point, read_valid] = from_encoder_type{}.read(it, last);
295 hilet[write_count, write_valid] = to_encoder_type{}.size(code_point);
296 count += write_count;
297 valid &= write_valid;
300 return {
count, valid};
303 template<
typename SrcIt,
typename SrcEndIt,
typename DstIt>
304 void _convert(SrcIt src, SrcEndIt src_last, DstIt dst)
const noexcept
309 _convert_ascii(src, src_last, dst);
311 if (src == src_last) {
315 hilet[code_point, from_valid] = from_encoder_type{}.read(src, src_last);
316 to_encoder_type{}.write(code_point, dst);
constexpr OutRange convert(It first, EndIt last) const noexcept
Convert text between the given encodings.
Definition char_converter.hpp:152
OutRange read(void const *ptr, size_t size, std::endian endian=std::endian::native) noexcept
Read text from a byte array.
Definition char_converter.hpp:182