Skip to content

Commit 8aa1d6a

Browse files
committed
Minor cleanup
1 parent 6d79757 commit 8aa1d6a

File tree

2 files changed

+14
-33
lines changed

2 files changed

+14
-33
lines changed

include/fmt/format.h

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,51 +2109,33 @@ FMT_CONSTEXPR FMT_INLINE auto write(OutputIt out, T value,
21092109
return write_int<Char>(out, make_write_int_arg(value, specs.sign()), specs);
21102110
}
21112111

2112-
FMT_INLINE auto count_code_points_with_display_width_precision(
2113-
string_view s, size_t display_width_precision) -> size_t {
2112+
inline auto convert_precision_to_size(string_view s, size_t precision)
2113+
-> size_t {
21142114
size_t display_width = 0;
2115-
size_t code_points = 0;
2116-
2117-
// Iterate through the string to compute display width
2115+
size_t num_code_points = 0;
21182116
for_each_codepoint(s, [&](uint32_t, string_view sv) {
2119-
// Compute the display width of the current code point
2120-
size_t cp_width = compute_width(sv);
2121-
if (display_width + cp_width > display_width_precision) {
2122-
return false; // Stop iteration when display width exceeds precision
2123-
}
2124-
2125-
display_width += cp_width;
2126-
code_points++;
2117+
display_width += compute_width(sv);
2118+
// Stop when display width exceeds precision.
2119+
if (display_width > precision) return false;
2120+
++num_code_points;
21272121
return true;
21282122
});
2129-
2130-
return code_points;
2123+
return code_point_index(s, num_code_points);
21312124
}
21322125

2133-
template <typename Char>
2134-
FMT_CONSTEXPR auto handle_precision(
2135-
basic_string_view<Char> s, const format_specs& specs,
2136-
FMT_ENABLE_IF(std::is_same<Char, char>::value)) -> size_t {
2137-
auto code_points = count_code_points_with_display_width_precision(
2138-
s, to_unsigned(specs.precision));
2139-
return code_point_index(s, code_points);
2140-
}
2141-
2142-
template <typename Char>
2143-
FMT_CONSTEXPR auto handle_precision(
2144-
basic_string_view<Char> s, const format_specs&,
2145-
FMT_ENABLE_IF(!std::is_same<Char, char>::value)) -> size_t {
2146-
return code_point_index(s, s.size());
2126+
template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
2127+
auto convert_precision_to_size(basic_string_view<Char>, size_t precision)
2128+
-> size_t {
2129+
return precision;
21472130
}
21482131

21492132
template <typename Char, typename OutputIt>
21502133
FMT_CONSTEXPR auto write(OutputIt out, basic_string_view<Char> s,
21512134
const format_specs& specs) -> OutputIt {
21522135
auto data = s.data();
21532136
auto size = s.size();
2154-
if (specs.precision >= 0 && to_unsigned(specs.precision) < size) {
2155-
size = handle_precision(s, specs);
2156-
}
2137+
if (specs.precision >= 0 && to_unsigned(specs.precision) < size)
2138+
size = convert_precision_to_size(s, to_unsigned(specs.precision));
21572139

21582140
bool is_debug = specs.type() == presentation_type::debug;
21592141
if (is_debug) {

test/format-test.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// For the license information refer to format.h.
77

88
// Check if fmt/format.h compiles with windows.h included before it.
9-
#include <gtest/gtest.h>
109
#ifdef _WIN32
1110
# include <windows.h>
1211
#endif

0 commit comments

Comments
 (0)