MCPcopy Create free account
hub / github.com/fmtlib/fmt / write_fixed

Method write_fixed

include/fmt/format.h:2522–2584  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2520template <typename Char, typename Grouping, typename OutputIt,
2521 typename DecimalFP>
2522FMT_CONSTEXPR20 auto write_fixed(OutputIt out, const DecimalFP& f,
2523 int significand_size, Char decimal_point,
2524 const format_specs& specs, sign s,
2525 locale_ref loc = {}) -> OutputIt {
2526 using iterator = reserve_iterator<OutputIt>;
2527
2528 int exp = f.exponent + significand_size;
2529 long long size = significand_size + (s != sign::none ? 1 : 0);
2530 if (f.exponent >= 0) {
2531 // 1234e5 -> 123400000[.0+]
2532 size += f.exponent;
2533 int num_zeros = specs.precision - exp;
2534 abort_fuzzing_if(num_zeros > 5000);
2535 if (specs.alt()) {
2536 ++size;
2537 if (num_zeros <= 0 && specs.type() != presentation_type::fixed)
2538 num_zeros = 0;
2539 if (num_zeros > 0) size += num_zeros;
2540 }
2541 auto grouping = Grouping(loc, specs.localized());
2542 size += grouping.count_separators(exp);
2543 return write_padded<Char, align::right>(
2544 out, specs, static_cast<size_t>(size), [&](iterator it) {
2545 if (s != sign::none) *it++ = detail::getsign<Char>(s);
2546 it = write_significand<Char>(it, f.significand, significand_size,
2547 f.exponent, grouping);
2548 if (!specs.alt()) return it;
2549 *it++ = decimal_point;
2550 return num_zeros > 0 ? detail::fill_n(it, num_zeros, Char('0')) : it;
2551 });
2552 }
2553 if (exp > 0) {
2554 // 1234e-2 -> 12.34[0+]
2555 int num_zeros = specs.alt() ? specs.precision - significand_size : 0;
2556 size += 1 + max_of(num_zeros, 0);
2557 auto grouping = Grouping(loc, specs.localized());
2558 size += grouping.count_separators(exp);
2559 return write_padded<Char, align::right>(
2560 out, specs, static_cast<size_t>(size), [&](iterator it) {
2561 if (s != sign::none) *it++ = detail::getsign<Char>(s);
2562 it = write_significand(it, f.significand, significand_size, exp,
2563 decimal_point, grouping);
2564 return num_zeros > 0 ? detail::fill_n(it, num_zeros, Char('0')) : it;
2565 });
2566 }
2567 // 1234e-6 -> 0.001234
2568 int num_zeros = -exp;
2569 if (significand_size == 0 && specs.precision >= 0 &&
2570 specs.precision < num_zeros) {
2571 num_zeros = specs.precision;
2572 }
2573 bool pointy = num_zeros != 0 || significand_size != 0 || specs.alt();
2574 size += 1 + (pointy ? 1 : 0) + num_zeros;
2575 return write_padded<Char, align::right>(
2576 out, specs, static_cast<size_t>(size), [&](iterator it) {
2577 if (s != sign::none) *it++ = detail::getsign<Char>(s);
2578 *it++ = Char('0');
2579 if (!pointy) return it;

Callers

nothing calls this directly

Calls 7

abort_fuzzing_ifFunction · 0.85
fill_nFunction · 0.85
max_ofFunction · 0.85
altMethod · 0.80
typeMethod · 0.45
localizedMethod · 0.45
count_separatorsMethod · 0.45

Tested by

no test coverage detected