Skip to content

Avoid redundant work when processing UTF-8 strings #4475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 6 additions & 24 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -696,26 +696,6 @@ FMT_CONSTEXPR inline auto compute_width(string_view s) -> size_t {
return num_code_points;
}

template <typename Char>
inline auto code_point_index(basic_string_view<Char> s, size_t n) -> size_t {
return min_of(n, s.size());
}

// Calculates the index of the nth code point in a UTF-8 string.
inline auto code_point_index(string_view s, size_t n) -> size_t {
size_t result = s.size();
const char* begin = s.begin();
for_each_codepoint(s, [begin, &n, &result](uint32_t, string_view sv) {
if (n != 0) {
--n;
return true;
}
result = to_unsigned(sv.begin() - begin);
return false;
});
return result;
}

template <typename T> struct is_integral : std::is_integral<T> {};
template <> struct is_integral<int128_opt> : std::true_type {};
template <> struct is_integral<uint128_t> : std::true_type {};
Expand Down Expand Up @@ -2150,15 +2130,17 @@ FMT_CONSTEXPR FMT_INLINE auto write(OutputIt out, T value,
inline auto convert_precision_to_size(string_view s, size_t precision)
-> size_t {
size_t display_width = 0;
size_t num_code_points = 0;
size_t result = s.size();
for_each_codepoint(s, [&](uint32_t, string_view sv) {
display_width += compute_width(sv);
// Stop when display width exceeds precision.
if (display_width > precision) return false;
++num_code_points;
if (display_width > precision) {
result = to_unsigned(sv.begin() - s.begin());
return false;
}
return true;
});
return code_point_index(s, num_code_points);
return result;
}

template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
Expand Down
Loading