From c3aa18000e59c5e4f6e68ad85f9009847be41e30 Mon Sep 17 00:00:00 2001 From: Vladislav Shchapov Date: Wed, 18 May 2022 18:04:47 +0500 Subject: [PATCH 1/3] Improve std::filesystem::path formatter. Signed-off-by: Vladislav Shchapov --- include/fmt/std.h | 11 +++-------- test/std-test.cc | 2 +- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/include/fmt/std.h b/include/fmt/std.h index baa30e1fd608..2a75d8ff78fd 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -19,14 +19,9 @@ #ifdef __cpp_lib_filesystem # include -template <> -struct fmt::formatter : formatter { - template - auto format(const std::filesystem::path& p, FormatContext& ctx) const -> - typename FormatContext::iterator { - return formatter::format(p.string(), ctx); - } -}; +FMT_BEGIN_NAMESPACE +template <> struct formatter : ostream_formatter {}; +FMT_END_NAMESPACE #endif FMT_BEGIN_NAMESPACE diff --git a/test/std-test.cc b/test/std-test.cc index 6cda28910b7f..dcd75d5fe77b 100644 --- a/test/std-test.cc +++ b/test/std-test.cc @@ -11,7 +11,7 @@ TEST(std_test, path) { #ifdef __cpp_lib_filesystem - EXPECT_EQ(fmt::format("{:8}", std::filesystem::path("foo")), "foo "); + EXPECT_EQ(fmt::format("{:8}", std::filesystem::path("foo")), "\"foo\" "); #endif } From 6d700ab66e3cb333ac1f7aac14f71028b6406221 Mon Sep 17 00:00:00 2001 From: Vladislav Shchapov Date: Wed, 18 May 2022 18:07:24 +0500 Subject: [PATCH 2/3] Improve xchar support for std formatters. Signed-off-by: Vladislav Shchapov --- include/fmt/std.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/fmt/std.h b/include/fmt/std.h index 2a75d8ff78fd..4a7df19c6bd6 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -20,12 +20,15 @@ # include FMT_BEGIN_NAMESPACE -template <> struct formatter : ostream_formatter {}; +template +struct formatter : basic_ostream_formatter { +}; FMT_END_NAMESPACE #endif FMT_BEGIN_NAMESPACE -template <> struct formatter : ostream_formatter {}; +template +struct formatter : basic_ostream_formatter {}; FMT_END_NAMESPACE #endif // FMT_STD_H_ From f68712dd641c33241d6587e4ea7651b6530ef1c6 Mon Sep 17 00:00:00 2001 From: Vladislav Shchapov Date: Sat, 21 May 2022 20:12:32 +0500 Subject: [PATCH 3/3] Use write_escaped_string to std::filesystem::path. Signed-off-by: Vladislav Shchapov --- include/fmt/std.h | 29 ++++++++++++++++++++++++++++- test/std-test.cc | 2 ++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/include/fmt/std.h b/include/fmt/std.h index 4a7df19c6bd6..0ba929fac54e 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -20,8 +20,35 @@ # include FMT_BEGIN_NAMESPACE + +namespace detail { + +template +void write_escaped_path(basic_memory_buffer& quoted, + const std::filesystem::path& p) { + write_escaped_string(std::back_inserter(quoted), p.string()); +} +template <> +void write_escaped_path( + basic_memory_buffer& quoted, + const std::filesystem::path& p) { + write_escaped_string( + std::back_inserter(quoted), p.native()); +} + +} // namespace detail + template -struct formatter : basic_ostream_formatter { +struct formatter + : formatter> { + template + auto format(const std::filesystem::path& p, FormatContext& ctx) const -> + typename FormatContext::iterator { + basic_memory_buffer quoted; + detail::write_escaped_path(quoted, p); + return formatter>::format( + basic_string_view(quoted.data(), quoted.size()), ctx); + } }; FMT_END_NAMESPACE #endif diff --git a/test/std-test.cc b/test/std-test.cc index dcd75d5fe77b..069c83871955 100644 --- a/test/std-test.cc +++ b/test/std-test.cc @@ -12,6 +12,8 @@ TEST(std_test, path) { #ifdef __cpp_lib_filesystem EXPECT_EQ(fmt::format("{:8}", std::filesystem::path("foo")), "\"foo\" "); + EXPECT_EQ(fmt::format("{}", std::filesystem::path("foo\"bar.txt")), + "\"foo\\\"bar.txt\""); #endif }