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

Ranges wide strings support #2236

Merged
merged 3 commits into from
Apr 16, 2021
Merged
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
2 changes: 1 addition & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3720,7 +3720,7 @@ struct formatter<arg_join<It, Sentinel, Char>, Char> {
private:
using value_type = typename std::iterator_traits<It>::value_type;
using formatter_type =
conditional_t<has_formatter<value_type, format_context>::value,
conditional_t<has_formatter<value_type, buffer_context<Char>>::value,
formatter<value_type, Char>,
detail::fallback_formatter<value_type, Char>>;

Expand Down
6 changes: 6 additions & 0 deletions include/fmt/ranges.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ OutputIterator copy(char ch, OutputIterator out) {
return out;
}

template <typename OutputIterator>
OutputIterator copy(wchar_t ch, OutputIterator out) {
*out++ = ch;
return out;
}

/// Return true value if T has std::string interface, like std::string_view.
template <typename T> class is_like_std_string {
template <typename U>
Expand Down
19 changes: 19 additions & 0 deletions test/ranges-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,25 @@ TEST(RangesTest, JoinTuple) {
EXPECT_EQ("4", fmt::format("{}", fmt::join(t4, "/")));
}

TEST(RangesTest, WideStringJoinTuple) {
// Value tuple args
std::tuple<wchar_t, int, float> t1 = std::make_tuple('a', 1, 2.0f);
EXPECT_EQ(L"(a, 1, 2)", fmt::format(L"({})", fmt::join(t1, L", ")));

// Testing lvalue tuple args
int x = 4;
std::tuple<wchar_t, int&> t2{'b', x};
EXPECT_EQ(L"b + 4", fmt::format(L"{}", fmt::join(t2, L" + ")));

// Empty tuple
std::tuple<> t3;
EXPECT_EQ(L"", fmt::format(L"{}", fmt::join(t3, L"|")));

// Single element tuple
std::tuple<float> t4{4.0f};
EXPECT_EQ(L"4", fmt::format(L"{}", fmt::join(t4, L"/")));
}

TEST(RangesTest, JoinInitializerList) {
EXPECT_EQ("1, 2, 3", fmt::format("{}", fmt::join({1, 2, 3}, ", ")));
EXPECT_EQ("fmt rocks !",
Expand Down