MCPcopy Create free account
hub / github.com/InteractiveComputerGraphics/SPlisHSPlasH / format_buffer

Function format_buffer

extern/json/json.hpp:17801–17869  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17799JSON_HEDLEY_NON_NULL(1)
17800JSON_HEDLEY_RETURNS_NON_NULL
17801inline char* format_buffer(char* buf, int len, int decimal_exponent,
17802 int min_exp, int max_exp)
17803{
17804 JSON_ASSERT(min_exp < 0);
17805 JSON_ASSERT(max_exp > 0);
17806
17807 const int k = len;
17808 const int n = len + decimal_exponent;
17809
17810 // v = buf * 10^(n-k)
17811 // k is the length of the buffer (number of decimal digits)
17812 // n is the position of the decimal point relative to the start of the buffer.
17813
17814 if (k <= n && n <= max_exp)
17815 {
17816 // digits[000]
17817 // len <= max_exp + 2
17818
17819 std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
17820 // Make it look like a floating-point number (#362, #378)
17821 buf[n + 0] = '.';
17822 buf[n + 1] = '0';
17823 return buf + (static_cast<size_t>(n) + 2);
17824 }
17825
17826 if (0 < n && n <= max_exp)
17827 {
17828 // dig.its
17829 // len <= max_digits10 + 1
17830
17831 JSON_ASSERT(k > n);
17832
17833 std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
17834 buf[n] = '.';
17835 return buf + (static_cast<size_t>(k) + 1U);
17836 }
17837
17838 if (min_exp < n && n <= 0)
17839 {
17840 // 0.[000]digits
17841 // len <= 2 + (-min_exp - 1) + max_digits10
17842
17843 std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
17844 buf[0] = '0';
17845 buf[1] = '.';
17846 std::memset(buf + 2, '0', static_cast<size_t>(-n));
17847 return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
17848 }
17849
17850 if (k == 1)
17851 {
17852 // dE+123
17853 // len <= 1 + 5
17854
17855 buf += 1;
17856 }
17857 else
17858 {

Callers 1

json.hppFile · 0.85

Calls 1

append_exponentFunction · 0.85

Tested by

no test coverage detected