Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore zero-padding for non-finite floating points #2310

Merged
merged 5 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest copying specs and changing fill from 0 to (space) instead.

}

// A decimal floating-point number significand * pow(10, exp).
Expand Down
8 changes: 6 additions & 2 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be " -nan" i.e. the width should still apply. Same elsewhere.

} else
fmt::print("Warning: compiler doesn't handle negative NaN correctly");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: please wrap the else statement in {} for consistency with if:

} else {
  fmt::print("Warning: compiler doesn't handle negative NaN correctly");
}

EXPECT_EQ(" nan", fmt::format("{: }", nan));
EXPECT_EQ("NAN", fmt::format("{:F}", nan));
Expand All @@ -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));
Expand Down