From c20874c28fabbbd0dad4c4d3202cdd803f2484bc Mon Sep 17 00:00:00 2001 From: Camille Bordignon Date: Tue, 8 Dec 2020 17:56:53 +0100 Subject: [PATCH] Reenable support for fallback formatter in join (#2040) (#2050) --- include/fmt/format.h | 23 ++++++++++++++++++----- test/ostream-test.cc | 5 +++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 49fd93d2..e8fe34c1 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3720,20 +3720,33 @@ struct arg_join : detail::view { }; template -struct formatter, Char> - : formatter::value_type, Char> { +struct formatter, Char> { + private: + using value_type = typename std::iterator_traits::value_type; + using formatter_type = + conditional_t::value, + formatter, + detail::fallback_formatter>; + + formatter_type value_formatter_; + + public: + template + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { + return value_formatter_.parse(ctx); + } + template auto format(const arg_join& value, FormatContext& ctx) -> decltype(ctx.out()) { - using base = formatter::value_type, Char>; auto it = value.begin; auto out = ctx.out(); if (it != value.end) { - out = base::format(*it++, ctx); + out = value_formatter_.format(*it++, ctx); while (it != value.end) { out = detail::copy_str(value.sep.begin(), value.sep.end(), out); ctx.advance_to(out); - out = base::format(*it++, ctx); + out = value_formatter_.format(*it++, ctx); } } return out; diff --git a/test/ostream-test.cc b/test/ostream-test.cc index 7642c94b..03e64392 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -188,6 +188,11 @@ TEST(OStreamTest, Join) { EXPECT_EQ("1, 2, 3", fmt::format("{}", fmt::join(v, v + 3, ", "))); } +TEST(OStreamTest, JoinFallbackFormatter) { + auto strs = std::vector{TestString("foo"), TestString("bar")}; + EXPECT_EQ("foo, bar", fmt::format("{}", fmt::join(strs, ", "))); +} + #if FMT_USE_CONSTEXPR TEST(OStreamTest, ConstexprString) { EXPECT_EQ("42", format(FMT_STRING("{}"), std::string("42")));