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<Char>() 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.
This commit is contained in:
NgoQuocViet2001 2026-10-24 22:39:30 +07:00
parent 0fc31633e7
commit adb4f149c0
2 changed files with 5 additions and 0 deletions

View File

@ -107,6 +107,8 @@ auto get_path_string(const std::filesystem::path& p,
if constexpr (std::is_same_v<Char, char> &&
std::is_same_v<PathChar, wchar_t>) {
return to_utf8<wchar_t>(native, to_utf8_error_policy::wtf);
} else if constexpr (std::is_same_v<Char, PathChar>) {
return native;
} else {
return p.string<Char>();
}

View File

@ -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")),