From 1b1c70108a1a88a4f4d259643a97aef954506ab4 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 18 Dec 2019 12:12:09 -0800 Subject: [PATCH] trailing_zeros -> showpoint --- include/fmt/format-inl.h | 2 +- include/fmt/format.h | 18 +++++++++--------- test/format-test.cc | 1 + 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 29888931..458ff336 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -1106,7 +1106,7 @@ int snprintf_float(T value, int precision, float_specs specs, char format[max_format_size]; char* format_ptr = format; *format_ptr++ = '%'; - if (specs.trailing_zeros) *format_ptr++ = '#'; + if (specs.showpoint) *format_ptr++ = '#'; if (precision >= 0) { *format_ptr++ = '.'; *format_ptr++ = '*'; diff --git a/include/fmt/format.h b/include/fmt/format.h index 5daa238a..4b23aa67 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1035,7 +1035,7 @@ struct float_specs { bool percent : 1; bool binary32 : 1; bool use_grisu : 1; - bool trailing_zeros : 1; + bool showpoint : 1; }; // Writes the exponent exp in the form "[+-]d{2,3}" to buffer. @@ -1076,7 +1076,7 @@ template class float_writer { // Insert a decimal point after the first digit and add an exponent. *it++ = static_cast(*digits_); int num_zeros = specs_.precision - num_digits_; - bool trailing_zeros = num_zeros > 0 && specs_.trailing_zeros; + bool trailing_zeros = num_zeros > 0 && specs_.showpoint; if (num_digits_ > 1 || trailing_zeros) *it++ = decimal_point_; it = copy_str(digits_ + 1, digits_ + num_digits_, it); if (trailing_zeros) @@ -1088,7 +1088,7 @@ template class float_writer { // 1234e7 -> 12340000000[.0+] it = copy_str(digits_, digits_ + num_digits_, it); it = std::fill_n(it, full_exp - num_digits_, static_cast('0')); - if (specs_.trailing_zeros) { + if (specs_.showpoint) { *it++ = decimal_point_; int num_zeros = specs_.precision - full_exp; if (num_zeros <= 0) { @@ -1105,7 +1105,7 @@ template class float_writer { } else if (full_exp > 0) { // 1234e-2 -> 12.34[0+] it = copy_str(digits_, digits_ + full_exp, it); - if (!specs_.trailing_zeros) { + if (!specs_.showpoint) { // Remove trailing zeros. int num_digits = num_digits_; while (num_digits > full_exp && digits_[num_digits - 1] == '0') @@ -1127,7 +1127,7 @@ template class float_writer { if (specs_.precision >= 0 && specs_.precision < num_zeros) num_zeros = specs_.precision; int num_digits = num_digits_; - if (!specs_.trailing_zeros) + if (!specs_.showpoint) while (num_digits > 0 && digits_[num_digits - 1] == '0') --num_digits; if (num_zeros != 0 || num_digits != 0) { *it++ = decimal_point_; @@ -1206,11 +1206,11 @@ template FMT_CONSTEXPR float_specs parse_float_type_spec( const basic_format_specs& specs, ErrorHandler&& eh = {}) { auto result = float_specs(); - result.trailing_zeros = specs.alt; + result.showpoint = specs.alt; switch (specs.type) { case 0: result.format = float_format::general; - result.trailing_zeros |= specs.precision != 0; + result.showpoint |= specs.precision != 0; break; case 'G': result.upper = true; @@ -1223,14 +1223,14 @@ FMT_CONSTEXPR float_specs parse_float_type_spec( FMT_FALLTHROUGH; case 'e': result.format = float_format::exp; - result.trailing_zeros |= specs.precision != 0; + result.showpoint |= specs.precision != 0; break; case 'F': result.upper = true; FMT_FALLTHROUGH; case 'f': result.format = float_format::fixed; - result.trailing_zeros |= specs.precision != 0; + result.showpoint |= specs.precision != 0; break; #if FMT_DEPRECATED_PERCENT case '%': diff --git a/test/format-test.cc b/test/format-test.cc index a2d254f6..67fe48e3 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -924,6 +924,7 @@ TEST(FormatterTest, HashFlag) { EXPECT_EQ("-42.0", format("{0:#}", -42.0)); EXPECT_EQ("-42.0", format("{0:#}", -42.0l)); + EXPECT_EQ("4.e+01", format("{:#.0e}", 42.0)); EXPECT_THROW_MSG(format("{0:#", 'c'), format_error, "missing '}' in format string"); EXPECT_THROW_MSG(format("{0:#}", 'c'), format_error,