From b994a0ab136ddf67305626e3474ebd09661539c1 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 29 Nov 2019 06:17:29 -0800 Subject: [PATCH] Fix handling of missing fraction in snprintf_float --- include/fmt/format-inl.h | 8 ++++---- test/format-test.cc | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 6d45df7f..9c8e0816 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -1198,17 +1198,17 @@ int snprintf_float(T value, int precision, float_specs specs, exp = exp * 10 + (*p++ - '0'); } while (p != end); if (sign == '-') exp = -exp; + int fraction_size = 0; if (exp_pos != begin + 1) { // Remove trailing zeros. auto fraction_end = exp_pos - 1; while (*fraction_end == '0') --fraction_end; // Move the fractional part left to get rid of the decimal point. - int fraction_size = static_cast(fraction_end - begin - 1); + fraction_size = static_cast(fraction_end - begin - 1); std::memmove(begin + 1, begin + 2, fraction_size); - buf.resize(fraction_size + offset + 1); - exp -= fraction_size; } - return exp; + buf.resize(fraction_size + offset + 1); + return exp - fraction_size; } } } // namespace internal diff --git a/test/format-test.cc b/test/format-test.cc index e747a407..84c9d8a0 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1203,6 +1203,7 @@ TEST(FormatterTest, Precision) { EXPECT_EQ("1.2", format("{0:.2}", 1.2345)); EXPECT_EQ("1.2", format("{0:.2}", 1.2345l)); EXPECT_EQ("1.2e+56", format("{:.2}", 1.234e56)); + EXPECT_EQ("1e+00", format("{:.0e}", 1.0L)); EXPECT_EQ( "4.9406564584124654417656879286822137236505980261432476442558568250067550" "727020875186529983636163599237979656469544571773092665671035593979639877"