Skip to content

Commit

Permalink
Reenable support for fallback formatter to join (#2040)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamibo committed Dec 4, 2020
1 parent 33f9a6d commit 742abd6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
30 changes: 19 additions & 11 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3719,24 +3719,32 @@ struct arg_join : detail::view {
: begin(b), end(e), sep(s) {}
};

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

template <typename It, typename Sentinel, typename Char>
struct formatter<arg_join<It, Sentinel, Char>, Char>
: formatter<typename std::iterator_traits<It>::value_type, Char> {
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);
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);
}
}
return out;
return format_join(static_cast<base&>(*this), value, ctx);
}
};

Expand Down
13 changes: 13 additions & 0 deletions include/fmt/ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ struct fallback_formatter<T, Char, enable_if_t<is_streamable<T, Char>::value>>
return std::copy(buffer.begin(), buffer.end(), ctx.out());
}
};

template <typename It, typename Sentinel, typename Char>
struct fallback_formatter<arg_join<It, Sentinel, Char>, Char>
: fallback_formatter<typename std::iterator_traits<It>::value_type, Char> {
template <typename FormatContext>
auto format(const arg_join<It, Sentinel, Char>& value, FormatContext& ctx)
-> decltype(ctx.out()) {
using base =
fallback_formatter<typename std::iterator_traits<It>::value_type, Char>;
return format_join(static_cast<base&>(*this), value, ctx);
}
};

} // namespace detail

template <typename Char>
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 742abd6

Please sign in to comment.