From 64a6c8459282de9cc2d8bbf8025a28d2aa6ccb05 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 1 Sep 2024 08:05:06 -0700 Subject: [PATCH] basic_format_parse_context -> parse_context --- include/fmt/base.h | 41 +++++++++++++++++++---------------------- include/fmt/chrono.h | 21 +++++++-------------- include/fmt/compile.h | 8 ++++---- include/fmt/format.h | 39 ++++++++++++++++++--------------------- include/fmt/printf.h | 6 +++--- include/fmt/std.h | 9 +++------ include/fmt/xchar.h | 2 +- 7 files changed, 55 insertions(+), 71 deletions(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index 88799cfd..fc9ba1b7 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -782,7 +782,7 @@ using char_t = typename V::value_type; * You can use the `format_parse_context` type alias for `char` instead. */ FMT_EXPORT -template class basic_format_parse_context { +template class parse_context { private: basic_string_view format_str_; int next_arg_id_; @@ -793,8 +793,8 @@ template class basic_format_parse_context { using char_type = Char; using iterator = const Char*; - explicit constexpr basic_format_parse_context( - basic_string_view format_str, int next_arg_id = 0) + explicit constexpr parse_context(basic_string_view format_str, + int next_arg_id = 0) : format_str_(format_str), next_arg_id_(next_arg_id) {} /// Returns an iterator to the beginning of the format string range being @@ -840,16 +840,17 @@ template class basic_format_parse_context { }; FMT_EXPORT -using format_parse_context = basic_format_parse_context; +template using basic_format_parse_context = parse_context; +using format_parse_context = parse_context; namespace detail { // A parse context with extra data used only in compile-time checks. template -class compile_parse_context : public basic_format_parse_context { +class compile_parse_context : public parse_context { private: int num_args_; const type* types_; - using base = basic_format_parse_context; + using base = parse_context; public: explicit FMT_CONSTEXPR compile_parse_context( @@ -1160,7 +1161,7 @@ template class counting_buffer : public buffer { } // namespace detail template -FMT_CONSTEXPR void basic_format_parse_context::do_check_arg_id(int id) { +FMT_CONSTEXPR void parse_context::do_check_arg_id(int id) { // Argument id is only checked at compile-time during parsing because // formatting has its own validation. if (detail::is_constant_evaluated() && @@ -1172,8 +1173,7 @@ FMT_CONSTEXPR void basic_format_parse_context::do_check_arg_id(int id) { } template -FMT_CONSTEXPR void basic_format_parse_context::check_dynamic_spec( - int arg_id) { +FMT_CONSTEXPR void parse_context::check_dynamic_spec(int arg_id) { if (detail::is_constant_evaluated() && (!FMT_GCC_VERSION || FMT_GCC_VERSION >= 1200)) { using context = detail::compile_parse_context; @@ -2047,7 +2047,7 @@ class context { using iterator = appender; using format_arg = basic_format_arg; - using parse_context_type = basic_format_parse_context; + using parse_context_type = parse_context; template using formatter_type = formatter; enum { builtin_types = FMT_BUILTIN_TYPES }; @@ -2474,7 +2474,7 @@ FMT_CONSTEXPR auto parse_arg_id(const Char* begin, const Char* end, } template struct dynamic_spec_handler { - basic_format_parse_context& ctx; + parse_context& ctx; arg_ref& ref; arg_id_kind& kind; @@ -2500,7 +2500,7 @@ template struct parse_dynamic_spec_result { template FMT_CONSTEXPR auto parse_dynamic_spec(const Char* begin, const Char* end, int& value, arg_ref& ref, - basic_format_parse_context& ctx) + parse_context& ctx) -> parse_dynamic_spec_result { FMT_ASSERT(begin != end, ""); auto kind = arg_id_kind::none; @@ -2533,8 +2533,7 @@ FMT_CONSTEXPR auto parse_dynamic_spec(const Char* begin, const Char* end, template FMT_CONSTEXPR auto parse_width(const Char* begin, const Char* end, format_specs& specs, arg_ref& width_ref, - basic_format_parse_context& ctx) - -> const Char* { + parse_context& ctx) -> const Char* { auto result = parse_dynamic_spec(begin, end, specs.width, width_ref, ctx); specs.set_dynamic_width(result.kind); return result.end; @@ -2544,8 +2543,7 @@ template FMT_CONSTEXPR auto parse_precision(const Char* begin, const Char* end, format_specs& specs, arg_ref& precision_ref, - basic_format_parse_context& ctx) - -> const Char* { + parse_context& ctx) -> const Char* { ++begin; if (begin == end) { report_error("invalid precision"); @@ -2563,8 +2561,8 @@ enum class state { start, align, sign, hash, zero, width, precision, locale }; template FMT_CONSTEXPR auto parse_format_specs(const Char* begin, const Char* end, dynamic_format_specs& specs, - basic_format_parse_context& ctx, - type arg_type) -> const Char* { + parse_context& ctx, type arg_type) + -> const Char* { auto c = '\0'; if (end - begin > 1) { auto next = to_ascii(begin[1]); @@ -2843,7 +2841,7 @@ FMT_CONSTEXPR inline auto check_char_specs(const format_specs& specs) -> bool { template FMT_VISIBILITY("hidden") // Suppress an ld warning on macOS (#3769). -FMT_CONSTEXPR auto parse_format_specs(basic_format_parse_context& ctx) +FMT_CONSTEXPR auto parse_format_specs(parse_context& ctx) -> decltype(ctx.begin()) { using context = buffered_context; using mapped_type = @@ -2870,7 +2868,7 @@ class format_string_checker { named_arg_info named_args_[NUM_NAMED_ARGS > 0 ? NUM_NAMED_ARGS : 1]; compile_parse_context context_; - using parse_func = const Char* (*)(basic_format_parse_context&); + using parse_func = const Char* (*)(parse_context&); parse_func parse_funcs_[NUM_ARGS > 0 ? NUM_ARGS : 1]; public: @@ -2932,8 +2930,7 @@ template struct native_formatter { public: using nonlocking = void; - template - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const Char* { + FMT_CONSTEXPR auto parse(parse_context& ctx) -> const Char* { if (ctx.begin() == ctx.end() || *ctx.begin() == '}') return ctx.begin(); auto end = parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx, TYPE); if (const_check(TYPE == type::char_type)) check_char_specs(specs_); diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 1ce12c41..5aca22f9 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -2098,8 +2098,7 @@ struct formatter : private formatter { bool use_tm_formatter_ = false; public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { + FMT_CONSTEXPR auto parse(parse_context& ctx) -> decltype(ctx.begin()) { auto it = ctx.begin(), end = ctx.end(); if (it != end && *it == 'L') { ++it; @@ -2128,8 +2127,7 @@ struct formatter : private formatter { bool use_tm_formatter_ = false; public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { + FMT_CONSTEXPR auto parse(parse_context& ctx) -> decltype(ctx.begin()) { auto it = ctx.begin(), end = ctx.end(); use_tm_formatter_ = it != end && *it != '}'; return use_tm_formatter_ ? formatter::parse(ctx) : it; @@ -2154,8 +2152,7 @@ struct formatter : private formatter { bool use_tm_formatter_ = false; public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { + FMT_CONSTEXPR auto parse(parse_context& ctx) -> decltype(ctx.begin()) { auto it = ctx.begin(), end = ctx.end(); if (it != end && *it == 'L') { ++it; @@ -2184,8 +2181,7 @@ struct formatter : private formatter { bool use_tm_formatter_ = false; public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { + FMT_CONSTEXPR auto parse(parse_context& ctx) -> decltype(ctx.begin()) { auto it = ctx.begin(), end = ctx.end(); use_tm_formatter_ = it != end && *it != '}'; return use_tm_formatter_ ? formatter::parse(ctx) : it; @@ -2209,8 +2205,7 @@ struct formatter : private formatter { bool use_tm_formatter_ = false; public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { + FMT_CONSTEXPR auto parse(parse_context& ctx) -> decltype(ctx.begin()) { auto it = ctx.begin(), end = ctx.end(); use_tm_formatter_ = it != end && *it != '}'; return use_tm_formatter_ ? formatter::parse(ctx) : it; @@ -2241,8 +2236,7 @@ struct formatter, Char> { basic_string_view format_str_; public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { + FMT_CONSTEXPR auto parse(parse_context& ctx) -> decltype(ctx.begin()) { auto it = ctx.begin(), end = ctx.end(); if (it == end || *it == '}') return it; @@ -2327,8 +2321,7 @@ template struct formatter { } public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { + FMT_CONSTEXPR auto parse(parse_context& ctx) -> decltype(ctx.begin()) { auto it = ctx.begin(), end = ctx.end(); if (it == end || *it == '}') return it; diff --git a/include/fmt/compile.h b/include/fmt/compile.h index 1da29240..235b0922 100644 --- a/include/fmt/compile.h +++ b/include/fmt/compile.h @@ -71,7 +71,7 @@ constexpr const auto& get([[maybe_unused]] const T& first, return detail::get(rest...); } -#if FMT_USE_NONTYPE_TEMPLATE_ARGS +# if FMT_USE_NONTYPE_TEMPLATE_ARGS template constexpr auto get_arg_index_by_name(basic_string_view name) -> int { if constexpr (is_statically_named_arg()) { @@ -82,14 +82,14 @@ constexpr auto get_arg_index_by_name(basic_string_view name) -> int { (void)name; // Workaround an MSVC bug about "unused" parameter. return -1; } -#endif +# endif template FMT_CONSTEXPR auto get_arg_index_by_name(basic_string_view name) -> int { -#if FMT_USE_NONTYPE_TEMPLATE_ARGS +# if FMT_USE_NONTYPE_TEMPLATE_ARGS if constexpr (sizeof...(Args) > 0) return get_arg_index_by_name<0, Args...>(name); -#endif +# endif (void)name; return -1; } diff --git a/include/fmt/format.h b/include/fmt/format.h index 77797801..3a69d659 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1039,7 +1039,7 @@ template class generic_context { public: using char_type = Char; using iterator = OutputIt; - using parse_context_type = basic_format_parse_context; + using parse_context_type = parse_context; template using formatter_type = formatter; enum { builtin_types = FMT_BUILTIN_TYPES }; @@ -3693,7 +3693,7 @@ template struct default_arg_formatter { void operator()(typename basic_format_arg::handle h) { // Use a null locale since the default format must be unlocalized. - auto parse_ctx = basic_format_parse_context({}); + auto parse_ctx = parse_context({}); auto format_ctx = context(out, {}, {}); h.format(parse_ctx, format_ctx); } @@ -4099,8 +4099,7 @@ template struct nested_formatter { public: constexpr nested_formatter() : width_(0) {} - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { + FMT_CONSTEXPR auto parse(parse_context& ctx) -> decltype(ctx.begin()) { auto it = ctx.begin(), end = ctx.end(); if (it == end) return it; auto specs = format_specs(); @@ -4179,46 +4178,45 @@ FMT_END_EXPORT namespace detail { template struct format_handler { - basic_format_parse_context parse_context; - buffered_context context; + parse_context parse_ctx; + buffered_context ctx; void on_text(const Char* begin, const Char* end) { - copy_noinline(begin, end, context.out()); + copy_noinline(begin, end, ctx.out()); } - FMT_CONSTEXPR auto on_arg_id() -> int { return parse_context.next_arg_id(); } + FMT_CONSTEXPR auto on_arg_id() -> int { return parse_ctx.next_arg_id(); } FMT_CONSTEXPR auto on_arg_id(int id) -> int { - parse_context.check_arg_id(id); + parse_ctx.check_arg_id(id); return id; } FMT_CONSTEXPR auto on_arg_id(basic_string_view id) -> int { - parse_context.check_arg_id(id); - int arg_id = context.arg_id(id); + parse_ctx.check_arg_id(id); + int arg_id = ctx.arg_id(id); if (arg_id < 0) report_error("argument not found"); return arg_id; } FMT_INLINE void on_replacement_field(int id, const Char*) { - context.arg(id).visit(default_arg_formatter{context.out()}); + ctx.arg(id).visit(default_arg_formatter{ctx.out()}); } auto on_format_specs(int id, const Char* begin, const Char* end) -> const Char* { - auto arg = get_arg(context, id); + auto arg = get_arg(ctx, id); // Not using a visitor for custom types gives better codegen. - if (arg.format_custom(begin, parse_context, context)) - return parse_context.begin(); + if (arg.format_custom(begin, parse_ctx, ctx)) return parse_ctx.begin(); auto specs = dynamic_format_specs(); - begin = parse_format_specs(begin, end, specs, parse_context, arg.type()); + begin = parse_format_specs(begin, end, specs, parse_ctx, arg.type()); if (specs.dynamic()) { handle_dynamic_spec(specs.dynamic_width(), specs.width, specs.width_ref, - context); + ctx); handle_dynamic_spec(specs.dynamic_precision(), specs.precision, - specs.precision_ref, context); + specs.precision_ref, ctx); } - arg.visit(arg_formatter{context.out(), specs, context.locale()}); + arg.visit(arg_formatter{ctx.out(), specs, ctx.locale()}); return begin; } @@ -4232,8 +4230,7 @@ void vformat_to(buffer& buf, basic_string_view fmt, if (fmt.size() == 2 && equal2(fmt.data(), "{}")) return args.get(0).visit(default_arg_formatter{out}); parse_format_string( - fmt, format_handler{basic_format_parse_context(fmt), - {out, args, loc}}); + fmt, format_handler{parse_context(fmt), {out, args, loc}}); } FMT_BEGIN_EXPORT diff --git a/include/fmt/printf.h b/include/fmt/printf.h index a5964b9a..09da6481 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -33,7 +33,7 @@ template class basic_printf_context { public: using char_type = Char; - using parse_context_type = basic_format_parse_context; + using parse_context_type = parse_context; template using formatter_type = printf_formatter; enum { builtin_types = 1 }; @@ -302,7 +302,7 @@ class printf_arg_formatter : public arg_formatter { } void operator()(typename basic_format_arg::handle handle) { - auto parse_ctx = basic_format_parse_context({}); + auto parse_ctx = parse_context({}); handle.format(parse_ctx, context_); } }; @@ -421,7 +421,7 @@ void vprintf(buffer& buf, basic_string_view format, using iterator = basic_appender; auto out = iterator(buf); auto context = basic_printf_context(out, args); - auto parse_ctx = basic_format_parse_context(format); + auto parse_ctx = parse_context(format); // Returns the argument with specified index or, if arg_index is -1, the next // argument. diff --git a/include/fmt/std.h b/include/fmt/std.h index 1e69b8b2..9a4a0b48 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -508,8 +508,7 @@ template struct formatter { public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { + FMT_CONSTEXPR auto parse(parse_context& ctx) -> decltype(ctx.begin()) { return ctx.begin(); } @@ -530,8 +529,7 @@ struct formatter< bool with_typename_ = false; public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { + FMT_CONSTEXPR auto parse(parse_context& ctx) -> decltype(ctx.begin()) { auto it = ctx.begin(); auto end = ctx.end(); if (it == end || *it == '}') return it; @@ -659,8 +657,7 @@ template struct formatter, Char> { } public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { + FMT_CONSTEXPR auto parse(parse_context& ctx) -> decltype(ctx.begin()) { if (ctx.begin() == ctx.end() || *ctx.begin() == '}') return ctx.begin(); return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx, detail::type_constant::value); diff --git a/include/fmt/xchar.h b/include/fmt/xchar.h index 35618de9..2c2c882c 100644 --- a/include/fmt/xchar.h +++ b/include/fmt/xchar.h @@ -59,7 +59,7 @@ inline auto write_loc(basic_appender out, loc_value value, FMT_BEGIN_EXPORT using wstring_view = basic_string_view; -using wformat_parse_context = basic_format_parse_context; +using wformat_parse_context = parse_context; using wformat_context = buffered_context; using wformat_args = basic_format_args; using wmemory_buffer = basic_memory_buffer;