From 29db6bd91f9aa59a1a2c8eb1615837532c5118e8 Mon Sep 17 00:00:00 2001 From: gawain Date: Fri, 1 Mar 2019 20:32:05 +0100 Subject: [PATCH 1/4] Add support for '%' type to output floating point values as a percentage. This helps with compatibility with Python's format strings. --- include/fmt/format-inl.h | 9 ++++++++- include/fmt/format.h | 24 +++++++++++++++++++++--- test/format-test.cc | 2 +- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 8f2fe1c92c18..7ca73d12fb9f 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -656,7 +656,14 @@ void sprintf_format(Double value, internal::buffer& buf, *format_ptr++ = '*'; } if (std::is_same::value) *format_ptr++ = 'L'; - char type = spec.type ? spec.type : 'g'; + + char type = spec.type; + + if (type == '%') { + type = 'f'; + } else if (type == 0) { + type = 'g'; + } #if FMT_MSC_VER if (type == 'F') { // MSVC's printf doesn't support 'F'. diff --git a/include/fmt/format.h b/include/fmt/format.h index 44584c1094ab..c4e5958d6ec0 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1274,6 +1274,9 @@ FMT_CONSTEXPR void handle_float_type_spec(char spec, Handler&& handler) { case 'F': handler.on_fixed(); break; + case '%': + handler.on_percent(); + break; case 'a': case 'A': handler.on_hex(); @@ -1338,6 +1341,7 @@ class float_type_checker : private ErrorHandler { FMT_CONSTEXPR void on_general() {} FMT_CONSTEXPR void on_exp() {} FMT_CONSTEXPR void on_fixed() {} + FMT_CONSTEXPR void on_percent() {} FMT_CONSTEXPR void on_hex() {} FMT_CONSTEXPR void on_error() { @@ -2808,10 +2812,11 @@ template class basic_writer { struct float_spec_handler { char type; - bool upper; - bool fixed; + bool upper{false}; + bool fixed{false}; + bool as_percentage{false}; - explicit float_spec_handler(char t) : type(t), upper(false), fixed(false) {} + explicit float_spec_handler(char t) : type(t) {} void on_general() { if (type == 'G') upper = true; @@ -2826,6 +2831,11 @@ struct float_spec_handler { if (type == 'F') upper = true; } + void on_percent() { + fixed = true; + as_percentage = true; + } + void on_hex() { if (type == 'A') upper = true; } @@ -2869,11 +2879,19 @@ void basic_writer::write_double(T value, const format_specs& spec) { memory_buffer buffer; int exp = 0; int precision = spec.has_precision() || !spec.type ? spec.precision : 6; + + if (handler.as_percentage) value *= 100.; + bool use_grisu = fmt::internal::use_grisu() && (!spec.type || handler.fixed) && internal::grisu2_format(static_cast(value), buffer, precision, handler.fixed, exp); if (!use_grisu) internal::sprintf_format(value, buffer, spec); + + if (handler.as_percentage) { + buffer.push_back('%'); + --exp; // Adjust decimal place position. + } align_spec as = spec; if (spec.align() == ALIGN_NUMERIC) { if (sign) { diff --git a/test/format-test.cc b/test/format-test.cc index 822557b7f827..45c3432ed027 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1443,7 +1443,7 @@ TEST(FormatterTest, FormatFloat) { } TEST(FormatterTest, FormatDouble) { - check_unknown_types(1.2, "eEfFgGaA", "double"); + check_unknown_types(1.2, "eEfFgGaA%", "double"); EXPECT_EQ("0.0", format("{:}", 0.0)); EXPECT_EQ("0.000000", format("{:f}", 0.0)); EXPECT_EQ("0", format("{:g}", 0.0)); From 73f31a8bcc6b41dda70156ce3d8987f6f639b55c Mon Sep 17 00:00:00 2001 From: gawain Date: Sun, 3 Mar 2019 20:47:37 +0100 Subject: [PATCH 2/4] Updates for formatting output as a percentage. * Added percentage sign if needed when dumping nan and infinity. * Added unit tests. * Used clang-format for changed source files. --- include/fmt/format-inl.h | 7 +++---- include/fmt/format.h | 30 +++++++++++++++++++----------- test/format-test.cc | 9 +++++++++ 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 7ca73d12fb9f..61e4790a5b9b 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -539,8 +539,7 @@ struct fixed_stop { void on_exp(int exp) { if (!fixed) return; exp += exp10; - if (exp >= 0) - precision += exp; + if (exp >= 0) precision += exp; } bool operator()(char*, int& size, uint64_t remainder, uint64_t divisor, @@ -660,9 +659,9 @@ void sprintf_format(Double value, internal::buffer& buf, char type = spec.type; if (type == '%') { - type = 'f'; + type = 'f'; } else if (type == 0) { - type = 'g'; + type = 'g'; } #if FMT_MSC_VER if (type == 'F') { diff --git a/include/fmt/format.h b/include/fmt/format.h index c4e5958d6ec0..3b9f097d8aee 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2646,10 +2646,12 @@ template class basic_writer { struct inf_or_nan_writer { char sign; + bool as_percentage; const char* str; size_t size() const { - return static_cast(INF_SIZE + (sign ? 1 : 0)); + return static_cast(INF_SIZE + (sign ? 1 : 0) + + (as_percentage ? 1 : 0)); } size_t width() const { return size(); } @@ -2657,6 +2659,7 @@ template class basic_writer { if (sign) *it++ = static_cast(sign); it = internal::copy_str( str, str + static_cast(INF_SIZE), it); + if (as_percentage) *it++ = static_cast('%'); } }; @@ -2832,8 +2835,8 @@ struct float_spec_handler { } void on_percent() { - fixed = true; - as_percentage = true; + fixed = true; + as_percentage = true; } void on_hex() { @@ -2864,17 +2867,22 @@ void basic_writer::write_double(T value, const format_specs& spec) { basic_writer& writer; format_specs spec; char sign; + bool as_percentage; void operator()(const char* str) const { - writer.write_padded(spec, inf_or_nan_writer{sign, str}); + writer.write_padded(spec, inf_or_nan_writer{sign, as_percentage, str}); } - } write_inf_or_nan = {*this, spec, sign}; + } write_inf_or_nan = {*this, spec, sign, handler.as_percentage}; // Format NaN and ininity ourselves because sprintf's output is not consistent // across platforms. - if (internal::fputil::isnotanumber(value)) - return write_inf_or_nan(handler.upper ? "NAN" : "nan"); - if (internal::fputil::isinfinity(value)) - return write_inf_or_nan(handler.upper ? "INF" : "inf"); + if (internal::fputil::isnotanumber(value)) { + write_inf_or_nan(handler.upper ? "NAN" : "nan"); + return; + } + if (internal::fputil::isinfinity(value)) { + write_inf_or_nan(handler.upper ? "INF" : "inf"); + return; + } memory_buffer buffer; int exp = 0; @@ -2889,8 +2897,8 @@ void basic_writer::write_double(T value, const format_specs& spec) { if (!use_grisu) internal::sprintf_format(value, buffer, spec); if (handler.as_percentage) { - buffer.push_back('%'); - --exp; // Adjust decimal place position. + buffer.push_back('%'); + --exp; // Adjust decimal place position. } align_spec as = spec; if (spec.align() == ALIGN_NUMERIC) { diff --git a/test/format-test.cc b/test/format-test.cc index 45c3432ed027..51ce763b0840 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1207,6 +1207,8 @@ TEST(FormatterTest, Precision) { "precision not allowed for this argument type"); EXPECT_THROW_MSG(format("{0:.2f}", 42ull), format_error, "precision not allowed for this argument type"); + EXPECT_THROW_MSG(format("{0:.2%}", 42), format_error, + "precision not allowed for this argument type"); EXPECT_THROW_MSG(format("{0:3.0}", 'x'), format_error, "precision not allowed for this argument type"); EXPECT_EQ("1.2", format("{0:.2}", 1.2345)); @@ -1440,6 +1442,7 @@ TEST(FormatterTest, FormatConvertibleToLongLong) { TEST(FormatterTest, FormatFloat) { EXPECT_EQ("392.500000", format("{0:f}", 392.5f)); + EXPECT_EQ("12.500000%", format("{0:%}", 0.125f)); } TEST(FormatterTest, FormatDouble) { @@ -1452,6 +1455,8 @@ TEST(FormatterTest, FormatDouble) { EXPECT_EQ("392.65", format("{:G}", 392.65)); EXPECT_EQ("392.650000", format("{:f}", 392.65)); EXPECT_EQ("392.650000", format("{:F}", 392.65)); + EXPECT_EQ("12.500000%", format("{:%}", 0.125)); + EXPECT_EQ("12.34%", format("{:.2%}", 0.1234432)); char buffer[BUFFER_SIZE]; safe_sprintf(buffer, "%e", 392.65); EXPECT_EQ(buffer, format("{0:e}", 392.65)); @@ -1473,6 +1478,7 @@ TEST(FormatterTest, FormatNaN) { EXPECT_EQ("nan ", format("{:<7}", nan)); EXPECT_EQ(" nan ", format("{:^7}", nan)); EXPECT_EQ(" nan", format("{:>7}", nan)); + EXPECT_EQ("nan%", format("{:%}", nan)); } TEST(FormatterTest, FormatInfinity) { @@ -1485,6 +1491,7 @@ TEST(FormatterTest, FormatInfinity) { EXPECT_EQ("inf ", format("{:<7}", inf)); EXPECT_EQ(" inf ", format("{:^7}", inf)); EXPECT_EQ(" inf", format("{:>7}", inf)); + EXPECT_EQ("inf%", format("{:%}", inf)); } TEST(FormatterTest, FormatLongDouble) { @@ -1495,6 +1502,8 @@ TEST(FormatterTest, FormatLongDouble) { EXPECT_EQ("392.65", format("{0:G}", 392.65l)); EXPECT_EQ("392.650000", format("{0:f}", 392.65l)); EXPECT_EQ("392.650000", format("{0:F}", 392.65l)); + EXPECT_EQ("12.500000%", format("{:%}", 0.125l)); + EXPECT_EQ("12.34%", format("{:.2%}", 0.1234432l)); char buffer[BUFFER_SIZE]; safe_sprintf(buffer, "%Le", 392.65l); EXPECT_EQ(buffer, format("{0:e}", 392.65l)); From d6d33a99c6e73a6c936ef649e336661b1a963d3a Mon Sep 17 00:00:00 2001 From: gawain Date: Sun, 3 Mar 2019 21:09:50 +0100 Subject: [PATCH 3/4] Changes to fix CI as g++-4.4 BUILD was broken. --- include/fmt/format.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 3b9f097d8aee..0af93ba64342 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2815,11 +2815,11 @@ template class basic_writer { struct float_spec_handler { char type; - bool upper{false}; - bool fixed{false}; - bool as_percentage{false}; + bool upper; + bool fixed; + bool as_percentage; - explicit float_spec_handler(char t) : type(t) {} + explicit float_spec_handler(char t) : type(t), upper(false), fixed(false), as_percentage(false) {} void on_general() { if (type == 'G') upper = true; From 141d1cf93df5bd3234566c96f2d578296a2fb1c7 Mon Sep 17 00:00:00 2001 From: gawain Date: Mon, 4 Mar 2019 22:26:41 +0100 Subject: [PATCH 4/4] Revert unnecessary changes. --- include/fmt/format.h | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 0af93ba64342..81fcfb392299 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2875,14 +2875,10 @@ void basic_writer::write_double(T value, const format_specs& spec) { // Format NaN and ininity ourselves because sprintf's output is not consistent // across platforms. - if (internal::fputil::isnotanumber(value)) { - write_inf_or_nan(handler.upper ? "NAN" : "nan"); - return; - } - if (internal::fputil::isinfinity(value)) { - write_inf_or_nan(handler.upper ? "INF" : "inf"); - return; - } + if (internal::fputil::isnotanumber(value)) + return write_inf_or_nan(handler.upper ? "NAN" : "nan"); + if (internal::fputil::isinfinity(value)) + return write_inf_or_nan(handler.upper ? "INF" : "inf"); memory_buffer buffer; int exp = 0;