Skip to content

Commit

Permalink
Simplify default formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Aug 4, 2024
1 parent 15f939c commit 16f5523
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3640,21 +3640,18 @@ FMT_CONSTEXPR auto write(OutputIt out, const T& value)
// An argument visitor that formats the argument and writes it via the output
// iterator. It's a class and not a generic lambda for compatibility with C++11.
template <typename Char> struct default_arg_formatter {
using iterator = basic_appender<Char>;
using context = buffered_context<Char>;

iterator out;
basic_appender<Char> out;
basic_format_args<context> args;
locale_ref loc;

template <typename T> auto operator()(T value) -> iterator {
return write<Char>(out, value);
}
auto operator()(typename basic_format_arg<context>::handle h) -> iterator {
basic_format_parse_context<Char> parse_ctx({});
context format_ctx(out, args, loc);
void operator()(monostate) { report_error("argument not found"); }
template <typename T> void operator()(T value) { write<Char>(out, value); }
void operator()(typename basic_format_arg<context>::handle h) {
// Pass the default locale since the default output must be unlocalized.
auto parse_ctx = basic_format_parse_context<Char>({});
auto format_ctx = context(out, args, {});
h.format(parse_ctx, format_ctx);
return format_ctx.out();
}
};

Expand Down Expand Up @@ -4125,12 +4122,8 @@ template <typename Char>
void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt,
typename vformat_args<Char>::type args, locale_ref loc) {
auto out = basic_appender<Char>(buf);
if (fmt.size() == 2 && equal2(fmt.data(), "{}")) {
auto arg = args.get(0);
if (!arg) report_error("argument not found");
arg.visit(default_arg_formatter<Char>{out, args, loc});
return;
}
if (fmt.size() == 2 && equal2(fmt.data(), "{}"))
return args.get(0).visit(default_arg_formatter<Char>{out, args});

struct format_handler {
basic_format_parse_context<Char> parse_context;
Expand Down Expand Up @@ -4160,9 +4153,8 @@ void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt,
}

FMT_INLINE void on_replacement_field(int id, const Char*) {
auto arg = get_arg(context, id);
context.advance_to(arg.visit(default_arg_formatter<Char>{
context.out(), context.args(), context.locale()}));
context.arg(id).visit(
default_arg_formatter<Char>{context.out(), context.args()});
}

auto on_format_specs(int id, const Char* begin, const Char* end)
Expand Down Expand Up @@ -4215,9 +4207,9 @@ FMT_CONSTEXPR FMT_INLINE auto native_formatter<T, Char, TYPE>::format(
specs_.precision_ref.kind == arg_id_kind::none) {
return write<Char>(ctx.out(), val, specs_, ctx.locale());
}
auto specs = specs_;
handle_dynamic_spec(specs.width, specs.width_ref, ctx);
handle_dynamic_spec(specs.precision, specs.precision_ref, ctx);
auto specs = format_specs(specs_);
handle_dynamic_spec(specs.width, specs_.width_ref, ctx);
handle_dynamic_spec(specs.precision, specs_.precision_ref, ctx);
return write<Char>(ctx.out(), val, specs, ctx.locale());
}

Expand Down

0 comments on commit 16f5523

Please sign in to comment.