MCPcopy Create free account
hub / github.com/OpenFodder/openfodder / format_buffer

Function format_buffer

Source/Utils/json.hpp:15313–15381  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15311 JSON_HEDLEY_NON_NULL(1)
15312 JSON_HEDLEY_RETURNS_NON_NULL
15313 inline char* format_buffer(char* buf, int len, int decimal_exponent,
15314 int min_exp, int max_exp)
15315 {
15316 JSON_ASSERT(min_exp < 0);
15317 JSON_ASSERT(max_exp > 0);
15318
15319 const int k = len;
15320 const int n = len + decimal_exponent;
15321
15322 // v = buf * 10^(n-k)
15323 // k is the length of the buffer (number of decimal digits)
15324 // n is the position of the decimal point relative to the start of the buffer.
15325
15326 if (k <= n && n <= max_exp)
15327 {
15328 // digits[000]
15329 // len <= max_exp + 2
15330
15331 std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
15332 // Make it look like a floating-point number (#362, #378)
15333 buf[n + 0] = '.';
15334 buf[n + 1] = '0';
15335 return buf + (static_cast<size_t>(n) + 2);
15336 }
15337
15338 if (0 < n && n <= max_exp)
15339 {
15340 // dig.its
15341 // len <= max_digits10 + 1
15342
15343 JSON_ASSERT(k > n);
15344
15345 std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
15346 buf[n] = '.';
15347 return buf + (static_cast<size_t>(k) + 1U);
15348 }
15349
15350 if (min_exp < n && n <= 0)
15351 {
15352 // 0.[000]digits
15353 // len <= 2 + (-min_exp - 1) + max_digits10
15354
15355 std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
15356 buf[0] = '0';
15357 buf[1] = '.';
15358 std::memset(buf + 2, '0', static_cast<size_t>(-n));
15359 return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
15360 }
15361
15362 if (k == 1)
15363 {
15364 // dE+123
15365 // len <= 1 + 5
15366
15367 buf += 1;
15368 }
15369 else
15370 {

Callers 1

json.hppFile · 0.85

Calls 1

append_exponentFunction · 0.85

Tested by

no test coverage detected