From 2453ee576a3291a662b6a96b8201e1e5582f27ad Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 9 Jun 2020 07:17:55 -0700 Subject: [PATCH] Improve default formatting --- include/fmt/core.h | 1 + include/fmt/format.h | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 9985c036..fc82546f 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -1341,6 +1341,7 @@ template class basic_format_context { format_arg arg(int id) const { return args_.get(id); } format_arg arg(basic_string_view name) { return args_.get(name); } int arg_id(basic_string_view name) { return args_.get_id(name); } + const basic_format_args& args() const { return args_; } detail::error_handler error_handler() { return {}; } void on_error(const char* message) { error_handler().on_error(message); } diff --git a/include/fmt/format.h b/include/fmt/format.h index 76c4fcea..8a0a1594 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1750,8 +1750,8 @@ template struct default_arg_formatter { } OutputIt operator()(typename basic_format_arg::handle handle) { - Char s[] = {Char('}')}; - basic_format_parse_context parse_ctx(basic_string_view(s, 1)); + auto s = Char('}'); + basic_format_parse_context parse_ctx(basic_string_view(&s, 1)); basic_format_context format_ctx(out, args, loc); handle.format(parse_ctx, format_ctx); return format_ctx.out(); @@ -2617,9 +2617,9 @@ struct format_handler : detail::error_handler { : parse_context(str), context(out, format_args, loc) {} void on_text(const Char* begin, const Char* end) { - auto size = detail::to_unsigned(end - begin); + auto size = to_unsigned(end - begin); auto out = context.out(); - auto&& it = detail::reserve(out, size); + auto&& it = reserve(out, size); it = std::copy_n(begin, size, it); context.advance_to(out); } @@ -2632,21 +2632,22 @@ struct format_handler : detail::error_handler { return arg_id; } - void on_replacement_field(int id, const Char* p) { + void on_replacement_field(int id, const Char*) { auto arg = get_arg(context, id); context.advance_to(visit_format_arg( - ArgFormatter(context, &parse_context, nullptr, p), arg)); + default_arg_formatter{ + context.out(), context.args(), context.locale()}, + arg)); } const Char* on_format_specs(int id, const Char* begin, const Char* end) { advance_to(parse_context, begin); auto arg = get_arg(context, id); - detail::custom_formatter f(parse_context, context); + custom_formatter f(parse_context, context); if (visit_format_arg(f, arg)) return parse_context.begin(); basic_format_specs specs; - using detail::specs_handler; using parse_context_t = basic_format_parse_context; - detail::specs_checker> handler( + specs_checker> handler( specs_handler(specs, parse_context, context), arg.type()); begin = parse_format_specs(begin, end, handler); @@ -3204,9 +3205,9 @@ typename Context::iterator vformat_to( if (format_str.size() == 2 && detail::equal2(format_str.data(), "{}")) { auto arg = args.get(0); if (!arg) detail::error_handler().on_error("argument not found"); - using it = typename ArgFormatter::iterator; + using iterator = typename ArgFormatter::iterator; return visit_format_arg( - detail::default_arg_formatter{out, args, loc}, arg); + detail::default_arg_formatter{out, args, loc}, arg); } detail::format_handler h(out, format_str, args, loc);