From 8aa1d6a9fbf0dd8b44b400e79c74c6aec9e1c1d9 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 25 May 2025 10:03:00 -0700 Subject: [PATCH] Minor cleanup --- include/fmt/format.h | 46 ++++++++++++++------------------------------ test/format-test.cc | 1 - 2 files changed, 14 insertions(+), 33 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 58c7cd79..685474f0 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2109,41 +2109,24 @@ FMT_CONSTEXPR FMT_INLINE auto write(OutputIt out, T value, return write_int(out, make_write_int_arg(value, specs.sign()), specs); } -FMT_INLINE auto count_code_points_with_display_width_precision( - string_view s, size_t display_width_precision) -> size_t { +inline auto convert_precision_to_size(string_view s, size_t precision) + -> size_t { size_t display_width = 0; - size_t code_points = 0; - - // Iterate through the string to compute display width + size_t num_code_points = 0; for_each_codepoint(s, [&](uint32_t, string_view sv) { - // Compute the display width of the current code point - size_t cp_width = compute_width(sv); - if (display_width + cp_width > display_width_precision) { - return false; // Stop iteration when display width exceeds precision - } - - display_width += cp_width; - code_points++; + display_width += compute_width(sv); + // Stop when display width exceeds precision. + if (display_width > precision) return false; + ++num_code_points; return true; }); - - return code_points; + return code_point_index(s, num_code_points); } -template -FMT_CONSTEXPR auto handle_precision( - basic_string_view s, const format_specs& specs, - FMT_ENABLE_IF(std::is_same::value)) -> size_t { - auto code_points = count_code_points_with_display_width_precision( - s, to_unsigned(specs.precision)); - return code_point_index(s, code_points); -} - -template -FMT_CONSTEXPR auto handle_precision( - basic_string_view s, const format_specs&, - FMT_ENABLE_IF(!std::is_same::value)) -> size_t { - return code_point_index(s, s.size()); +template ::value)> +auto convert_precision_to_size(basic_string_view, size_t precision) + -> size_t { + return precision; } template @@ -2151,9 +2134,8 @@ FMT_CONSTEXPR auto write(OutputIt out, basic_string_view s, const format_specs& specs) -> OutputIt { auto data = s.data(); auto size = s.size(); - if (specs.precision >= 0 && to_unsigned(specs.precision) < size) { - size = handle_precision(s, specs); - } + if (specs.precision >= 0 && to_unsigned(specs.precision) < size) + size = convert_precision_to_size(s, to_unsigned(specs.precision)); bool is_debug = specs.type() == presentation_type::debug; if (is_debug) { diff --git a/test/format-test.cc b/test/format-test.cc index 73f81c1d..e630ab0f 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -6,7 +6,6 @@ // For the license information refer to format.h. // Check if fmt/format.h compiles with windows.h included before it. -#include #ifdef _WIN32 # include #endif