Make write_escaped_path more portable

This commit is contained in:
Victor Zverovich 2023-11-07 05:46:15 -10:00
parent 050d41e857
commit 4548d1eae2

View File

@ -72,29 +72,21 @@ template <typename Char, typename PathChar> auto get_path_string(
return p.string<Char>(); return p.string<Char>();
} }
template <typename Char> template <typename Char, typename PathChar>
void write_escaped_path(basic_memory_buffer<Char>& quoted, void write_escaped_path(basic_memory_buffer<Char>& quoted,
const std::filesystem::path& p) { const std::filesystem::path& p,
write_escaped_string<Char>(std::back_inserter(quoted), p.string<Char>()); const std::basic_string<PathChar>& native) {
} if constexpr (std::is_same_v<Char, char> && std::is_same_v<PathChar, wchar_t>) {
# ifdef _WIN32
template <>
inline void write_escaped_path<char>(memory_buffer& quoted,
const std::filesystem::path& p) {
auto buf = basic_memory_buffer<wchar_t>(); auto buf = basic_memory_buffer<wchar_t>();
write_escaped_string<wchar_t>(std::back_inserter(buf), p.native()); write_escaped_string<wchar_t>(std::back_inserter(buf), native);
bool valid = to_utf8<wchar_t>::convert(quoted, {buf.data(), buf.size()}); bool valid = to_utf8<wchar_t>::convert(quoted, {buf.data(), buf.size()});
FMT_ASSERT(valid, "invalid utf16"); FMT_ASSERT(valid, "invalid utf16");
} } else if constexpr (std::is_same_v<Char, PathChar>) {
# endif // _WIN32
template <>
inline void write_escaped_path<std::filesystem::path::value_type>(
basic_memory_buffer<std::filesystem::path::value_type>& quoted,
const std::filesystem::path& p) {
write_escaped_string<std::filesystem::path::value_type>( write_escaped_string<std::filesystem::path::value_type>(
std::back_inserter(quoted), p.native()); std::back_inserter(quoted), native);
} else {
write_escaped_string<Char>(std::back_inserter(quoted), p.string<Char>());
}
} }
} // namespace detail } // namespace detail
@ -134,7 +126,7 @@ template <typename Char> struct formatter<std::filesystem::path, Char> {
return detail::write(ctx.out(), basic_string_view<Char>(s), specs); return detail::write(ctx.out(), basic_string_view<Char>(s), specs);
} }
auto quoted = basic_memory_buffer<Char>(); auto quoted = basic_memory_buffer<Char>();
detail::write_escaped_path(quoted, p); detail::write_escaped_path(quoted, p, p.native());
return detail::write(ctx.out(), return detail::write(ctx.out(),
basic_string_view<Char>(quoted.data(), quoted.size()), basic_string_view<Char>(quoted.data(), quoted.size()),
specs); specs);