MCPcopy Create free account
hub / github.com/Open-GD/OpenGD / format_buffer

Function format_buffer

Source/external/json.hpp:17763–17831  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17761JSON_HEDLEY_NON_NULL(1)
17762JSON_HEDLEY_RETURNS_NON_NULL
17763inline char* format_buffer(char* buf, int len, int decimal_exponent,
17764 int min_exp, int max_exp)
17765{
17766 JSON_ASSERT(min_exp < 0);
17767 JSON_ASSERT(max_exp > 0);
17768
17769 const int k = len;
17770 const int n = len + decimal_exponent;
17771
17772 // v = buf * 10^(n-k)
17773 // k is the length of the buffer (number of decimal digits)
17774 // n is the position of the decimal point relative to the start of the buffer.
17775
17776 if (k <= n && n <= max_exp)
17777 {
17778 // digits[000]
17779 // len <= max_exp + 2
17780
17781 std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
17782 // Make it look like a floating-point number (#362, #378)
17783 buf[n + 0] = '.';
17784 buf[n + 1] = '0';
17785 return buf + (static_cast<size_t>(n) + 2);
17786 }
17787
17788 if (0 < n && n <= max_exp)
17789 {
17790 // dig.its
17791 // len <= max_digits10 + 1
17792
17793 JSON_ASSERT(k > n);
17794
17795 std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
17796 buf[n] = '.';
17797 return buf + (static_cast<size_t>(k) + 1U);
17798 }
17799
17800 if (min_exp < n && n <= 0)
17801 {
17802 // 0.[000]digits
17803 // len <= 2 + (-min_exp - 1) + max_digits10
17804
17805 std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
17806 buf[0] = '0';
17807 buf[1] = '.';
17808 std::memset(buf + 2, '0', static_cast<size_t>(-n));
17809 return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
17810 }
17811
17812 if (k == 1)
17813 {
17814 // dE+123
17815 // len <= 1 + 5
17816
17817 buf += 1;
17818 }
17819 else
17820 {

Callers 1

json.hppFile · 0.85

Calls 1

append_exponentFunction · 0.85

Tested by

no test coverage detected