Skip to content

Commit

Permalink
Reenable support for fallback formatter to join (fmtlib#2040)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamibo committed Dec 7, 2020
1 parent a6fafe2 commit 94383a5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
22 changes: 17 additions & 5 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3720,24 +3720,36 @@ struct arg_join : detail::view {
};

template <typename It, typename Sentinel, typename Char>
struct formatter<arg_join<It, Sentinel, Char>, Char>
: formatter<typename std::iterator_traits<It>::value_type, Char> {
struct formatter<arg_join<It, Sentinel, Char>, Char> {
using value_type = typename std::iterator_traits<It>::value_type;
using formatter_type =
conditional_t<has_formatter<value_type, format_context>::value,
formatter<value_type, Char>,
detail::fallback_formatter<value_type, Char>>;

template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
return formatting.parse(ctx);
}

template <typename FormatContext>
auto format(const arg_join<It, Sentinel, Char>& value, FormatContext& ctx)
-> decltype(ctx.out()) {
using base = formatter<typename std::iterator_traits<It>::value_type, Char>;
auto it = value.begin;
auto out = ctx.out();
if (it != value.end) {
out = base::format(*it++, ctx);
out = formatting.format(*it++, ctx);
while (it != value.end) {
out = detail::copy_str<Char>(value.sep.begin(), value.sep.end(), out);
ctx.advance_to(out);
out = base::format(*it++, ctx);
out = formatting.format(*it++, ctx);
}
}
return out;
}

private:
formatter_type formatting;
};

/**
Expand Down
5 changes: 5 additions & 0 deletions test/ostream-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>{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")));
Expand Down

0 comments on commit 94383a5

Please sign in to comment.