Fix width computation in float formatter

This commit is contained in:
Victor Zverovich 2021-01-15 11:07:55 -08:00
parent f8c2f8480a
commit 532e846b86
2 changed files with 4 additions and 2 deletions

View File

@ -1962,11 +1962,12 @@ OutputIt write_float(OutputIt out, const DecimalFP& fp,
fspecs.precision < num_zeros) {
num_zeros = fspecs.precision;
}
size += 2 + to_unsigned(num_zeros);
bool pointy = num_zeros != 0 || significand_size != 0 || fspecs.showpoint;
size += 1 + (pointy ? 1 : 0) + to_unsigned(num_zeros);
return write_padded<align::right>(out, specs, size, [&](iterator it) {
if (sign) *it++ = static_cast<Char>(data::signs[sign]);
*it++ = zero;
if (num_zeros == 0 && significand_size == 0 && !fspecs.showpoint) return it;
if (!pointy) return it;
*it++ = decimal_point;
it = detail::fill_n(it, num_zeros, zero);
return write_significand<Char>(it, significand, significand_size);

View File

@ -871,6 +871,7 @@ TEST(FormatterTest, Width) {
EXPECT_EQ(format("{:*^5}", "🤡"), "**🤡**");
EXPECT_EQ(format("{:#6}", 42.0), " 42.0");
EXPECT_EQ(format("{:6c}", static_cast<int>('x')), "x ");
EXPECT_EQ(format("{:>06.0f}", 0.00884311), "000000");
}
template <typename T> inline T const_check(T value) { return value; }