From 03776dd988e5793056c398decffc0443c9b90cf5 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 10 Jun 2014 07:03:49 -0700 Subject: [PATCH] Add support for hexadecimal floating point format specifiers a and A. --- format.cc | 16 +++++----------- test/format-test.cc | 4 +++- test/printf-test.cc | 5 ++--- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/format.cc b/format.cc index 77fd1fd5..b7138985 100644 --- a/format.cc +++ b/format.cc @@ -393,7 +393,7 @@ void fmt::BasicWriter::FormatDouble( case 0: type = 'g'; break; - case 'e': case 'f': case 'g': + case 'e': case 'f': case 'g': case 'a': break; case 'F': #ifdef _MSC_VER @@ -401,7 +401,7 @@ void fmt::BasicWriter::FormatDouble( type = 'f'; #endif // Fall through. - case 'E': case 'G': + case 'E': case 'G': case 'A': upper = true; break; default: @@ -707,17 +707,11 @@ void fmt::BasicWriter::PrintfParser::Format( spec.width_ = 0; ParseFlags(spec, s, *arg); - /* - if (*s == '#') { - if (arg.type > LAST_NUMERIC_TYPE) - ReportError(s, "format specifier '#' requires numeric argument"); - spec.flags_ |= HASH_FLAG; - ++s; - }*/ - // Parse width and zero flag. - if (*s < '0' || *s > '9') + if (*s < '0' || *s > '9') { + // TODO: parse '*' width break; + } spec.width_ = internal::ParseNonnegativeInt(s, error); } // Fall through. diff --git a/test/format-test.cc b/test/format-test.cc index 6a9db1a0..7d4b12e0 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1201,7 +1201,7 @@ TEST(FormatterTest, FormatFloat) { } TEST(FormatterTest, FormatDouble) { - CheckUnknownTypes(1.2, "eEfFgG", "double"); + CheckUnknownTypes(1.2, "eEfFgGaA", "double"); EXPECT_EQ("0", str(Format("{0:}") << 0.0)); EXPECT_EQ("0.000000", str(Format("{0:f}") << 0.0)); EXPECT_EQ("392.65", str(Format("{0:}") << 392.65)); @@ -1215,6 +1215,8 @@ TEST(FormatterTest, FormatDouble) { SPrintf(buffer, "%E", 392.65); EXPECT_EQ(buffer, str(Format("{0:E}") << 392.65)); EXPECT_EQ("+0000392.6", str(Format("{0:+010.4g}") << 392.65)); + EXPECT_EQ("-0x1.5p+5", str(Format("{:a}") << -42.0)); + EXPECT_EQ("-0X1.5P+5", str(Format("{:A}") << -42.0)); } TEST(FormatterTest, FormatNaN) { diff --git a/test/printf-test.cc b/test/printf-test.cc index d6a3f3ab..35878e39 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -210,9 +210,8 @@ TEST(PrintfTest, HashFlag) { EXPECT_PRINTF("-42.0000", "%#g", -42.0); EXPECT_PRINTF("-42.0000", "%#G", -42.0); - // TODO - //EXPECT_PRINTF("-0x1.5p+5", "%#a", -42.0); - //EXPECT_PRINTF("-0x1.5A+5", "%#A", -42.0); + EXPECT_PRINTF("0x1.p+4", "%#a", 16.0); + EXPECT_PRINTF("0X1.P+4", "%#A", 16.0); // '#' flag is ignored for non-numeric types. EXPECT_PRINTF("x", "%#c", 'x');