mirror of
https://github.com/fmtlib/fmt.git
synced 2026-11-09 14:50:24 +08:00
Fix generic path format when formatting to wide strings (#4999)
* 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.
* Keep generic path format when converting to wide strings on POSIX
This commit is contained in:
parent
7f7a16a131
commit
6864349b74
@ -102,19 +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>) {
|
||||
@ -126,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>());
|
||||
}
|
||||
}
|
||||
|
||||
@ -337,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);
|
||||
|
||||
@ -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
|
||||
@ -32,9 +33,18 @@ 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");
|
||||
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")),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user