diff --git a/include/fmt/ranges.h b/include/fmt/ranges.h index e516bc95..c21b6924 100644 --- a/include/fmt/ranges.h +++ b/include/fmt/ranges.h @@ -644,23 +644,18 @@ struct formatter< public: FMT_CONSTEXPR auto parse(parse_context& ctx) -> const Char* { - return underlying_.parse(ctx); + auto it = underlying_.parse(ctx); + if FMT_CONSTEXPR20 (range_format_kind::value == + range_format::debug_string) + underlying_.set_debug_format(); + return it; } template auto format(range_type& range, FormatContext& ctx) const -> decltype(ctx.out()) { - auto out = ctx.out(); - if FMT_CONSTEXPR20 (range_format_kind::value == - range_format::debug_string) { - *out++ = '"'; - } - out = underlying_.format( + return underlying_.format( string_type{detail::range_begin(range), detail::range_end(range)}, ctx); - if FMT_CONSTEXPR20 (range_format_kind::value == - range_format::debug_string) - *out++ = '"'; - return out; } }; diff --git a/test/ranges-test.cc b/test/ranges-test.cc index e268b5c6..d3ca6498 100644 --- a/test/ranges-test.cc +++ b/test/ranges-test.cc @@ -348,6 +348,10 @@ TEST(ranges_test, range_format_string) { TEST(ranges_test, range_format_debug_string) { const vector_debug_string v{'f', 'o', 'o'}; EXPECT_EQ(fmt::format("{}", v), "\"foo\""); + EXPECT_EQ(fmt::format("{}", vector_debug_string{'\n'}), "\"\\n\""); + EXPECT_EQ(fmt::format("{}", vector_debug_string{'\\'}), "\"\\\\\""); + EXPECT_EQ(fmt::format("{}", vector_debug_string{'\"'}), "\"\\\"\""); + EXPECT_EQ(fmt::format("{:8}", vector_debug_string{'a'}), "\"a\" "); } // A range that provides non-const only begin()/end() to test fmt::join