diff --git a/include/fmt/format.h b/include/fmt/format.h index d1208844..fa76aa11 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2123,16 +2123,14 @@ template auto write_int(OutputIt out, UInt value, unsigned prefix, const format_specs& specs, const digit_grouping& grouping) -> OutputIt { - static_assert(std::is_same, UInt>::value, ""); - auto minimum = [](int a, int b) { return a < b ? a : b; }; + static_assert(std::is_same, UInt>::value, ""); int num_digits = 0; - char digits[40]; - + auto buffer = memory_buffer(); switch (specs.type) { case presentation_type::none: case presentation_type::dec: { - num_digits = minimum(count_digits(value), 40); - format_decimal(digits, value, num_digits); + num_digits = count_digits(value); + format_decimal(appender(buffer), value, num_digits); break; } case presentation_type::hex_lower: @@ -2140,8 +2138,8 @@ auto write_int(OutputIt out, UInt value, unsigned prefix, bool upper = specs.type == presentation_type::hex_upper; if (specs.alt) prefix_append(prefix, unsigned(upper ? 'X' : 'x') << 8 | '0'); - num_digits = minimum(count_digits<4>(value), 40); - format_uint<4>(digits, value, num_digits, upper); + num_digits = count_digits<4>(value); + format_uint<4,Char>(appender(buffer), value, num_digits, upper); break; } case presentation_type::bin_lower: @@ -2149,17 +2147,17 @@ auto write_int(OutputIt out, UInt value, unsigned prefix, bool upper = specs.type == presentation_type::bin_upper; if (specs.alt) prefix_append(prefix, unsigned(upper ? 'B' : 'b') << 8 | '0'); - num_digits = minimum(count_digits<1>(value), 40); - format_uint<1>(digits, value, num_digits); + num_digits = count_digits<1>(value); + format_uint<1,Char>(appender(buffer), value, num_digits); break; } case presentation_type::oct: { - num_digits = minimum(count_digits<3>(value), 40); + num_digits = count_digits<3>(value); // Octal prefix '0' is counted as a digit, so only add it if precision // is not greater than the number of digits. if (specs.alt && specs.precision <= num_digits && value != 0) prefix_append(prefix, '0'); - format_uint<3>(digits, value, num_digits); + format_uint<3,Char>(appender(buffer), value, num_digits); break; } case presentation_type::chr: @@ -2171,10 +2169,10 @@ auto write_int(OutputIt out, UInt value, unsigned prefix, unsigned size = (prefix != 0 ? prefix >> 24 : 0) + to_unsigned(num_digits) + to_unsigned(grouping.count_separators(num_digits)); return write_padded( - out, specs, size, size, [=](reserve_iterator it) { + out, specs, size, size, [&](reserve_iterator it) { for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8) *it++ = static_cast(p & 0xff); - return grouping.apply(it, string_view(digits, to_unsigned(num_digits))); + return grouping.apply(it, string_view(buffer.data(), buffer.size())); }); }