From 120a9696b707436bc45072b9fb0ecd4ae4267902 Mon Sep 17 00:00:00 2001 From: Remotion Date: Thu, 7 Jun 2018 22:32:47 +0200 Subject: [PATCH 1/2] Added quotes for strings in ranges and tuple likes. --- include/fmt/ranges.h | 39 +++++++++++++++++++++++++++++---------- test/ranges-test.cc | 10 +++++----- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/include/fmt/ranges.h b/include/fmt/ranges.h index 770f43b7b882..a8c663b30161 100644 --- a/include/fmt/ranges.h +++ b/include/fmt/ranges.h @@ -162,6 +162,33 @@ void for_each(Tuple &&tup, F &&f) { const auto indexes = get_indexes(tup); for_each(indexes, std::forward(tup), std::forward(f)); } + +template +FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&, + typename std::enable_if::type>::value>::type* = nullptr) { + return add_space ? " {}" : "{}"; +} + +template +FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&, + typename std::enable_if::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 @@ -185,11 +212,7 @@ struct formatter 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; } @@ -252,11 +275,7 @@ struct formatter 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, " ... "); break; diff --git a/test/ranges-test.cc b/test/ranges-test.cc index 1fab6ab43528..248985b0b5f6 100644 --- a/test/ranges-test.cc +++ b/test/ranges-test.cc @@ -32,7 +32,7 @@ TEST(RangesTest, FormatVector2) { TEST(RangesTest, FormatMap) { std::map 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) { @@ -41,9 +41,9 @@ TEST(RangesTest, FormatPair) { } TEST(RangesTest, FormatTuple) { - std::tuple tu1{42, 3.14159265358979f, - "this is tuple"}; - EXPECT_EQ("(42, 3.14159, this is tuple)", fmt::format("{}", tu1)); + std::tuple tu1{42, 3.14159265358979f, + "this is tuple", 'i'}; + EXPECT_EQ("(42, 3.14159, \"this is tuple\", 'i')", fmt::format("{}", tu1)); } /// Check if 'if constexpr' is supported. @@ -81,7 +81,7 @@ struct tuple_element { 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 > From 85fc14af1c6b9d792fd84fffc09e4595a4f0e0bb Mon Sep 17 00:00:00 2001 From: Remotion Date: Fri, 8 Jun 2018 16:01:07 +0200 Subject: [PATCH 2/2] Fixed line length. --- include/fmt/ranges.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/include/fmt/ranges.h b/include/fmt/ranges.h index a8c663b30161..7e9a7f0618ae 100644 --- a/include/fmt/ranges.h +++ b/include/fmt/ranges.h @@ -165,13 +165,15 @@ void for_each(Tuple &&tup, F &&f) { template FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&, - typename std::enable_if::type>::value>::type* = nullptr) { + typename std::enable_if< + !is_like_std_string::type>::value>::type* = nullptr) { return add_space ? " {}" : "{}"; } template FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&, - typename std::enable_if::type>::value>::type* = nullptr) { + typename std::enable_if< + is_like_std_string::type>::value>::type* = nullptr) { return add_space ? " \"{}\"" : "\"{}\""; } @@ -212,7 +214,10 @@ struct formatter 0), v), v); + format_to(out, + internal::format_str_quoted( + (formatting.add_delimiter_spaces && i > 0), v), + v); ++i; } @@ -275,7 +280,10 @@ struct formatter 0), *it), *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, " ... "); break;