From cbbee1b38569c6394cc1169b5c49fd2640035012 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 12 Jun 2019 20:03:56 -0700 Subject: [PATCH] Fix handling of hexfloat --- include/fmt/format-inl.h | 29 +++++++++++++++-------------- test/format-test.cc | 2 ++ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 9a00c21f..96e9cd73 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -786,20 +786,21 @@ void sprintf_format(Double value, internal::buffer& buf, // Find the decimal point. auto p = buf.data(), end = p + n; if (*p == '+' || *p == '-') ++p; - if (spec.type == 'a' || spec.type == 'A') p += 2; // Skip "0x". - while (p < end && *p >= '0' && *p <= '9') ++p; - if (p < end && *p != 'e' && *p != 'E') { - if (*p != '.') *p = '.'; - if (!spec.type) { - // Keep only one trailing zero after the decimal point. - ++p; - if (*p == '0') ++p; - while (p != end && *p >= '1' && *p <= '9') ++p; - char* where = p; - while (p != end && *p == '0') ++p; - if (p == end || *p < '0' || *p > '9') { - if (p != end) std::memmove(where, p, to_unsigned(end - p)); - n -= static_cast(p - where); + if (spec.type != 'a' && spec.type != 'A') { + while (p < end && *p >= '0' && *p <= '9') ++p; + if (p < end && *p != 'e' && *p != 'E') { + if (*p != '.') *p = '.'; + if (!spec.type) { + // Keep only one trailing zero after the decimal point. + ++p; + if (*p == '0') ++p; + while (p != end && *p >= '1' && *p <= '9') ++p; + char* where = p; + while (p != end && *p == '0') ++p; + if (p == end || *p < '0' || *p > '9') { + if (p != end) std::memmove(where, p, to_unsigned(end - p)); + n -= static_cast(p - where); + } } } } diff --git a/test/format-test.cc b/test/format-test.cc index 7c2b1c5a..0d7153e3 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1506,6 +1506,8 @@ TEST(FormatterTest, FormatLongDouble) { safe_sprintf(buffer, "%Le", 392.65l); EXPECT_EQ(buffer, format("{0:e}", 392.65l)); EXPECT_EQ("+0000392.6", format("{0:+010.4g}", 392.64l)); + safe_sprintf(buffer, "%La", 3.31l); + EXPECT_EQ(buffer, format("{:a}", 3.31l)); } TEST(FormatterTest, FormatChar) {