MCPcopy Create free account
hub / github.com/fmtlib/fmt / for_each_codepoint

Function for_each_codepoint

include/fmt/format.h:621–653  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

619// corresponding to the code point. cp is invalid_code_point on error.
620template <typename F>
621FMT_CONSTEXPR void for_each_codepoint(string_view s, F f) {
622 auto decode = [f](const char* buf_ptr, const char* ptr) {
623 auto cp = uint32_t();
624 auto error = 0;
625 auto end = utf8_decode(buf_ptr, &cp, &error);
626 bool result = f(error ? invalid_code_point : cp,
627 string_view(ptr, error ? 1 : to_unsigned(end - buf_ptr)));
628 return result ? (error ? buf_ptr + 1 : end) : nullptr;
629 };
630
631 auto p = s.data();
632 const size_t block_size = 4; // utf8_decode always reads blocks of 4 chars.
633 if (s.size() >= block_size) {
634 for (auto end = p + s.size() - block_size + 1; p < end;) {
635 p = decode(p, p);
636 if (!p) return;
637 }
638 }
639 auto num_chars_left = to_unsigned(s.data() + s.size() - p);
640 if (num_chars_left == 0) return;
641
642 // Suppress bogus -Wstringop-overflow.
643 if (FMT_GCC_VERSION) num_chars_left &= 3;
644 char buf[2 * block_size - 1] = {};
645 copy<char>(p, p + num_chars_left, buf);
646 const char* buf_ptr = buf;
647 do {
648 auto end = decode(buf_ptr, p);
649 if (!end) return;
650 p += end - buf_ptr;
651 buf_ptr = end;
652 } while (buf_ptr < buf + num_chars_left);
653}
654
655FMT_CONSTEXPR inline auto display_width_of(uint32_t cp) noexcept -> size_t {
656 return to_unsigned(

Callers 3

utf8_to_utf16Method · 0.85
find_escapeFunction · 0.85
writeMethod · 0.85

Calls 4

utf8_decodeFunction · 0.85
to_unsignedFunction · 0.85
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected