From 9bbb53ffcd6eaee9f263c63cd135c5a3ef50928b Mon Sep 17 00:00:00 2001 From: Jin S Date: Sat, 25 Nov 2023 19:19:13 -0500 Subject: [PATCH 1/3] add generic and native of path --- include/fmt/std.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/include/fmt/std.h b/include/fmt/std.h index 4799df1a317d..82380546fa2f 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -78,7 +78,7 @@ #if FMT_CPP_LIB_FILESYSTEM FMT_BEGIN_NAMESPACE - + namespace detail { template auto get_path_string( @@ -114,6 +114,7 @@ template struct formatter { format_specs specs_; detail::arg_ref width_ref_; bool debug_ = false; + char path_type_ = 'n'; public: FMT_CONSTEXPR void set_debug_format(bool set = true) { debug_ = set; } @@ -130,20 +131,25 @@ template struct formatter { debug_ = true; ++it; } + if (it != end && (*it == 'g' || *it == 'n')) { + path_type_ = *it++; + } return it; } template auto format(const std::filesystem::path& p, FormatContext& ctx) const { auto specs = specs_; + auto path_type = path_type_; + auto path_string = (path_type == 'n') ? p.native() : p.generic_string(); detail::handle_dynamic_spec(specs.width, width_ref_, ctx); if (!debug_) { - auto s = detail::get_path_string(p, p.native()); + auto s = detail::get_path_string(p, path_string); return detail::write(ctx.out(), basic_string_view(s), specs); } auto quoted = basic_memory_buffer(); - detail::write_escaped_path(quoted, p, p.native()); + detail::write_escaped_path(quoted, p, path_string); return detail::write(ctx.out(), basic_string_view(quoted.data(), quoted.size()), specs); From e5dd88b0e9bc77be349ce58d2068318608a2d181 Mon Sep 17 00:00:00 2001 From: Jin S Date: Sun, 26 Nov 2023 21:17:25 -0500 Subject: [PATCH 2/3] add generic and native path format spec --- include/fmt/std.h | 5 +++++ test/std-test.cc | 9 +++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/fmt/std.h b/include/fmt/std.h index 82380546fa2f..c9805acbcfce 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -141,7 +141,12 @@ template struct formatter { auto format(const std::filesystem::path& p, FormatContext& ctx) const { auto specs = specs_; auto path_type = path_type_; + # ifdef _WIN32 + auto path_string = (path_type == 'n') ? p.native() : p.generic_wstring(); + # else auto path_string = (path_type == 'n') ? p.native() : p.generic_string(); + # endif + detail::handle_dynamic_spec(specs.width, width_ref_, ctx); if (!debug_) { diff --git a/test/std-test.cc b/test/std-test.cc index dc1073b58fc8..7a81aaf9a5f5 100644 --- a/test/std-test.cc +++ b/test/std-test.cc @@ -25,15 +25,20 @@ TEST(std_test, path) { EXPECT_EQ(fmt::format("{}", path("foo\"bar")), "foo\"bar"); EXPECT_EQ(fmt::format("{:?}", path("foo\"bar")), "\"foo\\\"bar\""); + + EXPECT_EQ(fmt::format("{:n}", path("/usr/bin")), "/usr/bin"); + EXPECT_EQ(fmt::format("{:g}", path("/usr/bin")), "/usr/bin"); +# ifdef _WIN32 + EXPECT_EQ(fmt::format("{:n}", path("C:\\foo")), "C:\\foo"); + EXPECT_EQ(fmt::format("{:g}", path("C:\\foo")), "C:/foo"); -# ifdef _WIN32 EXPECT_EQ(fmt::format("{}", path( L"\x0428\x0447\x0443\x0447\x044B\x043D\x0448" L"\x0447\x044B\x043D\x0430")), "Шчучыншчына"); EXPECT_EQ(fmt::format("{}", path(L"\xd800")), "�"); EXPECT_EQ(fmt::format("{:?}", path(L"\xd800")), "\"\\ud800\""); -# endif +# endif } // Test ambiguity problem described in #2954. From 6a1c643adbc5e229c9c541e69d4a4465ffcf5537 Mon Sep 17 00:00:00 2001 From: Jin S Date: Tue, 28 Nov 2023 22:06:37 -0500 Subject: [PATCH 3/3] fix nits --- include/fmt/std.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/fmt/std.h b/include/fmt/std.h index c9805acbcfce..7b92cea5bf14 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -78,7 +78,7 @@ #if FMT_CPP_LIB_FILESYSTEM FMT_BEGIN_NAMESPACE - + namespace detail { template auto get_path_string( @@ -142,9 +142,9 @@ template struct formatter { auto specs = specs_; auto path_type = path_type_; # ifdef _WIN32 - auto path_string = (path_type == 'n') ? p.native() : p.generic_wstring(); + auto path_string = path_type == 'n' ? p.native() : p.generic_wstring(); # else - auto path_string = (path_type == 'n') ? p.native() : p.generic_string(); + auto path_string = path_type == 'n' ? p.native() : p.generic_string(); # endif detail::handle_dynamic_spec(specs.width, width_ref_,