102 using from_char_type = from_encoder_type::char_type;
103 using to_char_type = to_encoder_type::char_type;
113 template<
typename OutRange,
typename InRange>
114 [[nodiscard]]
constexpr OutRange
convert(InRange&& src)
const noexcept
121 auto const[size, valid] = _size(cbegin(src), cend(src));
124 if constexpr (From == To and std::is_same_v<InRange, OutRange>) {
126 r = std::forward<InRange>(src);
137 if (From == To and valid) {
143 _convert(cbegin(src), cend(src),
begin(r));
155 template<
typename OutRange,
typename It,
typename EndIt>
156 [[nodiscard]]
constexpr OutRange
convert(It first, EndIt last)
const noexcept
160 auto const[size, valid] = _size(first, last);
167 if (From == To and valid) {
172 _convert(first, last,
begin(r));
185 template<
typename OutRange = std::basic_
string<to_
char_type>>
186 [[nodiscard]] OutRange
read(
void const *ptr,
size_t size, std::endian endian = std::endian::native)
noexcept
188 hi_assert_not_null(ptr);
190 auto const num_chars = size /
sizeof(from_char_type);
193 if (endian == std::endian::native) {
194 if (floor(ptr,
sizeof(from_char_type)) == ptr) {
195 return convert<OutRange>(
196 reinterpret_cast<from_char_type
const *
>(ptr),
reinterpret_cast<from_char_type
const *
>(ptr) + num_chars);
201 return convert<OutRange>(
std::move(tmp));
207 for (
auto& c : tmp) {
208 c = std::byteswap(c);
210 return convert<OutRange>(
std::move(tmp));
219 template<
typename InRange>
222 return convert<to_string_type>(std::forward<InRange>(src));
226#if defined(HI_HAS_SSE2)
227 using chunk16_type = __m128i;
229 using chunk16_type = void;
232 constexpr static bool _has_read_ascii_chunk16 =
true;
233 constexpr static bool _has_write_ascii_chunk16 =
true;
235 template<
typename It,
typename EndIt>
236 constexpr void _size_ascii(It& it, EndIt last,
size_t& count)
const noexcept
238 if (not std::is_constant_evaluated()) {
239#if defined(HI_HAS_SSE2)
240 if constexpr (_has_read_ascii_chunk16 and _has_write_ascii_chunk16) {
242 auto const chunk = from_encoder_type{}.read_ascii_chunk16(it);
243 auto const ascii_mask = _mm_movemask_epi8(chunk);
246 auto partial_count = std::countr_zero(truncate<uint16_t>(ascii_mask));
248 count += partial_count;
259 template<
typename SrcIt,
typename SrcEndIt,
typename DstIt>
260 void _convert_ascii(SrcIt& src, SrcEndIt src_last, DstIt& dst)
const noexcept
262 if (not std::is_constant_evaluated()) {
263#if defined(HI_HAS_SSE2)
264 if constexpr (_has_read_ascii_chunk16 and _has_write_ascii_chunk16) {
266 auto const chunk = from_encoder_type{}.read_ascii_chunk16(src);
267 auto const ascii_mask = _mm_movemask_epi8(chunk);
273 to_encoder_type{}.write_ascii_chunk16(chunk, dst);
282 template<
typename It,
typename EndIt>
290 _size_ascii(it, last, count);
296 auto const[code_point, read_valid] = from_encoder_type{}.read(it, last);
299 auto const[write_count, write_valid] = to_encoder_type{}.size(code_point);
300 count += write_count;
301 valid &= write_valid;
304 return {
count, valid};
307 template<
typename SrcIt,
typename SrcEndIt,
typename DstIt>
308 void _convert(SrcIt src, SrcEndIt src_last, DstIt dst)
const noexcept
313 _convert_ascii(src, src_last, dst);
315 if (src == src_last) {
319 auto const[code_point, from_valid] = from_encoder_type{}.read(src, src_last);
320 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:156
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:186