MCPcopy Create free account
hub / github.com/arun11299/cpp-jwt / format_buffer

Function format_buffer

include/jwt/json/json.hpp:15314–15382  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 1

json.hppFile · 0.85

Calls 1

append_exponentFunction · 0.85

Tested by

no test coverage detected