Skip to content

Commit

Permalink
Added quotes for strings in ranges and tuple likes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Remotion committed Jun 7, 2018
1 parent 1b8a7f8 commit 2db6b46
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
39 changes: 29 additions & 10 deletions include/fmt/ranges.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,33 @@ void for_each(Tuple &&tup, F &&f) {
const auto indexes = get_indexes(tup);
for_each(indexes, std::forward<Tuple>(tup), std::forward<F>(f));
}

template<typename Arg>
FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&,
typename std::enable_if<!is_like_std_string<typename std::decay<Arg>::type>::value>::type* = nullptr) {
return add_space ? " {}" : "{}";
}

template<typename Arg>
FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&,
typename std::enable_if<is_like_std_string<typename std::decay<Arg>::type>::value>::type* = nullptr) {
return add_space ? " \"{}\"" : "\"{}\"";
}

FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const char*) {
return add_space ? " \"{}\"" : "\"{}\"";
}
FMT_CONSTEXPR const wchar_t* format_str_quoted(bool add_space, const wchar_t*) {
return add_space ? L" \"{}\"" : L"\"{}\"";
}

FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const char) {
return add_space ? " '{}'" : "'{}'";
}
FMT_CONSTEXPR const wchar_t* format_str_quoted(bool add_space, const wchar_t) {
return add_space ? L" '{}'" : L"'{}'";
}

} // namespace internal

template <typename T>
Expand All @@ -185,11 +212,7 @@ struct formatter<TupleT, Char,
}
internal::copy(formatting.delimiter, out);
}
if (formatting.add_delimiter_spaces && i > 0) {
format_to(out, " {}", v);
} else {
format_to(out, "{}", v);
}
format_to(out, internal::format_str_quoted((formatting.add_delimiter_spaces && i > 0), v), v);
++i;
}

Expand Down Expand Up @@ -252,11 +275,7 @@ struct formatter<RangeT, Char,
}
internal::copy(formatting.delimiter, out);
}
if (formatting.add_delimiter_spaces && i > 0) {
format_to(out, " {}", *it);
} else {
format_to(out, "{}", *it);
}
format_to(out, internal::format_str_quoted((formatting.add_delimiter_spaces && i > 0), *it), *it);
if (++i > formatting.range_length_limit) {
format_to(out, " ... <other elements>");
break;
Expand Down
8 changes: 4 additions & 4 deletions test/ranges-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ TEST(RangesTest, FormatVector2) {

TEST(RangesTest, FormatMap) {
std::map<std::string, int32_t> simap{{"one", 1}, {"two", 2}};
EXPECT_EQ("{(one, 1), (two, 2)}", fmt::format("{}", simap));
EXPECT_EQ("{(\"one\", 1), (\"two\", 2)}", fmt::format("{}", simap));
}

TEST(RangesTest, FormatPair) {
Expand All @@ -42,8 +42,8 @@ TEST(RangesTest, FormatPair) {

TEST(RangesTest, FormatTuple) {
std::tuple<int64_t, float, std::string> tu1{42, 3.14159265358979f,
"this is tuple"};
EXPECT_EQ("(42, 3.14159, this is tuple)", fmt::format("{}", tu1));
"this is tuple", 'i'};
EXPECT_EQ("(42, 3.14159, \"this is tuple\", 'i')", fmt::format("{}", tu1));
}

/// Check if 'if constexpr' is supported.
Expand Down Expand Up @@ -81,7 +81,7 @@ struct tuple_element<N, my_struct> {

TEST(RangesTest, FormatStruct) {
my_struct mst{13, "my struct"};
EXPECT_EQ("(13, my struct)", fmt::format("{}", mst));
EXPECT_EQ("(13, \"my struct\")", fmt::format("{}", mst));
}

#endif // (__cplusplus > 201402L) || (defined(_MSVC_LANG) && _MSVC_LANG >
Expand Down

0 comments on commit 2db6b46

Please sign in to comment.