Skip to content

Commit 120a969

Browse files
committed
Added quotes for strings in ranges and tuple likes.
1 parent 1b8a7f8 commit 120a969

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

include/fmt/ranges.h

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,33 @@ void for_each(Tuple &&tup, F &&f) {
162162
const auto indexes = get_indexes(tup);
163163
for_each(indexes, std::forward<Tuple>(tup), std::forward<F>(f));
164164
}
165+
166+
template<typename Arg>
167+
FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&,
168+
typename std::enable_if<!is_like_std_string<typename std::decay<Arg>::type>::value>::type* = nullptr) {
169+
return add_space ? " {}" : "{}";
170+
}
171+
172+
template<typename Arg>
173+
FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&,
174+
typename std::enable_if<is_like_std_string<typename std::decay<Arg>::type>::value>::type* = nullptr) {
175+
return add_space ? " \"{}\"" : "\"{}\"";
176+
}
177+
178+
FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const char*) {
179+
return add_space ? " \"{}\"" : "\"{}\"";
180+
}
181+
FMT_CONSTEXPR const wchar_t* format_str_quoted(bool add_space, const wchar_t*) {
182+
return add_space ? L" \"{}\"" : L"\"{}\"";
183+
}
184+
185+
FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const char) {
186+
return add_space ? " '{}'" : "'{}'";
187+
}
188+
FMT_CONSTEXPR const wchar_t* format_str_quoted(bool add_space, const wchar_t) {
189+
return add_space ? L" '{}'" : L"'{}'";
190+
}
191+
165192
} // namespace internal
166193

167194
template <typename T>
@@ -185,11 +212,7 @@ struct formatter<TupleT, Char,
185212
}
186213
internal::copy(formatting.delimiter, out);
187214
}
188-
if (formatting.add_delimiter_spaces && i > 0) {
189-
format_to(out, " {}", v);
190-
} else {
191-
format_to(out, "{}", v);
192-
}
215+
format_to(out, internal::format_str_quoted((formatting.add_delimiter_spaces && i > 0), v), v);
193216
++i;
194217
}
195218

@@ -252,11 +275,7 @@ struct formatter<RangeT, Char,
252275
}
253276
internal::copy(formatting.delimiter, out);
254277
}
255-
if (formatting.add_delimiter_spaces && i > 0) {
256-
format_to(out, " {}", *it);
257-
} else {
258-
format_to(out, "{}", *it);
259-
}
278+
format_to(out, internal::format_str_quoted((formatting.add_delimiter_spaces && i > 0), *it), *it);
260279
if (++i > formatting.range_length_limit) {
261280
format_to(out, " ... <other elements>");
262281
break;

test/ranges-test.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ TEST(RangesTest, FormatVector2) {
3232

3333
TEST(RangesTest, FormatMap) {
3434
std::map<std::string, int32_t> simap{{"one", 1}, {"two", 2}};
35-
EXPECT_EQ("{(one, 1), (two, 2)}", fmt::format("{}", simap));
35+
EXPECT_EQ("{(\"one\", 1), (\"two\", 2)}", fmt::format("{}", simap));
3636
}
3737

3838
TEST(RangesTest, FormatPair) {
@@ -41,9 +41,9 @@ TEST(RangesTest, FormatPair) {
4141
}
4242

4343
TEST(RangesTest, FormatTuple) {
44-
std::tuple<int64_t, float, std::string> tu1{42, 3.14159265358979f,
45-
"this is tuple"};
46-
EXPECT_EQ("(42, 3.14159, this is tuple)", fmt::format("{}", tu1));
44+
std::tuple<int64_t, float, std::string, char> tu1{42, 3.14159265358979f,
45+
"this is tuple", 'i'};
46+
EXPECT_EQ("(42, 3.14159, \"this is tuple\", 'i')", fmt::format("{}", tu1));
4747
}
4848

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

8282
TEST(RangesTest, FormatStruct) {
8383
my_struct mst{13, "my struct"};
84-
EXPECT_EQ("(13, my struct)", fmt::format("{}", mst));
84+
EXPECT_EQ("(13, \"my struct\")", fmt::format("{}", mst));
8585
}
8686

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

0 commit comments

Comments
 (0)