From 5750f9d0f4ce33a82850bfba6a8365b5186199ee Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 7 Nov 2026 07:55:27 -0800 Subject: [PATCH] Reuse size_padding for localized integers and simplify the test Both integer writers now compute zero padding the same way. --- include/fmt/format.h | 56 ++++++++++++++++++++------------------------ test/format-test.cc | 6 ----- 2 files changed, 25 insertions(+), 37 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 1bf3bebe..339e154e 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2207,6 +2207,27 @@ FMT_CONSTEXPR auto write_int_chr(OutputIt out, UInt abs_value, bool negative, return write_char(out, static_cast(abs_value), specs); } +// Size and padding computation separate from write_int to avoid template bloat. +struct size_padding { + unsigned size; + unsigned padding; + + FMT_CONSTEXPR size_padding(int num_digits, unsigned prefix, + const format_specs& specs) + : size((prefix >> 24) + to_unsigned(num_digits)), padding(0) { + if (specs.align() == align::numeric) { + auto width = to_unsigned(specs.width); + if (width > size) { + padding = width - size; + size = width; + } + } else if (specs.precision > num_digits) { + size = (prefix >> 24) + to_unsigned(specs.precision); + padding = to_unsigned(specs.precision - num_digits); + } + } +}; + // Writes a decimal integer with digit grouping. template auto write_int(OutputIt out, UInt value, unsigned prefix, @@ -2246,19 +2267,13 @@ auto write_int(OutputIt out, UInt value, unsigned prefix, return write_int_chr(out, value, (prefix & 0xff) == '-', specs); } - unsigned size = (prefix != 0 ? prefix >> 24 : 0) + to_unsigned(num_digits) + - to_unsigned(grouping.count_separators(num_digits)); - unsigned padding = 0; - if (specs.align() == align::numeric) { - auto width = to_unsigned(specs.width); - if (width > size) padding = width - size; - size += padding; - } + auto sp = size_padding(num_digits + grouping.count_separators(num_digits), + prefix, specs); return write_padded( - out, specs, size, size, [&](reserve_iterator it) { + out, specs, sp.size, [&](reserve_iterator it) { for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8) *it++ = static_cast(p & 0xff); - it = detail::fill_n(it, padding, static_cast('0')); + it = detail::fill_n(it, sp.padding, static_cast('0')); return grouping.apply(it, string_view(buffer.data(), buffer.size())); }); } @@ -2317,27 +2332,6 @@ template struct loc_writer { } }; -// Size and padding computation separate from write_int to avoid template bloat. -struct size_padding { - unsigned size; - unsigned padding; - - FMT_CONSTEXPR size_padding(int num_digits, unsigned prefix, - const format_specs& specs) - : size((prefix >> 24) + to_unsigned(num_digits)), padding(0) { - if (specs.align() == align::numeric) { - auto width = to_unsigned(specs.width); - if (width > size) { - padding = width - size; - size = width; - } - } else if (specs.precision > num_digits) { - size = (prefix >> 24) + to_unsigned(specs.precision); - padding = to_unsigned(specs.precision - num_digits); - } - } -}; - template FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, write_int_arg arg, const format_specs& specs) -> OutputIt { diff --git a/test/format-test.cc b/test/format-test.cc index 6a403967..134d6382 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -2694,17 +2694,11 @@ TEST(format_test, format_locale) { TEST(format_test, format_int_locale_zero_padding) { auto loc = std::locale::classic(); EXPECT_EQ(fmt::format(loc, "{:08L}", -12), "-0000012"); - EXPECT_EQ(fmt::format(loc, "{:+08L}", 12), "+0000012"); - EXPECT_EQ(fmt::format(loc, "{: 08L}", 12), " 0000012"); - EXPECT_EQ(fmt::format(loc, "{:08L}", 12), "00000012"); EXPECT_EQ(fmt::format(loc, "{:02L}", -12), "-12"); - EXPECT_EQ(fmt::format(loc, "{:>08L}", -12), " -12"); EXPECT_EQ(fmt::format(loc, "{:0>8L}", -12), "00000-12"); loc = std::locale(loc, new fmt::format_facet(",")); EXPECT_EQ(fmt::format(loc, "{:08L}", -1234), "-001,234"); EXPECT_EQ(fmt::format(loc, "{:#010Lx}", -0x1234), "-0x001,234"); - EXPECT_EQ(fmt::format(loc, "{:#010Lb}", -10), "-0b001,010"); - EXPECT_EQ(fmt::format(loc, "{:#08Lo}", -01234), "-001,234"); } #endif // FMT_USE_LOCALE