Use replacement character in path

This commit is contained in:
Victor Zverovich 2023-07-22 06:56:52 -07:00
parent dbabb305c3
commit 3dec65b7fd
3 changed files with 20 additions and 18 deletions

View File

@ -1421,11 +1421,11 @@ template <typename WChar, typename Buffer = memory_buffer> class to_utf8 {
public:
to_utf8() {}
explicit to_utf8(basic_string_view<WChar> s) {
explicit to_utf8(basic_string_view<WChar> s,
to_utf8_error_policy policy = to_utf8_error_policy::abort) {
static_assert(sizeof(WChar) == 2 || sizeof(WChar) == 4,
"Expect utf16 or utf32");
if (!convert(s))
if (!convert(s, policy))
FMT_THROW(std::runtime_error(sizeof(WChar) == 2 ? "invalid utf16"
: "invalid utf32"));
}
@ -1437,8 +1437,9 @@ template <typename WChar, typename Buffer = memory_buffer> class to_utf8 {
// Performs conversion returning a bool instead of throwing exception on
// conversion error. This method may still throw in case of memory allocation
// error.
bool convert(basic_string_view<WChar> s) {
if (!convert(buffer_, s)) return false;
bool convert(basic_string_view<WChar> s,
to_utf8_error_policy policy = to_utf8_error_policy::abort) {
if (!convert(buffer_, s, policy)) return false;
buffer_.push_back(0);
return true;
}
@ -1453,11 +1454,11 @@ template <typename WChar, typename Buffer = memory_buffer> class to_utf8 {
if (p == s.end() || (c & 0xfc00) != 0xd800 || (*p & 0xfc00) != 0xdc00) {
if (policy == to_utf8_error_policy::abort) return false;
buf.append(string_view("<EFBFBD>"));
--p;
} else {
c = (c << 10) + static_cast<uint32_t>(*p) - 0x35fdc00;
}
}
if (c < 0x80) {
} else if (c < 0x80) {
buf.push_back(static_cast<char>(c));
} else if (c < 0x800) {
buf.push_back(static_cast<char>(0xc0 | (c >> 6)));

View File

@ -74,7 +74,7 @@ void write_escaped_path(basic_memory_buffer<Char>& quoted,
# ifdef _WIN32
template <>
auto get_path_string<char>(const std::filesystem::path& p) {
return to_utf8<wchar_t>(p.native());
return to_utf8<wchar_t>(p.native(), to_utf8_error_policy::replace);
}
template <>

View File

@ -15,23 +15,23 @@
#include "fmt/ranges.h"
#include "gtest-extra.h" // StartsWith
using testing::StartsWith;
#ifdef __cpp_lib_filesystem
TEST(std_test, path) {
EXPECT_EQ(fmt::format("{:8}", std::filesystem::path("foo")), "foo ");
EXPECT_EQ(fmt::format("{}", std::filesystem::path("foo\"bar.txt")),
"foo\"bar.txt");
EXPECT_EQ(fmt::format("{:?}", std::filesystem::path("foo\"bar.txt")),
"\"foo\\\"bar.txt\"");
using std::filesystem::path;
EXPECT_EQ(fmt::format("{}", path("/usr/bin")), "/usr/bin");
EXPECT_EQ(fmt::format("{:?}", path("/usr/bin")), "\"/usr/bin\"");
EXPECT_EQ(fmt::format("{:8}", path("foo")), "foo ");
EXPECT_EQ(fmt::format("{}", path("foo\"bar")), "foo\"bar");
EXPECT_EQ(fmt::format("{:?}", path("foo\"bar")), "\"foo\\\"bar\"");
# ifdef _WIN32
EXPECT_EQ(fmt::format("{}", std::filesystem::path(
EXPECT_EQ(fmt::format("{}", path(
L"\x0428\x0447\x0443\x0447\x044B\x043D\x0448"
L"\x0447\x044B\x043D\x0430")),
"Шчучыншчына");
EXPECT_EQ(fmt::format("{:?}", std::filesystem::path(L"\xd800")),
"\"\\ud800\"");
EXPECT_EQ(fmt::format("{}", path(L"\xd800")), "<EFBFBD>");
EXPECT_EQ(fmt::format("{:?}", path(L"\xd800")), "\"\\ud800\"");
# endif
}
@ -191,6 +191,7 @@ const char* my_exception::what() const noexcept { return msg.c_str(); }
} // namespace my_ns1
TEST(std_test, exception) {
using testing::StartsWith;
exception_test<std::exception>();
exception_test<std::runtime_error>();