mirror of
https://github.com/fmtlib/fmt.git
synced 2026-10-28 18:07:37 +08:00
Keep generic path format when converting to wide strings on POSIX
This commit is contained in:
parent
adb4f149c0
commit
0fe44d56d0
@ -102,21 +102,19 @@ template <int N> struct ubitint {};
|
||||
#if FMT_CPP_LIB_FILESYSTEM
|
||||
|
||||
template <typename Char, typename PathChar>
|
||||
auto get_path_string(const std::filesystem::path& p,
|
||||
const std::basic_string<PathChar>& native) {
|
||||
auto get_path_string(const std::basic_string<PathChar>& native) {
|
||||
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>();
|
||||
return std::filesystem::path(native).string<Char>();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Char, typename PathChar>
|
||||
void write_escaped_path(basic_memory_buffer<Char>& quoted,
|
||||
const std::filesystem::path& p,
|
||||
const std::basic_string<PathChar>& native) {
|
||||
if constexpr (std::is_same_v<Char, char> &&
|
||||
std::is_same_v<PathChar, wchar_t>) {
|
||||
@ -128,7 +126,8 @@ void write_escaped_path(basic_memory_buffer<Char>& quoted,
|
||||
write_escaped_string<std::filesystem::path::value_type>(
|
||||
std::back_inserter(quoted), native);
|
||||
} else {
|
||||
write_escaped_string<Char>(std::back_inserter(quoted), p.string<Char>());
|
||||
write_escaped_string<Char>(std::back_inserter(quoted),
|
||||
std::filesystem::path(native).string<Char>());
|
||||
}
|
||||
}
|
||||
|
||||
@ -339,11 +338,11 @@ template <typename Char> struct formatter<std::filesystem::path, Char> {
|
||||
detail::handle_dynamic_spec(specs.dynamic_width(), specs.width, width_ref_,
|
||||
ctx);
|
||||
if (!debug_) {
|
||||
auto s = detail::get_path_string<Char>(p, path_string);
|
||||
auto s = detail::get_path_string<Char>(path_string);
|
||||
return detail::write(ctx.out(), basic_string_view<Char>(s), specs);
|
||||
}
|
||||
auto quoted = basic_memory_buffer<Char>();
|
||||
detail::write_escaped_path(quoted, p, path_string);
|
||||
detail::write_escaped_path(quoted, path_string);
|
||||
return detail::write(ctx.out(),
|
||||
basic_string_view<Char>(quoted.data(), quoted.size()),
|
||||
specs);
|
||||
|
||||
@ -33,6 +33,13 @@ TEST(std_test, path) {
|
||||
EXPECT_EQ(fmt::format("{:?}", path("foo\"bar")), "\"foo\\\"bar\"");
|
||||
|
||||
EXPECT_EQ(fmt::format("{:g}", path("/usr/bin")), "/usr/bin");
|
||||
|
||||
// libstdc++ collapses repeated separators in the generic format.
|
||||
const path repeated = "a//b";
|
||||
EXPECT_EQ(fmt::format("{:g}", repeated), repeated.generic_string());
|
||||
EXPECT_EQ(fmt::format(L"{:g}", repeated), repeated.generic_wstring());
|
||||
EXPECT_EQ(fmt::format(L"{:?g}", repeated),
|
||||
L"\"" + repeated.generic_wstring() + L"\"");
|
||||
# ifdef _WIN32
|
||||
EXPECT_EQ(fmt::format("{}", path("C:\\foo")), "C:\\foo");
|
||||
EXPECT_EQ(fmt::format("{:g}", path("C:\\foo")), "C:/foo");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user