From 09a606d17a2c5c2a7a295f873f67d626264d2181 Mon Sep 17 00:00:00 2001 From: Nathan Chandrasekar <322284620+vera-rubin@users.noreply.github.com> Date: Sat, 31 Oct 2026 13:15:44 -0400 Subject: [PATCH] Fix zero padding for localized integers (#4994) --- include/fmt/format.h | 7 +++++++ test/format-test.cc | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/fmt/format.h b/include/fmt/format.h index a2fc9621..f74ad7a9 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2248,10 +2248,17 @@ auto write_int(OutputIt out, UInt value, unsigned prefix, 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; + } return write_padded( out, specs, size, 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')); return grouping.apply(it, string_view(buffer.data(), buffer.size())); }); } diff --git a/test/format-test.cc b/test/format-test.cc index 34b0dfe1..c44db6ab 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -2686,6 +2686,22 @@ TEST(format_test, format_locale) { EXPECT_EQ(fmt::format(loc, "{:10Lo}", 12345), " 30,071"); } +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 struct convertible_to_nonconst_cstring {