Reuse size_padding for localized integers and simplify the test

Both integer writers now compute zero padding the same way.
This commit is contained in:
Victor Zverovich 2026-11-07 07:55:27 -08:00
parent 080b1d0219
commit 5750f9d0f4
2 changed files with 25 additions and 37 deletions

View File

@ -2207,6 +2207,27 @@ FMT_CONSTEXPR auto write_int_chr(OutputIt out, UInt abs_value, bool negative,
return write_char<Char>(out, static_cast<Char>(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 <typename OutputIt, typename UInt, typename Char>
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<Char>(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<Char, align::right>(
out, specs, size, size, [&](reserve_iterator<OutputIt> it) {
out, specs, sp.size, [&](reserve_iterator<OutputIt> it) {
for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8)
*it++ = static_cast<Char>(p & 0xff);
it = detail::fill_n(it, padding, static_cast<Char>('0'));
it = detail::fill_n(it, sp.padding, static_cast<Char>('0'));
return grouping.apply(it, string_view(buffer.data(), buffer.size()));
});
}
@ -2317,27 +2332,6 @@ template <typename Char = char> 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 <typename Char, typename OutputIt, typename T>
FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, write_int_arg<T> arg,
const format_specs& specs) -> OutputIt {

View File

@ -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<std::locale>(","));
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