From 796662a612023090a421f9a397fe58b506e4829d Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Mon, 27 Dec 2021 09:38:06 -0800 Subject: [PATCH] Escape range items convertible to std::string_view --- include/fmt/ranges.h | 11 ++++++++++- test/ranges-test.cc | 11 +++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/include/fmt/ranges.h b/include/fmt/ranges.h index 71474f32..ff9054d4 100644 --- a/include/fmt/ranges.h +++ b/include/fmt/ranges.h @@ -65,7 +65,9 @@ template class is_std_string_like { public: static FMT_CONSTEXPR_DECL const bool value = - is_string::value || !std::is_void(nullptr))>::value; + is_string::value || + std::is_convertible>::value || + !std::is_void(nullptr))>::value; }; template @@ -520,6 +522,13 @@ auto write_range_entry(OutputIt out, basic_string_view str) -> OutputIt { return out; } +template >::value)> +inline auto write_range_entry(OutputIt out, const T& str) -> OutputIt { + auto sv = std_string_view(str); + return write_range_entry(out, basic_string_view(sv)); +} + template ::value)> OutputIt write_range_entry(OutputIt out, const Arg v) { diff --git a/test/ranges-test.cc b/test/ranges-test.cc index 86dd5520..63cb8c8b 100644 --- a/test/ranges-test.cc +++ b/test/ranges-test.cc @@ -350,3 +350,14 @@ TEST(ranges_test, escape_string) { "[\"\\xf4\\x8f\\xbf\\xc0\"]"); } } + +#ifdef FMT_USE_STRING_VIEW +struct convertible_to_string_view { + operator std::string_view() const { return "foo"; } +}; + +TEST(ranges_test, escape_convertible_to_string_view) { + EXPECT_EQ(fmt::format("{}", std::vector(1)), + "[\"foo\"]"); +} +#endif // FMT_USE_STRING_VIEW