diff --git a/include/fmt/ranges.h b/include/fmt/ranges.h index 02e69001..ae14907e 100644 --- a/include/fmt/ranges.h +++ b/include/fmt/ranges.h @@ -32,18 +32,22 @@ struct formatting_base { template struct formatting_range : formatting_base { + static FMT_CONSTEXPR_DECL const std::size_t range_length_limit = + FMT_RANGE_OUTPUT_LENGTH_LIMIT; // output only up to N items from the range. Char prefix = '{'; Char delimiter = ','; Char postfix = '}'; - static FMT_CONSTEXPR_DECL const bool add_spaces = false; + static FMT_CONSTEXPR_DECL const bool add_delimiter_spaces = true; + static FMT_CONSTEXPR_DECL const bool add_prepostfix_space = false; }; template struct formatting_tuple : formatting_base { - Char prefix = '['; + Char prefix = '('; Char delimiter = ','; - Char postfix = ']'; - static FMT_CONSTEXPR_DECL const bool add_spaces = false; + Char postfix = ')'; + static FMT_CONSTEXPR_DECL const bool add_delimiter_spaces = true; + static FMT_CONSTEXPR_DECL const bool add_prepostfix_space = false; }; namespace internal { @@ -92,10 +96,11 @@ template struct is_range_ : std::false_type {}; template -struct is_range_().begin()), - decltype(std::declval().end())>, - void>> : std::true_type {}; +struct is_range_().begin()), + decltype(std::declval().end())>, + void>::type> : std::true_type {}; template struct is_range { @@ -162,16 +167,20 @@ struct formatter 0) { + if (i > 0) { + if (formatting.add_prepostfix_space) { + *out++ = ' '; + } internal::copy(formatting.delimiter, out); } - if (formatting.add_spaces) { + if (formatting.add_delimiter_spaces && i > 0) { format_to(out, " {}", v); } else { format_to(out, "{}", v); } + ++i; }); - if (formatting.add_spaces) { + if (formatting.add_prepostfix_space) { *out++ = ' '; } internal::copy(formatting.postfix, out); @@ -184,8 +193,6 @@ template struct formatter< RangeT, Char, typename std::enable_if::value>::type> { - static FMT_CONSTEXPR_DECL const std::size_t range_length_limit = - FMT_RANGE_OUTPUT_LENGTH_LIMIT; // output only up to N items from the range. fmt::formatting_range formatting; @@ -201,19 +208,22 @@ struct formatter< RangeT, Char, std::size_t i = 0; for (const auto &it : values) { if (i > 0) { + if (formatting.add_prepostfix_space) { + *out++ = ' '; + } internal::copy(formatting.delimiter, out); } - if (formatting.add_spaces) { + if (formatting.add_delimiter_spaces && i > 0) { format_to(out, " {}", it); } else { format_to(out, "{}", it); } - if (++i > range_length_limit) { + if (++i > formatting.range_length_limit) { format_to(out, " ... "); break; } } - if (formatting.add_spaces) { + if (formatting.add_prepostfix_space) { *out++ = ' '; } internal::copy(formatting.postfix, out); diff --git a/test/ranges-test.cc b/test/ranges-test.cc index 49d87080..1944173e 100644 --- a/test/ranges-test.cc +++ b/test/ranges-test.cc @@ -9,10 +9,6 @@ // All Rights Reserved // {fmt} support for ranges, containers and types tuple interface. -#ifdef WIN32 -#define _CRT_SECURE_NO_WARNINGS -#endif - #include "fmt/ranges.h" #include "gtest/gtest.h" @@ -36,20 +32,21 @@ 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) { std::pair pa1{42, 3.14159265358979f}; - EXPECT_EQ("[42,3.14159]", fmt::format("{}", pa1)); + EXPECT_EQ("(42,3.14159)", fmt::format("{}", pa1)); } TEST(RangesTest, FormatTuple) { std::tuple tu1{42, 3.14159265358979f, "this is tuple"}; - EXPECT_EQ("[42,3.14159,this is tuple]", fmt::format("{}", tu1)); + EXPECT_EQ("(42,3.14159,this is tuple)", fmt::format("{}", tu1)); } +/// check if 'if constexpr' is supported. #if (__cplusplus > 201402L) || \ (defined(_MSVC_LANG) && _MSVC_LANG > 201402L && _MSC_VER >= 1910) @@ -84,7 +81,8 @@ 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 > 201402L && _MSC_VER >= 1910) \ No newline at end of file +#endif // (__cplusplus > 201402L) || (defined(_MSVC_LANG) && _MSVC_LANG > + // 201402L && _MSC_VER >= 1910) \ No newline at end of file