From 509447b4d5ca42c03d15e4de810c528c0fb25986 Mon Sep 17 00:00:00 2001 From: Alex Astashyn Date: Mon, 5 Dec 2016 19:03:39 -0500 Subject: [PATCH] Small bufix related to creation of fmt string for snprintf --- src/json.hpp | 17 ++++++++--------- src/json.hpp.re2c | 17 ++++++++--------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index a886c4369..14780ca8c 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -6464,19 +6464,18 @@ class basic_json } static constexpr auto d = - std::numeric_limits::digits10+1; + std::numeric_limits::digits10; + static_assert(d == 6 or d == 15 or d == 16 or d == 17, ""); - // I'm not sure why we need that +1 above, if at all, + static constexpr auto fmt = d == 6 ? "%.7g" + : d == 15 ? "%.16g" + : d == 16 ? "%.17g" + : d == 17 ? "%.18g" + : "%.19g"; + // I'm not sure why we need to +1 the precision, // but without it there's a unit-test that fails // that asserts precision of the output - static_assert(d == 6 or d == 15 or d == 16 or d == 17, ""); - static constexpr auto fmt = d == 6 ? "%.6g" - : d == 15 ? "%.15g" - : d == 16 ? "%.16g" - : d == 17 ? "%.17g" - : "%.18g"; - snprintf(m_buf.data(), m_buf.size(), fmt, x); const std::locale loc; diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index dd7af5dde..cac41603e 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -6464,19 +6464,18 @@ class basic_json } static constexpr auto d = - std::numeric_limits::digits10+1; + std::numeric_limits::digits10; + static_assert(d == 6 or d == 15 or d == 16 or d == 17, ""); - // I'm not sure why we need that +1 above, if at all, + static constexpr auto fmt = d == 6 ? "%.7g" + : d == 15 ? "%.16g" + : d == 16 ? "%.17g" + : d == 17 ? "%.18g" + : "%.19g"; + // I'm not sure why we need to +1 the precision, // but without it there's a unit-test that fails // that asserts precision of the output - static_assert(d == 6 or d == 15 or d == 16 or d == 17, ""); - static constexpr auto fmt = d == 6 ? "%.6g" - : d == 15 ? "%.15g" - : d == 16 ? "%.16g" - : d == 17 ? "%.17g" - : "%.18g"; - snprintf(m_buf.data(), m_buf.size(), fmt, x); const std::locale loc;