From 93a30a07466a05fbf2725e35cd35b83ae35941b5 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 10 May 2023 10:58:09 -0700 Subject: [PATCH] unicode_to_utf8 -> to_utf8 since both sides of conversion are Unicode --- include/fmt/chrono.h | 4 ++-- include/fmt/format.h | 6 +++--- include/fmt/std.h | 2 +- src/os.cc | 17 ++++++++--------- test/format-impl-test.cc | 4 ++-- test/os-test.cc | 8 ++++---- 6 files changed, 20 insertions(+), 21 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 55e8a506..1a2a12cf 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -377,8 +377,8 @@ auto write_encoded_tm_str(OutputIt out, string_view in, const std::locale& loc) unit_t unit; write_codecvt(unit, in, loc); // In UTF-8 is used one to four one-byte code units. - unicode_to_utf8> - u; + auto u = + to_utf8>(); if (!u.convert({unit.buf, to_unsigned(unit.end - unit.buf)})) FMT_THROW(format_error("failed to format time")); return copy_str(u.c_str(), u.c_str() + u.size(), out); diff --git a/include/fmt/format.h b/include/fmt/format.h index ed8b29eb..c925e13e 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1420,13 +1420,13 @@ class utf8_to_utf16 { // A converter from UTF-16/UTF-32 (host endian) to UTF-8. template -class unicode_to_utf8 { +class to_utf8 { private: Buffer buffer_; public: - unicode_to_utf8() {} - explicit unicode_to_utf8(basic_string_view s) { + to_utf8() {} + explicit to_utf8(basic_string_view s) { static_assert(sizeof(WChar) == 2 || sizeof(WChar) == 4, "Expect utf16 or utf32"); diff --git a/include/fmt/std.h b/include/fmt/std.h index 4c2a28cf..1fda6154 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -61,7 +61,7 @@ inline void write_escaped_path(memory_buffer& quoted, auto buf = basic_memory_buffer(); write_escaped_string(std::back_inserter(buf), p.native()); // Convert UTF-16 to UTF-8. - if (!unicode_to_utf8::convert(quoted, {buf.data(), buf.size()})) + if (!to_utf8::convert(quoted, {buf.data(), buf.size()})) FMT_THROW(std::runtime_error("invalid utf16")); } # endif diff --git a/src/os.cc b/src/os.cc index 01732b38..b9451b0c 100644 --- a/src/os.cc +++ b/src/os.cc @@ -110,9 +110,9 @@ class utf8_system_category final : public std::error_category { public: const char* name() const noexcept override { return "system"; } std::string message(int error_code) const override { - system_message msg(error_code); + auto&& msg = system_message(error_code); if (msg) { - unicode_to_utf8 utf8_message; + auto utf8_message = to_utf8(); if (utf8_message.convert(msg)) { return utf8_message.str(); } @@ -137,12 +137,12 @@ std::system_error vwindows_error(int err_code, string_view format_str, void detail::format_windows_error(detail::buffer& out, int error_code, const char* message) noexcept { FMT_TRY { - system_message msg(error_code); + auto&& msg = system_message(error_code); if (msg) { - unicode_to_utf8 utf8_message; + auto utf8_message = to_utf8(); if (utf8_message.convert(msg)) { - fmt::format_to(buffer_appender(out), FMT_STRING("{}: {}"), - message, string_view(utf8_message)); + fmt::format_to(appender(out), FMT_STRING("{}: {}"), message, + string_view(utf8_message)); return; } } @@ -337,9 +337,8 @@ file file::open_windows_file(wcstring_view path, int oflag) { int fd = -1; auto err = _wsopen_s(&fd, path.c_str(), oflag, _SH_DENYNO, default_open_mode); if (fd == -1) { - FMT_THROW( - system_error(err, FMT_STRING("cannot open file {}"), - detail::unicode_to_utf8(path.c_str()).c_str())); + FMT_THROW(system_error(err, FMT_STRING("cannot open file {}"), + detail::to_utf8(path.c_str()).c_str())); } return file(fd); } diff --git a/test/format-impl-test.cc b/test/format-impl-test.cc index 0364bf86..0d477e52 100644 --- a/test/format-impl-test.cc +++ b/test/format-impl-test.cc @@ -560,9 +560,9 @@ TEST(format_impl_test, utf8_decode_bogus_byte_sequences) { EXPECT_EQ(len, 2); // "bogus [c0 0a] recovery %d", len); } -TEST(format_impl_test, unicode_to_utf8) { +TEST(format_impl_test, to_utf8) { auto s = std::string("ёжик"); - fmt::detail::unicode_to_utf8 u(L"\x0451\x0436\x0438\x043A"); + auto u = fmt::detail::to_utf8(L"\x0451\x0436\x0438\x043A"); EXPECT_EQ(s, u.str()); EXPECT_EQ(s.size(), u.size()); } diff --git a/test/os-test.cc b/test/os-test.cc index 2a34efd4..1e6c7c4b 100644 --- a/test/os-test.cc +++ b/test/os-test.cc @@ -29,8 +29,8 @@ TEST(os_test, format_windows_error) { FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, ERROR_FILE_EXISTS, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast(&message), 0, nullptr); - fmt::detail::unicode_to_utf8 utf8_message( - wstring_view(message, result - 2)); + auto utf8_message = + fmt::detail::to_utf8(wstring_view(message, result - 2)); LocalFree(message); fmt::memory_buffer actual_message; fmt::detail::format_windows_error(actual_message, ERROR_FILE_EXISTS, "test"); @@ -55,8 +55,8 @@ TEST(os_test, format_long_windows_error) { LocalFree(message); return; } - fmt::detail::unicode_to_utf8 utf8_message( - wstring_view(message, result - 2)); + auto utf8_message = + fmt::detail::to_utf8(wstring_view(message, result - 2)); LocalFree(message); fmt::memory_buffer actual_message; fmt::detail::format_windows_error(actual_message, provisioning_not_allowed,