From 4548d1eae20f456a3e8119042d7dd313d0a2d851 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 7 Nov 2023 05:46:15 -1000 Subject: [PATCH] Make write_escaped_path more portable --- include/fmt/std.h | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/include/fmt/std.h b/include/fmt/std.h index 4095a387..4d1f97d2 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -72,29 +72,21 @@ template auto get_path_string( return p.string(); } -template +template void write_escaped_path(basic_memory_buffer& quoted, - const std::filesystem::path& p) { - write_escaped_string(std::back_inserter(quoted), p.string()); -} - -# ifdef _WIN32 -template <> -inline void write_escaped_path(memory_buffer& quoted, - const std::filesystem::path& p) { - auto buf = basic_memory_buffer(); - write_escaped_string(std::back_inserter(buf), p.native()); - bool valid = to_utf8::convert(quoted, {buf.data(), buf.size()}); - FMT_ASSERT(valid, "invalid utf16"); -} -# endif // _WIN32 - -template <> -inline void write_escaped_path( - basic_memory_buffer& quoted, - const std::filesystem::path& p) { - write_escaped_string( - std::back_inserter(quoted), p.native()); + const std::filesystem::path& p, + const std::basic_string& native) { + if constexpr (std::is_same_v && std::is_same_v) { + auto buf = basic_memory_buffer(); + write_escaped_string(std::back_inserter(buf), native); + bool valid = to_utf8::convert(quoted, {buf.data(), buf.size()}); + FMT_ASSERT(valid, "invalid utf16"); + } else if constexpr (std::is_same_v) { + write_escaped_string( + std::back_inserter(quoted), native); + } else { + write_escaped_string(std::back_inserter(quoted), p.string()); + } } } // namespace detail @@ -134,7 +126,7 @@ template struct formatter { return detail::write(ctx.out(), basic_string_view(s), specs); } auto quoted = basic_memory_buffer(); - detail::write_escaped_path(quoted, p); + detail::write_escaped_path(quoted, p, p.native()); return detail::write(ctx.out(), basic_string_view(quoted.data(), quoted.size()), specs);