Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix join for specific types (#1981) #2034

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3674,21 +3674,39 @@ struct arg_join : detail::view {
: begin(b), end(e), sep(s) {}
};

template <typename Char>
using iterator_arg_mapper = detail::arg_mapper<FMT_BUFFER_CONTEXT(Char)>;

template <typename It, typename Char>
using iterator_format_type =
decltype(std::declval<iterator_arg_mapper<Char>>().map(
std::declval<typename std::iterator_traits<It>::value_type>()));

template <typename It, typename Char>
using iterator_formatter = conditional_t<
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely not so nice.
Also TBH I'm not sure that using FMT_BUFFER_CONTEXT is correct here.

has_formatter<iterator_format_type<It, Char>,
FMT_BUFFER_CONTEXT(Char)>::value,
formatter<iterator_format_type<It, Char>, Char>,
detail::fallback_formatter<iterator_format_type<It, Char>, Char>>;

template <typename It, typename Sentinel, typename Char>
struct formatter<arg_join<It, Sentinel, Char>, Char>
: formatter<typename std::iterator_traits<It>::value_type, Char> {
: iterator_formatter<It, 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>;
using base = iterator_formatter<It, Char>;

iterator_arg_mapper<Char> mapper;

auto it = value.begin;
auto out = ctx.out();
if (it != value.end) {
out = base::format(*it++, ctx);
out = base::format(mapper.map(*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 = base::format(mapper.map(*it++), ctx);
}
}
return out;
Expand Down
15 changes: 15 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstddef>
#include <cstring>
#include <list>
#include <memory>
Expand Down Expand Up @@ -1761,6 +1762,20 @@ TEST(FormatTest, JoinArg) {
#endif
}

TEST(FormatTest, JoinByte) {
using fmt::join;
#if __cplusplus >= 201703L
using byte = std::byte;
#else
enum class byte : unsigned char {};
#endif

std::vector<byte> v = {static_cast<byte>(0x1), static_cast<byte>(0x2)};

EXPECT_EQ("(1, 2)", format("({})", join(v, ", ")));
EXPECT_EQ("(0x1, 0x2)", format("({0:#x})", join(v, ", ")));
}

template <typename T> std::string str(const T& value) {
return fmt::format("{}", value);
}
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, JoinCustom) {
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