Ignore zero-padding for non-finite floating points

This commit is contained in:
Matthias Liedtke 2021-05-23 18:25:22 +02:00
parent bc13c6de39
commit 5951dc2ee4
2 changed files with 13 additions and 4 deletions

View File

@ -1591,10 +1591,15 @@ OutputIt write_nonfinite(OutputIt out, bool isinf,
constexpr size_t str_size = 3;
auto sign = fspecs.sign;
auto size = str_size + (sign ? 1 : 0);
return write_padded(out, specs, size, [=](reserve_iterator<OutputIt> it) {
auto copy_it = [=](reserve_iterator<OutputIt> it) {
if (sign) *it++ = static_cast<Char>(data::signs[sign]);
return copy_str<Char>(str, str + str_size, it);
});
};
// no '0'-padding applied for non-finite values
const bool is_zero_fill =
specs.fill.size() == 1 && *specs.fill.data() == static_cast<Char>('0');
return is_zero_fill ? base_iterator(out, copy_it(reserve(out, size)))
: write_padded<align::right>(out, specs, size, copy_it);
}
// A decimal floating-point number significand * pow(10, exp).

View File

@ -1272,9 +1272,11 @@ TEST(format_test, format_nan) {
double nan = std::numeric_limits<double>::quiet_NaN();
EXPECT_EQ("nan", fmt::format("{}", nan));
EXPECT_EQ("+nan", fmt::format("{:+}", nan));
if (std::signbit(-nan))
EXPECT_EQ("+nan", fmt::format("{:+06}", nan));
if (std::signbit(-nan)) {
EXPECT_EQ("-nan", fmt::format("{}", -nan));
else
EXPECT_EQ("-nan", fmt::format("{:+06}", -nan));
} else
fmt::print("Warning: compiler doesn't handle negative NaN correctly");
EXPECT_EQ(" nan", fmt::format("{: }", nan));
EXPECT_EQ("NAN", fmt::format("{:F}", nan));
@ -1288,6 +1290,8 @@ TEST(format_test, format_infinity) {
EXPECT_EQ("inf", fmt::format("{}", inf));
EXPECT_EQ("+inf", fmt::format("{:+}", inf));
EXPECT_EQ("-inf", fmt::format("{}", -inf));
EXPECT_EQ("+inf", fmt::format("{:+06}", inf));
EXPECT_EQ("-inf", fmt::format("{:+06}", -inf));
EXPECT_EQ(" inf", fmt::format("{: }", inf));
EXPECT_EQ("INF", fmt::format("{:F}", inf));
EXPECT_EQ("inf ", fmt::format("{:<7}", inf));