From 090ee135952b870adccee7b0f96c122ef6661acd Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 17 Jan 2024 05:59:21 -0800 Subject: [PATCH] Pass char type to write --- include/fmt/format.h | 110 ++++++++++++++++++++++--------------------- 1 file changed, 57 insertions(+), 53 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index e17a3b93eeab..6388befb760a 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1725,7 +1725,7 @@ FMT_NOINLINE FMT_CONSTEXPR auto fill(OutputIt it, size_t n, const fill_t& fill) // Writes the output of f, padded according to format specifications in specs. // size: output size in code units. // width: output display width in (terminal) column positions. -template FMT_CONSTEXPR auto write_padded(OutputIt out, const format_specs& specs, size_t size, size_t width, F&& f) -> OutputIt { @@ -1744,21 +1744,21 @@ FMT_CONSTEXPR auto write_padded(OutputIt out, const format_specs& specs, return base_iterator(out, it); } -template constexpr auto write_padded(OutputIt out, const format_specs& specs, size_t size, F&& f) -> OutputIt { - return write_padded(out, specs, size, size, f); + return write_padded(out, specs, size, size, f); } -template +template FMT_CONSTEXPR auto write_bytes(OutputIt out, string_view bytes, const format_specs& specs) -> OutputIt { - return write_padded(out, specs, bytes.size(), - [bytes](reserve_iterator it) { - const char* data = bytes.data(); - return copy(data, data + bytes.size(), it); - }); + return write_padded( + out, specs, bytes.size(), [bytes](reserve_iterator it) { + const char* data = bytes.data(); + return copy(data, data + bytes.size(), it); + }); } template @@ -1771,7 +1771,7 @@ auto write_ptr(OutputIt out, UIntPtr value, const format_specs* specs) *it++ = static_cast('x'); return format_uint<4, Char>(it, value, num_digits); }; - return specs ? write_padded(out, *specs, size, write) + return specs ? write_padded(out, *specs, size, write) : base_iterator(out, write(reserve(out, size))); } @@ -1928,7 +1928,7 @@ template FMT_CONSTEXPR auto write_char(OutputIt out, Char value, const format_specs& specs) -> OutputIt { bool is_debug = specs.type == presentation_type::debug; - return write_padded(out, specs, 1, [=](reserve_iterator it) { + return write_padded(out, specs, 1, [=](reserve_iterator it) { if (is_debug) return write_escaped_char(it, value); *it++ = value; return it; @@ -1942,7 +1942,7 @@ FMT_CONSTEXPR auto write(OutputIt out, Char value, using unsigned_type = conditional_t::value, unsigned char, unsigned>; return check_char_specs(specs) - ? write_char(out, value, specs) + ? write_char(out, value, specs) : write(out, static_cast(value), specs, loc); } @@ -1972,7 +1972,7 @@ template struct write_int_data { // // where are written by write_digits(it). // prefix contains chars in three lower bytes and the size in the fourth byte. -template +template FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, int num_digits, unsigned prefix, const format_specs& specs, @@ -1987,7 +1987,7 @@ FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, int num_digits, return base_iterator(out, write_digits(it)); } auto data = write_int_data(num_digits, prefix, specs); - return write_padded( + return write_padded( out, specs, data.size, [=](reserve_iterator it) { for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8) *it++ = static_cast(p & 0xff); @@ -2103,12 +2103,12 @@ auto write_int(OutputIt out, UInt value, unsigned prefix, format_uint<1, char>(appender(buffer), value, num_digits); break; case presentation_type::chr: - return write_char(out, static_cast(value), specs); + return write_char(out, static_cast(value), specs); } unsigned size = (prefix != 0 ? prefix >> 24 : 0) + to_unsigned(num_digits) + to_unsigned(grouping.count_separators(num_digits)); - return write_padded( + return write_padded( out, specs, size, size, [&](reserve_iterator it) { for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8) *it++ = static_cast(p & 0xff); @@ -2181,7 +2181,7 @@ FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, write_int_arg arg, case presentation_type::none: case presentation_type::dec: { int num_digits = count_digits(abs_value); - return write_int( + return write_int( out, num_digits, prefix, specs, [=](reserve_iterator it) { return format_decimal(it, abs_value, num_digits).end; }); @@ -2190,7 +2190,7 @@ FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, write_int_arg arg, if (specs.alt) prefix_append(prefix, unsigned(specs.upper ? 'X' : 'x') << 8 | '0'); int num_digits = count_digits<4>(abs_value); - return write_int( + return write_int( out, num_digits, prefix, specs, [=](reserve_iterator it) { return format_uint<4, Char>(it, abs_value, num_digits, specs.upper); }); @@ -2201,22 +2201,22 @@ FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, write_int_arg arg, // is not greater than the number of digits. if (specs.alt && specs.precision <= num_digits && abs_value != 0) prefix_append(prefix, '0'); - return write_int(out, num_digits, prefix, specs, - [=](reserve_iterator it) { - return format_uint<3, Char>(it, abs_value, num_digits); - }); + return write_int( + out, num_digits, prefix, specs, [=](reserve_iterator it) { + return format_uint<3, Char>(it, abs_value, num_digits); + }); } case presentation_type::bin: { if (specs.alt) prefix_append(prefix, unsigned(specs.upper ? 'B' : 'b') << 8 | '0'); int num_digits = count_digits<1>(abs_value); - return write_int(out, num_digits, prefix, specs, - [=](reserve_iterator it) { - return format_uint<1, Char>(it, abs_value, num_digits); - }); + return write_int( + out, num_digits, prefix, specs, [=](reserve_iterator it) { + return format_uint<1, Char>(it, abs_value, num_digits); + }); } case presentation_type::chr: - return write_char(out, static_cast(abs_value), specs); + return write_char(out, static_cast(abs_value), specs); } return out; } @@ -2224,7 +2224,7 @@ template FMT_CONSTEXPR FMT_NOINLINE auto write_int_noinline( OutputIt out, write_int_arg arg, const format_specs& specs, locale_ref loc) -> OutputIt { - return write_int(out, arg, specs, loc); + return write_int(out, arg, specs, loc); } template ::value && @@ -2234,8 +2234,8 @@ FMT_CONSTEXPR FMT_INLINE auto write(OutputIt out, T value, const format_specs& specs, locale_ref loc) -> OutputIt { if (specs.localized && write_loc(out, value, specs, loc)) return out; - return write_int_noinline(out, make_write_int_arg(value, specs.sign), specs, - loc); + return write_int_noinline(out, make_write_int_arg(value, specs.sign), + specs, loc); } // An inlined version of write used in format string compilation. template & specs, locale_ref loc) -> OutputIt { if (specs.localized && write_loc(out, value, specs, loc)) return out; - return write_int(out, make_write_int_arg(value, specs.sign), specs, loc); + return write_int(out, make_write_int_arg(value, specs.sign), specs, + loc); } // An output iterator that counts the number of objects written to it and @@ -2307,18 +2308,18 @@ FMT_CONSTEXPR auto write(OutputIt out, basic_string_view s, else width = compute_width(basic_string_view(data, size)); } - return write_padded(out, specs, size, width, - [=](reserve_iterator it) { - if (is_debug) return write_escaped_string(it, s); - return copy(data, data + size, it); - }); + return write_padded(out, specs, size, width, + [=](reserve_iterator it) { + if (is_debug) return write_escaped_string(it, s); + return copy(data, data + size, it); + }); } template FMT_CONSTEXPR auto write(OutputIt out, basic_string_view> s, const format_specs& specs, locale_ref) -> OutputIt { - return write(out, s, specs); + return write(out, s, specs); } template FMT_CONSTEXPR auto write(OutputIt out, const Char* s, @@ -2327,7 +2328,7 @@ FMT_CONSTEXPR auto write(OutputIt out, const Char* s, if (specs.type == presentation_type::pointer) return write_ptr(out, bit_cast(s), &specs); if (!s) report_error("string pointer is null"); - return write(out, basic_string_view(s), specs, {}); + return write(out, basic_string_view(s), specs, {}); } template () == '0'; if (is_zero_fill) specs.fill = ' '; - return write_padded(out, specs, size, [=](reserve_iterator it) { - if (sign) *it++ = detail::sign(sign); - return copy(str, str + str_size, it); - }); + return write_padded(out, specs, size, + [=](reserve_iterator it) { + if (sign) *it++ = detail::sign(sign); + return copy(str, str + str_size, it); + }); } // A decimal floating-point number significand * pow(10, exp). @@ -2621,8 +2623,9 @@ FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f, *it++ = static_cast(exp_char); return write_exponent(output_exp, it); }; - return specs.width > 0 ? write_padded(out, specs, size, write) - : base_iterator(out, write(reserve(out, size))); + return specs.width > 0 + ? write_padded(out, specs, size, write) + : base_iterator(out, write(reserve(out, size))); } int exp = f.exponent + significand_size; @@ -2638,7 +2641,7 @@ FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f, } auto grouping = Grouping(loc, fspecs.locale); size += to_unsigned(grouping.count_separators(exp)); - return write_padded(out, specs, size, [&](iterator it) { + return write_padded(out, specs, size, [&](iterator it) { if (sign) *it++ = detail::sign(sign); it = write_significand(it, significand, significand_size, f.exponent, grouping); @@ -2652,7 +2655,7 @@ FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f, size += 1 + to_unsigned(num_zeros > 0 ? num_zeros : 0); auto grouping = Grouping(loc, fspecs.locale); size += to_unsigned(grouping.count_separators(exp)); - return write_padded(out, specs, size, [&](iterator it) { + return write_padded(out, specs, size, [&](iterator it) { if (sign) *it++ = detail::sign(sign); it = write_significand(it, significand, significand_size, exp, decimal_point, grouping); @@ -2667,7 +2670,7 @@ FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f, } bool pointy = num_zeros != 0 || significand_size != 0 || fspecs.showpoint; size += 1 + (pointy ? 1 : 0) + to_unsigned(num_zeros); - return write_padded(out, specs, size, [&](iterator it) { + return write_padded(out, specs, size, [&](iterator it) { if (sign) *it++ = detail::sign(sign); *it++ = zero; if (!pointy) return it; @@ -3585,8 +3588,8 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, if (fspecs.format == float_format::hex) { if (fspecs.sign) buffer.push_back(detail::sign(fspecs.sign)); format_hexfloat(convert_float(value), specs.precision, fspecs, buffer); - return write_bytes(out, {buffer.data(), buffer.size()}, - specs); + return write_bytes(out, {buffer.data(), buffer.size()}, + specs); } int precision = specs.precision >= 0 || specs.type == presentation_type::none ? specs.precision @@ -3686,8 +3689,8 @@ FMT_CONSTEXPR auto write(OutputIt out, T value, -> OutputIt { return specs.type != presentation_type::none && specs.type != presentation_type::string - ? write(out, value ? 1 : 0, specs, {}) - : write_bytes(out, value ? "true" : "false", specs); + ? write(out, value ? 1 : 0, specs, {}) + : write_bytes(out, value ? "true" : "false", specs); } template @@ -3766,7 +3769,7 @@ template struct arg_formatter { template FMT_CONSTEXPR FMT_INLINE auto operator()(T value) -> iterator { - return detail::write(out, value, specs, locale); + return detail::write(out, value, specs, locale); } auto operator()(typename basic_format_arg::handle) -> iterator { // User-defined types are handled separately because they require access @@ -4185,7 +4188,8 @@ template struct nested_formatter { specs.width = width_; specs.fill = fill_; specs.align = align_; - return detail::write(ctx.out(), string_view(buf.data(), buf.size()), specs); + return detail::write(ctx.out(), string_view(buf.data(), buf.size()), + specs); } auto nested(const T& value) const -> nested_view {