-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR.
test/format-test.cc
Outdated
EXPECT_EQ("-nan", fmt::format("{}", -nan)); | ||
else | ||
EXPECT_EQ("-nan", fmt::format("{:+06}", -nan)); |
There was a problem hiding this comment.
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.
include/fmt/format.h
Outdated
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); |
There was a problem hiding this comment.
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.
test/format-test.cc
Outdated
// '0'-fill option sets alignment to numeric overwriting any user-provided | ||
// alignment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the desired behavior? Currently we lose the originally provided alignment because it seems to get replaced by align::numeric
when using 0
-padding.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, we should preserve the original alignment. I think it can be dome by changing
Line 1950 in ca46637
specs_.align = align::numeric; |
to something like
if (specs_.align == align::none) specs_.align = align::numeric;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, that works!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two nits, otherwise LGTM.
include/fmt/format.h
Outdated
const float_specs& fspecs) { | ||
auto str = | ||
isinf ? (fspecs.upper ? "INF" : "inf") : (fspecs.upper ? "NAN" : "nan"); | ||
constexpr size_t str_size = 3; | ||
auto sign = fspecs.sign; | ||
auto size = str_size + (sign ? 1 : 0); | ||
// replace '0'-padding with space for non-finite values |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: please make this a proper sentence:
// Replace '0'-padding with space for non-finite values.
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"); |
There was a problem hiding this comment.
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");
}
Thank you! |
Fixes #2305.
Is there a better way to check for the
0
-padding-option than what I wrote?