From 645fde421dc48a02908f40de8ccbac58bdb2a103 Mon Sep 17 00:00:00 2001 From: Jin S Date: Mon, 4 Mar 2024 15:56:26 -0500 Subject: [PATCH] implement changes --- include/fmt/ranges.h | 69 ++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 37 deletions(-) diff --git a/include/fmt/ranges.h b/include/fmt/ranges.h index 6e22d115eecb..1a9cd72aa54a 100644 --- a/include/fmt/ranges.h +++ b/include/fmt/ranges.h @@ -412,42 +412,40 @@ struct range_formatter< FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { auto it = ctx.begin(); auto end = ctx.end(); + detail::maybe_set_debug_format(underlying_, true); + if (it == end) { + return underlying_.parse(ctx); + } - if (it != end && *it == 'n') { + switch (detail::to_ascii(*it)) { + case 'n': set_brackets({}, {}); ++it; - } else { - bool check_for_s = false; - if (it != end && *it == '?') { - ++it; - detail::maybe_set_debug_format(underlying_, true); - set_brackets({}, {}); - check_for_s = true; - is_debug = true; - } - if (it != end && *it == 's') { - if (!std::is_same::value) { - report_error("invalid format specifier"); - } - if (!is_debug) { - set_brackets(detail::string_literal{}, - detail::string_literal{}); - } - check_for_s = false; - is_string_format = true; - ++it; - } - if (check_for_s) { + break; + case '?': + is_debug = true; + set_brackets({}, {}); + ++it; + FMT_FALLTHROUGH; + case 's': + if (it == end || *it != 's' || !std::is_same::value) { report_error("invalid format specifier"); } + if (!is_debug) { + set_brackets(detail::string_literal{}, + detail::string_literal{}); + set_separator({}); + detail::maybe_set_debug_format(underlying_, false); + } + is_string_format = true; + ++it; + return it; } if (it != end && *it != '}') { - if (is_string_format || *it != ':') - report_error("invalid format specifier"); + if (*it != ':') report_error("invalid format specifier"); + detail::maybe_set_debug_format(underlying_, false); ++it; - } else { - if (!is_string_format) detail::maybe_set_debug_format(underlying_, true); } ctx.advance_to(it); @@ -455,23 +453,20 @@ struct range_formatter< } template ::value, bool> = true> + FMT_ENABLE_IF(std::is_same::value)> auto write_debug_string(Output& out, Iter& it, IterEnd& end) const -> Output { auto buf = basic_memory_buffer(); for (; it != end; ++it) { - auto&& item = *it; - buf.push_back(item); + buf.push_back(*it); } - format_specs spec_str{}; + format_specs spec_str; spec_str.type = presentation_type::debug; return detail::write( out, basic_string_view(buf.data(), buf.size()), spec_str); } template ::value), bool> = true> - auto write_debug_string(Output& out, Iter& it, IterEnd& end) const -> Output { - detail::ignore_unused(it); - detail::ignore_unused(end); + FMT_ENABLE_IF(!std::is_same::value)> + auto write_debug_string(Output& out, Iter&, IterEnd&) const -> Output { return out; } @@ -481,14 +476,14 @@ struct range_formatter< auto out = ctx.out(); auto it = detail::range_begin(range); auto end = detail::range_end(range); - if (is_string_format && is_debug) { + if (is_debug) { return write_debug_string(out, it, end); } out = detail::copy(opening_bracket_, out); int i = 0; for (; it != end; ++it) { - if (i > 0 && !is_string_format) out = detail::copy(separator_, out); + if (i > 0) out = detail::copy(separator_, out); ctx.advance_to(out); auto&& item = *it; out = underlying_.format(mapper.map(item), ctx);