MCPcopy Create free account
hub / github.com/CppCXY/EmmyLuaCodeStyle / format_buffer

Function format_buffer

3rd/nlohmann_json/single_include/nlohmann/json.hpp:16294–16362  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16292JSON_HEDLEY_NON_NULL(1)
16293JSON_HEDLEY_RETURNS_NON_NULL
16294inline char* format_buffer(char* buf, int len, int decimal_exponent,
16295 int min_exp, int max_exp)
16296{
16297 JSON_ASSERT(min_exp < 0);
16298 JSON_ASSERT(max_exp > 0);
16299
16300 const int k = len;
16301 const int n = len + decimal_exponent;
16302
16303 // v = buf * 10^(n-k)
16304 // k is the length of the buffer (number of decimal digits)
16305 // n is the position of the decimal point relative to the start of the buffer.
16306
16307 if (k <= n && n <= max_exp)
16308 {
16309 // digits[000]
16310 // len <= max_exp + 2
16311
16312 std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
16313 // Make it look like a floating-point number (#362, #378)
16314 buf[n + 0] = '.';
16315 buf[n + 1] = '0';
16316 return buf + (static_cast<size_t>(n) + 2);
16317 }
16318
16319 if (0 < n && n <= max_exp)
16320 {
16321 // dig.its
16322 // len <= max_digits10 + 1
16323
16324 JSON_ASSERT(k > n);
16325
16326 std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
16327 buf[n] = '.';
16328 return buf + (static_cast<size_t>(k) + 1U);
16329 }
16330
16331 if (min_exp < n && n <= 0)
16332 {
16333 // 0.[000]digits
16334 // len <= 2 + (-min_exp - 1) + max_digits10
16335
16336 std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
16337 buf[0] = '0';
16338 buf[1] = '.';
16339 std::memset(buf + 2, '0', static_cast<size_t>(-n));
16340 return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
16341 }
16342
16343 if (k == 1)
16344 {
16345 // dE+123
16346 // len <= 1 + 5
16347
16348 buf += 1;
16349 }
16350 else
16351 {

Callers 1

json.hppFile · 0.70

Calls 1

append_exponentFunction · 0.70

Tested by

no test coverage detected