From 0faa968cc3aae5a3ad155d9e4764d890293d3088 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 17 Mar 2019 13:15:33 -0700 Subject: [PATCH] Make floating-point formatting locale-independent --- include/fmt/format-inl.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index b180051d..b6994794 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -735,13 +735,17 @@ void sprintf_format(Double value, internal::buffer& buf, if (result >= 0) { unsigned n = internal::to_unsigned(result); if (n < buf.capacity()) { - if (!spec.type) { - // Keep only one trailing zero after the decimal point. - auto p = static_cast(std::memchr(buf.data(), '.', n)); - if (p) { + // 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; - const char* end = buf.data() + n; while (p != end && *p >= '1' && *p <= '9') ++p; char* where = p; while (p != end && *p == '0') ++p;