| 17885 | JSON_HEDLEY_NON_NULL(1) |
| 17886 | JSON_HEDLEY_RETURNS_NON_NULL |
| 17887 | inline char* format_buffer(char* buf, int len, int decimal_exponent, |
| 17888 | int min_exp, int max_exp) |
| 17889 | { |
| 17890 | JSON_ASSERT(min_exp < 0); |
| 17891 | JSON_ASSERT(max_exp > 0); |
| 17892 | |
| 17893 | const int k = len; |
| 17894 | const int n = len + decimal_exponent; |
| 17895 | |
| 17896 | // v = buf * 10^(n-k) |
| 17897 | // k is the length of the buffer (number of decimal digits) |
| 17898 | // n is the position of the decimal point relative to the start of the buffer. |
| 17899 | |
| 17900 | if (k <= n && n <= max_exp) |
| 17901 | { |
| 17902 | // digits[000] |
| 17903 | // len <= max_exp + 2 |
| 17904 | |
| 17905 | std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k)); |
| 17906 | // Make it look like a floating-point number (#362, #378) |
| 17907 | buf[n + 0] = '.'; |
| 17908 | buf[n + 1] = '0'; |
| 17909 | return buf + (static_cast<size_t>(n) + 2); |
| 17910 | } |
| 17911 | |
| 17912 | if (0 < n && n <= max_exp) |
| 17913 | { |
| 17914 | // dig.its |
| 17915 | // len <= max_digits10 + 1 |
| 17916 | |
| 17917 | JSON_ASSERT(k > n); |
| 17918 | |
| 17919 | std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n)); |
| 17920 | buf[n] = '.'; |
| 17921 | return buf + (static_cast<size_t>(k) + 1U); |
| 17922 | } |
| 17923 | |
| 17924 | if (min_exp < n && n <= 0) |
| 17925 | { |
| 17926 | // 0.[000]digits |
| 17927 | // len <= 2 + (-min_exp - 1) + max_digits10 |
| 17928 | |
| 17929 | std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k)); |
| 17930 | buf[0] = '0'; |
| 17931 | buf[1] = '.'; |
| 17932 | std::memset(buf + 2, '0', static_cast<size_t>(-n)); |
| 17933 | return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k)); |
| 17934 | } |
| 17935 | |
| 17936 | if (k == 1) |
| 17937 | { |
| 17938 | // dE+123 |
| 17939 | // len <= 1 + 5 |
| 17940 | |
| 17941 | buf += 1; |
| 17942 | } |
| 17943 | else |
| 17944 | { |
no test coverage detected