Skip to content

Commit

Permalink
Fix handling of hexfloat
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jun 13, 2019
1 parent 92a44db commit cbbee1b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
29 changes: 15 additions & 14 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -786,20 +786,21 @@ void sprintf_format(Double value, internal::buffer<char>& 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<unsigned>(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<unsigned>(p - where);
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit cbbee1b

Please sign in to comment.