| 18598 | JSON_HEDLEY_NON_NULL(1) |
| 18599 | JSON_HEDLEY_RETURNS_NON_NULL |
| 18600 | inline char* format_buffer(char* buf, int len, int decimal_exponent, |
| 18601 | int min_exp, int max_exp) |
| 18602 | { |
| 18603 | JSON_ASSERT(min_exp < 0); |
| 18604 | JSON_ASSERT(max_exp > 0); |
| 18605 | |
| 18606 | const int k = len; |
| 18607 | const int n = len + decimal_exponent; |
| 18608 | |
| 18609 | // v = buf * 10^(n-k) |
| 18610 | // k is the length of the buffer (number of decimal digits) |
| 18611 | // n is the position of the decimal point relative to the start of the buffer. |
| 18612 | |
| 18613 | if (k <= n && n <= max_exp) |
| 18614 | { |
| 18615 | // digits[000] |
| 18616 | // len <= max_exp + 2 |
| 18617 | |
| 18618 | std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k)); |
| 18619 | // Make it look like a floating-point number (#362, #378) |
| 18620 | buf[n + 0] = '.'; |
| 18621 | buf[n + 1] = '0'; |
| 18622 | return buf + (static_cast<size_t>(n) + 2); |
| 18623 | } |
| 18624 | |
| 18625 | if (0 < n && n <= max_exp) |
| 18626 | { |
| 18627 | // dig.its |
| 18628 | // len <= max_digits10 + 1 |
| 18629 | |
| 18630 | JSON_ASSERT(k > n); |
| 18631 | |
| 18632 | std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n)); |
| 18633 | buf[n] = '.'; |
| 18634 | return buf + (static_cast<size_t>(k) + 1U); |
| 18635 | } |
| 18636 | |
| 18637 | if (min_exp < n && n <= 0) |
| 18638 | { |
| 18639 | // 0.[000]digits |
| 18640 | // len <= 2 + (-min_exp - 1) + max_digits10 |
| 18641 | |
| 18642 | std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k)); |
| 18643 | buf[0] = '0'; |
| 18644 | buf[1] = '.'; |
| 18645 | std::memset(buf + 2, '0', static_cast<size_t>(-n)); |
| 18646 | return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k)); |
| 18647 | } |
| 18648 | |
| 18649 | if (k == 1) |
| 18650 | { |
| 18651 | // dE+123 |
| 18652 | // len <= 1 + 5 |
| 18653 | |
| 18654 | buf += 1; |
| 18655 | } |
| 18656 | else |
| 18657 | { |
no test coverage detected