! @brief 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. @pre min_exp < 0 @pre max_exp > 0 */
| 11872 | @pre max_exp > 0 |
| 11873 | */ |
| 11874 | inline char* format_buffer(char* buf, int len, int decimal_exponent, |
| 11875 | int min_exp, int max_exp) |
| 11876 | { |
| 11877 | assert(min_exp < 0); |
| 11878 | assert(max_exp > 0); |
| 11879 | |
| 11880 | const int k = len; |
| 11881 | const int n = len + decimal_exponent; |
| 11882 | |
| 11883 | // v = buf * 10^(n-k) |
| 11884 | // k is the length of the buffer (number of decimal digits) |
| 11885 | // n is the position of the decimal point relative to the start of the buffer. |
| 11886 | |
| 11887 | if (k <= n and n <= max_exp) |
| 11888 | { |
| 11889 | // digits[000] |
| 11890 | // len <= max_exp + 2 |
| 11891 | |
| 11892 | std::memset(buf + k, '0', static_cast<size_t>(n - k)); |
| 11893 | // Make it look like a floating-point number (#362, #378) |
| 11894 | buf[n + 0] = '.'; |
| 11895 | buf[n + 1] = '0'; |
| 11896 | return buf + (n + 2); |
| 11897 | } |
| 11898 | |
| 11899 | if (0 < n and n <= max_exp) |
| 11900 | { |
| 11901 | // dig.its |
| 11902 | // len <= max_digits10 + 1 |
| 11903 | |
| 11904 | assert(k > n); |
| 11905 | |
| 11906 | std::memmove(buf + (n + 1), buf + n, static_cast<size_t>(k - n)); |
| 11907 | buf[n] = '.'; |
| 11908 | return buf + (k + 1); |
| 11909 | } |
| 11910 | |
| 11911 | if (min_exp < n and n <= 0) |
| 11912 | { |
| 11913 | // 0.[000]digits |
| 11914 | // len <= 2 + (-min_exp - 1) + max_digits10 |
| 11915 | |
| 11916 | std::memmove(buf + (2 + -n), buf, static_cast<size_t>(k)); |
| 11917 | buf[0] = '0'; |
| 11918 | buf[1] = '.'; |
| 11919 | std::memset(buf + 2, '0', static_cast<size_t>(-n)); |
| 11920 | return buf + (2 + (-n) + k); |
| 11921 | } |
| 11922 | |
| 11923 | if (k == 1) |
| 11924 | { |
| 11925 | // dE+123 |
| 11926 | // len <= 1 + 5 |
| 11927 | |
| 11928 | buf += 1; |
| 11929 | } |
| 11930 | else |
| 11931 | { |
no test coverage detected