From 72e519a4bd0758563ccfaa02ee7cd2a93a8785d9 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 22 Jun 2019 19:48:37 -0700 Subject: [PATCH] Add formatter --- include/fmt/format.h | 1 + test/format-test.cc | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index 116cd26f..f428fb6b 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -3005,6 +3005,7 @@ FMT_FORMAT_AS(float, double); FMT_FORMAT_AS(Char*, const Char*); FMT_FORMAT_AS(std::basic_string, basic_string_view); FMT_FORMAT_AS(std::nullptr_t, const void*); +FMT_FORMAT_AS(internal::std_string_view, basic_string_view); template struct formatter : formatter { diff --git a/test/format-test.cc b/test/format-test.cc index 1c292ba0..2b9b5857 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1590,9 +1590,20 @@ TEST(FormatterTest, FormatStringView) { EXPECT_EQ("", format("{}", string_view())); } -#ifdef FMT_USE_STD_STRING_VIEW +#ifdef FMT_USE_STRING_VIEW +struct string_viewable {}; + +FMT_BEGIN_NAMESPACE +template <> struct formatter : formatter { + auto format(string_viewable, format_context& ctx) -> decltype(ctx.out()) { + return formatter::format("foo", ctx); + } +}; +FMT_END_NAMESPACE + TEST(FormatterTest, FormatStdStringView) { - EXPECT_EQ("test", format("{0}", std::string_view("test"))); + EXPECT_EQ("test", format("{}", std::string_view("test"))); + EXPECT_EQ("foo", format("{}", string_viewable())); } #endif