MCPcopy Create free account
hub / github.com/Tablecruncher/tablecruncher / format_buffer

Function format_buffer

external/json/json.hpp:10644–10712  ·  view source on GitHub ↗

! @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 */

Source from the content-addressed store, hash-verified

10642@pre max_exp > 0
10643*/
10644inline char* format_buffer(char* buf, int len, int decimal_exponent,
10645 int min_exp, int max_exp)
10646{
10647 assert(min_exp < 0);
10648 assert(max_exp > 0);
10649
10650 const int k = len;
10651 const int n = len + decimal_exponent;
10652
10653 // v = buf * 10^(n-k)
10654 // k is the length of the buffer (number of decimal digits)
10655 // n is the position of the decimal point relative to the start of the buffer.
10656
10657 if (k <= n and n <= max_exp)
10658 {
10659 // digits[000]
10660 // len <= max_exp + 2
10661
10662 std::memset(buf + k, '0', static_cast<size_t>(n - k));
10663 // Make it look like a floating-point number (#362, #378)
10664 buf[n + 0] = '.';
10665 buf[n + 1] = '0';
10666 return buf + (n + 2);
10667 }
10668
10669 if (0 < n and n <= max_exp)
10670 {
10671 // dig.its
10672 // len <= max_digits10 + 1
10673
10674 assert(k > n);
10675
10676 std::memmove(buf + (n + 1), buf + n, static_cast<size_t>(k - n));
10677 buf[n] = '.';
10678 return buf + (k + 1);
10679 }
10680
10681 if (min_exp < n and n <= 0)
10682 {
10683 // 0.[000]digits
10684 // len <= 2 + (-min_exp - 1) + max_digits10
10685
10686 std::memmove(buf + (2 + -n), buf, static_cast<size_t>(k));
10687 buf[0] = '0';
10688 buf[1] = '.';
10689 std::memset(buf + 2, '0', static_cast<size_t>(-n));
10690 return buf + (2 + (-n) + k);
10691 }
10692
10693 if (k == 1)
10694 {
10695 // dE+123
10696 // len <= 1 + 5
10697
10698 buf += 1;
10699 }
10700 else
10701 {

Callers 1

to_charsFunction · 0.85

Calls 1

append_exponentFunction · 0.85

Tested by

no test coverage detected