MCPcopy Create free account
hub / github.com/LAStools/LAStools / format_buffer

Function format_buffer

src/json.hpp:17891–17959  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 1

json.hppFile · 0.85

Calls 1

append_exponentFunction · 0.85

Tested by

no test coverage detected