Right-align inf and nan with a bare width (#4895)

fmt::format("{:6}", nan) gave "nan   " where std::format and printf("%6f")
give "   nan". write_nonfinite was the only numeric write path taking
write_padded's align::left default, so a width with no explicit align
left-aligned inf and nan while every finite value right-aligned. That
default only reaches align::none, leaving explicit <, > and ^ and the 0
flag unchanged.
This commit is contained in:
Advit Arora 2026-08-27 21:24:40 +05:30 committed by GitHub
parent e27cc20bd9
commit e76a9520a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 6 deletions

View File

@ -2402,12 +2402,11 @@ FMT_CONSTEXPR20 auto write_nonfinite(OutputIt out, bool isnan,
const bool is_zero_fill =
specs.fill_size() == 1 && specs.fill_unit<Char>() == '0';
if (is_zero_fill) specs.set_fill(' ');
return write_padded<Char>(out, specs, size,
[=](reserve_iterator<OutputIt> it) {
if (s != sign::none)
*it++ = detail::getsign<Char>(s);
return copy<Char>(str, str + str_size, it);
});
return write_padded<Char, align::right>(
out, specs, size, [=](reserve_iterator<OutputIt> it) {
if (s != sign::none) *it++ = detail::getsign<Char>(s);
return copy<Char>(str, str + str_size, it);
});
}
// A decimal floating-point number significand * pow(10, exp).

View File

@ -1622,6 +1622,7 @@ TEST(format_test, format_nan) {
EXPECT_EQ(fmt::format("{:<7}", nan), "nan ");
EXPECT_EQ(fmt::format("{:^7}", nan), " nan ");
EXPECT_EQ(fmt::format("{:>7}", nan), " nan");
EXPECT_EQ(fmt::format("{:7}", nan), " nan");
}
TEST(format_test, format_infinity) {
@ -1639,6 +1640,7 @@ TEST(format_test, format_infinity) {
EXPECT_EQ(fmt::format("{:<7}", inf), "inf ");
EXPECT_EQ(fmt::format("{:^7}", inf), " inf ");
EXPECT_EQ(fmt::format("{:>7}", inf), " inf");
EXPECT_EQ(fmt::format("{:7}", inf), " inf");
}
TEST(format_test, format_long_double) {