keep width for non-finite formatting with 0-padding
This commit is contained in:
parent
5951dc2ee4
commit
49b77edfc3
@ -1584,22 +1584,21 @@ FMT_CONSTEXPR OutputIt write(OutputIt out, const Char* s,
|
|||||||
|
|
||||||
template <typename Char, typename OutputIt>
|
template <typename Char, typename OutputIt>
|
||||||
OutputIt write_nonfinite(OutputIt out, bool isinf,
|
OutputIt write_nonfinite(OutputIt out, bool isinf,
|
||||||
const basic_format_specs<Char>& specs,
|
basic_format_specs<Char> specs,
|
||||||
const float_specs& fspecs) {
|
const float_specs& fspecs) {
|
||||||
auto str =
|
auto str =
|
||||||
isinf ? (fspecs.upper ? "INF" : "inf") : (fspecs.upper ? "NAN" : "nan");
|
isinf ? (fspecs.upper ? "INF" : "inf") : (fspecs.upper ? "NAN" : "nan");
|
||||||
constexpr size_t str_size = 3;
|
constexpr size_t str_size = 3;
|
||||||
auto sign = fspecs.sign;
|
auto sign = fspecs.sign;
|
||||||
auto size = str_size + (sign ? 1 : 0);
|
auto size = str_size + (sign ? 1 : 0);
|
||||||
auto copy_it = [=](reserve_iterator<OutputIt> it) {
|
// replace '0'-padding with space for non-finite values
|
||||||
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 =
|
const bool is_zero_fill =
|
||||||
specs.fill.size() == 1 && *specs.fill.data() == static_cast<Char>('0');
|
specs.fill.size() == 1 && *specs.fill.data() == static_cast<Char>('0');
|
||||||
return is_zero_fill ? base_iterator(out, copy_it(reserve(out, size)))
|
if (is_zero_fill) specs.fill[0] = static_cast<Char>(' ');
|
||||||
: write_padded<align::right>(out, specs, size, copy_it);
|
return write_padded(out, specs, size, [=](reserve_iterator<OutputIt> it) {
|
||||||
|
if (sign) *it++ = static_cast<Char>(data::signs[sign]);
|
||||||
|
return copy_str<Char>(str, str + str_size, it);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// A decimal floating-point number significand * pow(10, exp).
|
// A decimal floating-point number significand * pow(10, exp).
|
||||||
|
|||||||
@ -1272,10 +1272,15 @@ TEST(format_test, format_nan) {
|
|||||||
double nan = std::numeric_limits<double>::quiet_NaN();
|
double nan = std::numeric_limits<double>::quiet_NaN();
|
||||||
EXPECT_EQ("nan", fmt::format("{}", nan));
|
EXPECT_EQ("nan", fmt::format("{}", nan));
|
||||||
EXPECT_EQ("+nan", fmt::format("{:+}", nan));
|
EXPECT_EQ("+nan", fmt::format("{:+}", nan));
|
||||||
EXPECT_EQ("+nan", fmt::format("{:+06}", nan));
|
EXPECT_EQ(" +nan", fmt::format("{:+06}", nan));
|
||||||
|
// '0'-fill option sets alignment to numeric overwriting any user-provided
|
||||||
|
// alignment
|
||||||
|
EXPECT_EQ(" +nan", fmt::format("{:^+06}", nan));
|
||||||
|
EXPECT_EQ(" +nan", fmt::format("{:<+06}", nan));
|
||||||
|
EXPECT_EQ(" +nan", fmt::format("{:>+06}", nan));
|
||||||
if (std::signbit(-nan)) {
|
if (std::signbit(-nan)) {
|
||||||
EXPECT_EQ("-nan", fmt::format("{}", -nan));
|
EXPECT_EQ("-nan", fmt::format("{}", -nan));
|
||||||
EXPECT_EQ("-nan", fmt::format("{:+06}", -nan));
|
EXPECT_EQ(" -nan", fmt::format("{:+06}", -nan));
|
||||||
} else
|
} else
|
||||||
fmt::print("Warning: compiler doesn't handle negative NaN correctly");
|
fmt::print("Warning: compiler doesn't handle negative NaN correctly");
|
||||||
EXPECT_EQ(" nan", fmt::format("{: }", nan));
|
EXPECT_EQ(" nan", fmt::format("{: }", nan));
|
||||||
@ -1290,8 +1295,13 @@ 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("{:+}", 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("{:+06}", -inf));
|
EXPECT_EQ(" -inf", fmt::format("{:+06}", -inf));
|
||||||
|
// '0'-fill option sets alignment to numeric overwriting any user-provided
|
||||||
|
// alignment
|
||||||
|
EXPECT_EQ(" +inf", fmt::format("{:^+06}", 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("{: }", inf));
|
||||||
EXPECT_EQ("INF", fmt::format("{:F}", inf));
|
EXPECT_EQ("INF", fmt::format("{:F}", inf));
|
||||||
EXPECT_EQ("inf ", fmt::format("{:<7}", inf));
|
EXPECT_EQ("inf ", fmt::format("{:<7}", inf));
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user