MCPcopy Create free account
hub / github.com/CalcProgrammer1/OpenRGB / format_buffer

Function format_buffer

dependencies/json/json.hpp:15388–15456  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15386JSON_HEDLEY_NON_NULL(1)
15387JSON_HEDLEY_RETURNS_NON_NULL
15388inline char* format_buffer(char* buf, int len, int decimal_exponent,
15389 int min_exp, int max_exp)
15390{
15391 JSON_ASSERT(min_exp < 0);
15392 JSON_ASSERT(max_exp > 0);
15393
15394 const int k = len;
15395 const int n = len + decimal_exponent;
15396
15397 // v = buf * 10^(n-k)
15398 // k is the length of the buffer (number of decimal digits)
15399 // n is the position of the decimal point relative to the start of the buffer.
15400
15401 if (k <= n && n <= max_exp)
15402 {
15403 // digits[000]
15404 // len <= max_exp + 2
15405
15406 std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
15407 // Make it look like a floating-point number (#362, #378)
15408 buf[n + 0] = '.';
15409 buf[n + 1] = '0';
15410 return buf + (static_cast<size_t>(n) + 2);
15411 }
15412
15413 if (0 < n && n <= max_exp)
15414 {
15415 // dig.its
15416 // len <= max_digits10 + 1
15417
15418 JSON_ASSERT(k > n);
15419
15420 std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
15421 buf[n] = '.';
15422 return buf + (static_cast<size_t>(k) + 1U);
15423 }
15424
15425 if (min_exp < n && n <= 0)
15426 {
15427 // 0.[000]digits
15428 // len <= 2 + (-min_exp - 1) + max_digits10
15429
15430 std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
15431 buf[0] = '0';
15432 buf[1] = '.';
15433 std::memset(buf + 2, '0', static_cast<size_t>(-n));
15434 return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
15435 }
15436
15437 if (k == 1)
15438 {
15439 // dE+123
15440 // len <= 1 + 5
15441
15442 buf += 1;
15443 }
15444 else
15445 {

Callers 1

json.hppFile · 0.70

Calls 1

append_exponentFunction · 0.70

Tested by

no test coverage detected