mirror of
https://github.com/fmtlib/fmt.git
synced 2026-10-28 18:07:37 +08:00
Merge 0fe44d56d006d4750b41f4ebe056b9fe69902599 into f87a75e15262614ce8aa0e6a0664df9d0823fb92
This commit is contained in:
commit
cb16448e44
@ -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