From fae6f7e0816e37f83a28a1c2d913b764f2352796 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 26 Nov 2022 08:50:46 -0800 Subject: [PATCH] Optimize range formatter --- include/fmt/core.h | 4 ++-- include/fmt/ranges.h | 25 +++++++++---------------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index eea2ebcb..5b8bf765 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -3121,8 +3121,8 @@ struct formatter = 0> - FMT_CONSTEXPR void set_debug_format() { - specs_.type = presentation_type::debug; + FMT_CONSTEXPR void set_debug_format(bool set = true) { + specs_.type = set ? presentation_type::debug : presentation_type::none; } template diff --git a/include/fmt/ranges.h b/include/fmt/ranges.h index 2555b7e9..42ba01c2 100644 --- a/include/fmt/ranges.h +++ b/include/fmt/ranges.h @@ -440,20 +440,20 @@ struct range_formatter< detail::string_literal{}; template - FMT_CONSTEXPR static auto maybe_set_debug_format(U& u, int) - -> decltype(u.set_debug_format()) { - u.set_debug_format(); + FMT_CONSTEXPR static auto maybe_set_debug_format(U& u, bool set) + -> decltype(u.set_debug_format(set)) { + u.set_debug_format(set); } template FMT_CONSTEXPR static void maybe_set_debug_format(U&, ...) {} - FMT_CONSTEXPR void maybe_set_debug_format() { - maybe_set_debug_format(underlying_, 0); + FMT_CONSTEXPR void maybe_set_debug_format(bool set) { + maybe_set_debug_format(underlying_, set); } public: - FMT_CONSTEXPR range_formatter() {} + FMT_CONSTEXPR range_formatter() { maybe_set_debug_format(true); } FMT_CONSTEXPR auto underlying() -> detail::range_formatter_type& { return underlying_; @@ -473,25 +473,18 @@ struct range_formatter< FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { auto it = ctx.begin(); auto end = ctx.end(); - if (it == end || *it == '}') { - maybe_set_debug_format(); - return underlying_.parse(ctx); - } - if (*it == 'n') { + if (it != end && *it == 'n') { set_brackets({}, {}); ++it; } - if (*it == '}') { - maybe_set_debug_format(); - ctx.advance_to(it); - return underlying_.parse(ctx); - } + if (it == end || *it == '}') return it; if (*it != ':') FMT_THROW(format_error("no other top-level range formatters supported")); + maybe_set_debug_format(false); custom_specs_ = true; ++it; ctx.advance_to(it);