Skip to content

Commit

Permalink
Small bufix related to creation of fmt string for snprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Astashyn authored and Alex Astashyn committed Dec 6, 2016
1 parent 2197856 commit 509447b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
17 changes: 8 additions & 9 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6464,19 +6464,18 @@ class basic_json
}

static constexpr auto d =
std::numeric_limits<number_float_t>::digits10+1;
std::numeric_limits<number_float_t>::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;
Expand Down
17 changes: 8 additions & 9 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -6464,19 +6464,18 @@ class basic_json
}

static constexpr auto d =
std::numeric_limits<number_float_t>::digits10+1;
std::numeric_limits<number_float_t>::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;
Expand Down

0 comments on commit 509447b

Please sign in to comment.