TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ format_buffer()

JSON_HEDLEY_RETURNS_NON_NULL char * nlohmann::detail::dtoa_impl::format_buffer ( char *  buf,
int  len,
int  decimal_exponent,
int  min_exp,
int  max_exp 
)
inline

prettify v = buf * 10^decimal_exponent

If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point notation. Otherwise it will be printed in exponential notation.

Precondition
min_exp < 0
max_exp > 0

Definition at line 16467 of file json.hpp.

16469{
16470 JSON_ASSERT(min_exp < 0);
16471 JSON_ASSERT(max_exp > 0);
16472 const int k = len;
16473 const int n = len + decimal_exponent;
16474
16475 // v = buf * 10^(n-k)
16476 // k is the length of the buffer (number of decimal digits)
16477 // n is the position of the decimal point relative to the start of the buffer.
16478
16479 if (k <= n && n <= max_exp)
16480 {
16481 // digits[000]
16482 // len <= max_exp + 2
16483 std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
16484 // Make it look like a floating-point number (#362, #378)
16485 buf[n + 0] = '.';
16486 buf[n + 1] = '0';
16487 return buf + (static_cast<size_t>(n) + 2);
16488 }
16489
16490 if (0 < n && n <= max_exp)
16491 {
16492 // dig.its
16493 // len <= max_digits10 + 1
16494 JSON_ASSERT(k > n);
16495 std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
16496 buf[n] = '.';
16497 return buf + (static_cast<size_t>(k) + 1U);
16498 }
16499
16500 if (min_exp < n && n <= 0)
16501 {
16502 // 0.[000]digits
16503 // len <= 2 + (-min_exp - 1) + max_digits10
16504 std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
16505 buf[0] = '0';
16506 buf[1] = '.';
16507 std::memset(buf + 2, '0', static_cast<size_t>(-n));
16508 return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
16509 }
16510
16511 if (k == 1)
16512 {
16513 // dE+123
16514 // len <= 1 + 5
16515 buf += 1;
16516 }
16517
16518 else
16519 {
16520 // d.igitsE+123
16521 // len <= max_digits10 + 1 + 5
16522 std::memmove(buf + 2, buf + 1, static_cast<size_t>(k) - 1);
16523 buf[1] = '.';
16524 buf += 1 + static_cast<size_t>(k);
16525 }
16526
16527 *buf++ = 'e';
16528 return append_exponent(buf, n - 1);
16529}
JSON_HEDLEY_RETURNS_NON_NULL char * append_exponent(char *buf, int e)
appends a decimal representation of e to buf
Definition: json.hpp:16411