From adb4f149c0655c3d12e033cff11f5830b5234bf9 Mon Sep 17 00:00:00 2001 From: NgoQuocViet2001 Date: Sat, 24 Oct 2026 22:39:30 +0700 Subject: [PATCH] Fix generic path format when formatting to wide strings get_path_string used the native or generic string computed by the path formatter only when converting wchar_t to UTF-8 and fell back to p.string() otherwise, so the 'g' specifier was ignored when the output character type matched path::value_type. On Windows, fmt::format(L"{:g}", path(L"C:\\foo")) returned L"C:\\foo" instead of L"C:/foo". Use the passed string in that case, as write_escaped_path already does. --- include/fmt/std.h | 2 ++ test/std-test.cc | 3 +++ 2 files changed, 5 insertions(+) diff --git a/include/fmt/std.h b/include/fmt/std.h index 2bab00f2..a8f7c9f1 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -107,6 +107,8 @@ auto get_path_string(const std::filesystem::path& p, if constexpr (std::is_same_v && std::is_same_v) { return to_utf8(native, to_utf8_error_policy::wtf); + } else if constexpr (std::is_same_v) { + return native; } else { return p.string(); } diff --git a/test/std-test.cc b/test/std-test.cc index eaf238b4..32f77e55 100644 --- a/test/std-test.cc +++ b/test/std-test.cc @@ -14,6 +14,7 @@ #include "fmt/os.h" // fmt::system_category #include "fmt/ranges.h" +#include "fmt/xchar.h" #include "gtest-extra.h" // StartsWith #ifdef __cpp_lib_filesystem @@ -35,6 +36,8 @@ TEST(std_test, path) { # ifdef _WIN32 EXPECT_EQ(fmt::format("{}", path("C:\\foo")), "C:\\foo"); EXPECT_EQ(fmt::format("{:g}", path("C:\\foo")), "C:/foo"); + EXPECT_EQ(fmt::format(L"{}", path(L"C:\\foo")), L"C:\\foo"); + EXPECT_EQ(fmt::format(L"{:g}", path(L"C:\\foo")), L"C:/foo"); EXPECT_EQ(fmt::format("{}", path(L"\x0428\x0447\x0443\x0447\x044B\x043D\x0448" L"\x0447\x044B\x043D\x0430")),