mirror of
https://github.com/fmtlib/fmt.git
synced 2025-04-25 03:02:38 +00:00
unicode_to_utf8 -> to_utf8 since both sides of conversion are Unicode
This commit is contained in:
parent
a08196b149
commit
93a30a0746
@ -377,8 +377,8 @@ auto write_encoded_tm_str(OutputIt out, string_view in, const std::locale& loc)
|
|||||||
unit_t unit;
|
unit_t unit;
|
||||||
write_codecvt(unit, in, loc);
|
write_codecvt(unit, in, loc);
|
||||||
// In UTF-8 is used one to four one-byte code units.
|
// In UTF-8 is used one to four one-byte code units.
|
||||||
unicode_to_utf8<code_unit, basic_memory_buffer<char, unit_t::max_size * 4>>
|
auto u =
|
||||||
u;
|
to_utf8<code_unit, basic_memory_buffer<char, unit_t::max_size * 4>>();
|
||||||
if (!u.convert({unit.buf, to_unsigned(unit.end - unit.buf)}))
|
if (!u.convert({unit.buf, to_unsigned(unit.end - unit.buf)}))
|
||||||
FMT_THROW(format_error("failed to format time"));
|
FMT_THROW(format_error("failed to format time"));
|
||||||
return copy_str<char>(u.c_str(), u.c_str() + u.size(), out);
|
return copy_str<char>(u.c_str(), u.c_str() + u.size(), out);
|
||||||
|
@ -1420,13 +1420,13 @@ class utf8_to_utf16 {
|
|||||||
|
|
||||||
// A converter from UTF-16/UTF-32 (host endian) to UTF-8.
|
// A converter from UTF-16/UTF-32 (host endian) to UTF-8.
|
||||||
template <typename WChar, typename Buffer = memory_buffer>
|
template <typename WChar, typename Buffer = memory_buffer>
|
||||||
class unicode_to_utf8 {
|
class to_utf8 {
|
||||||
private:
|
private:
|
||||||
Buffer buffer_;
|
Buffer buffer_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
unicode_to_utf8() {}
|
to_utf8() {}
|
||||||
explicit unicode_to_utf8(basic_string_view<WChar> s) {
|
explicit to_utf8(basic_string_view<WChar> s) {
|
||||||
static_assert(sizeof(WChar) == 2 || sizeof(WChar) == 4,
|
static_assert(sizeof(WChar) == 2 || sizeof(WChar) == 4,
|
||||||
"Expect utf16 or utf32");
|
"Expect utf16 or utf32");
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ inline void write_escaped_path<char>(memory_buffer& quoted,
|
|||||||
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), p.native());
|
||||||
// Convert UTF-16 to UTF-8.
|
// Convert UTF-16 to UTF-8.
|
||||||
if (!unicode_to_utf8<wchar_t>::convert(quoted, {buf.data(), buf.size()}))
|
if (!to_utf8<wchar_t>::convert(quoted, {buf.data(), buf.size()}))
|
||||||
FMT_THROW(std::runtime_error("invalid utf16"));
|
FMT_THROW(std::runtime_error("invalid utf16"));
|
||||||
}
|
}
|
||||||
# endif
|
# endif
|
||||||
|
17
src/os.cc
17
src/os.cc
@ -110,9 +110,9 @@ class utf8_system_category final : public std::error_category {
|
|||||||
public:
|
public:
|
||||||
const char* name() const noexcept override { return "system"; }
|
const char* name() const noexcept override { return "system"; }
|
||||||
std::string message(int error_code) const override {
|
std::string message(int error_code) const override {
|
||||||
system_message msg(error_code);
|
auto&& msg = system_message(error_code);
|
||||||
if (msg) {
|
if (msg) {
|
||||||
unicode_to_utf8<wchar_t> utf8_message;
|
auto utf8_message = to_utf8<wchar_t>();
|
||||||
if (utf8_message.convert(msg)) {
|
if (utf8_message.convert(msg)) {
|
||||||
return utf8_message.str();
|
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<char>& out, int error_code,
|
void detail::format_windows_error(detail::buffer<char>& out, int error_code,
|
||||||
const char* message) noexcept {
|
const char* message) noexcept {
|
||||||
FMT_TRY {
|
FMT_TRY {
|
||||||
system_message msg(error_code);
|
auto&& msg = system_message(error_code);
|
||||||
if (msg) {
|
if (msg) {
|
||||||
unicode_to_utf8<wchar_t> utf8_message;
|
auto utf8_message = to_utf8<wchar_t>();
|
||||||
if (utf8_message.convert(msg)) {
|
if (utf8_message.convert(msg)) {
|
||||||
fmt::format_to(buffer_appender<char>(out), FMT_STRING("{}: {}"),
|
fmt::format_to(appender(out), FMT_STRING("{}: {}"), message,
|
||||||
message, string_view(utf8_message));
|
string_view(utf8_message));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -337,9 +337,8 @@ file file::open_windows_file(wcstring_view path, int oflag) {
|
|||||||
int fd = -1;
|
int fd = -1;
|
||||||
auto err = _wsopen_s(&fd, path.c_str(), oflag, _SH_DENYNO, default_open_mode);
|
auto err = _wsopen_s(&fd, path.c_str(), oflag, _SH_DENYNO, default_open_mode);
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
FMT_THROW(
|
FMT_THROW(system_error(err, FMT_STRING("cannot open file {}"),
|
||||||
system_error(err, FMT_STRING("cannot open file {}"),
|
detail::to_utf8<wchar_t>(path.c_str()).c_str()));
|
||||||
detail::unicode_to_utf8<wchar_t>(path.c_str()).c_str()));
|
|
||||||
}
|
}
|
||||||
return file(fd);
|
return file(fd);
|
||||||
}
|
}
|
||||||
|
@ -560,9 +560,9 @@ TEST(format_impl_test, utf8_decode_bogus_byte_sequences) {
|
|||||||
EXPECT_EQ(len, 2); // "bogus [c0 0a] recovery %d", len);
|
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("ёжик");
|
auto s = std::string("ёжик");
|
||||||
fmt::detail::unicode_to_utf8<wchar_t> u(L"\x0451\x0436\x0438\x043A");
|
auto u = fmt::detail::to_utf8<wchar_t>(L"\x0451\x0436\x0438\x043A");
|
||||||
EXPECT_EQ(s, u.str());
|
EXPECT_EQ(s, u.str());
|
||||||
EXPECT_EQ(s.size(), u.size());
|
EXPECT_EQ(s.size(), u.size());
|
||||||
}
|
}
|
||||||
|
@ -29,8 +29,8 @@ TEST(os_test, format_windows_error) {
|
|||||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||||
nullptr, ERROR_FILE_EXISTS, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
nullptr, ERROR_FILE_EXISTS, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||||
reinterpret_cast<LPWSTR>(&message), 0, nullptr);
|
reinterpret_cast<LPWSTR>(&message), 0, nullptr);
|
||||||
fmt::detail::unicode_to_utf8<wchar_t> utf8_message(
|
auto utf8_message =
|
||||||
wstring_view(message, result - 2));
|
fmt::detail::to_utf8<wchar_t>(wstring_view(message, result - 2));
|
||||||
LocalFree(message);
|
LocalFree(message);
|
||||||
fmt::memory_buffer actual_message;
|
fmt::memory_buffer actual_message;
|
||||||
fmt::detail::format_windows_error(actual_message, ERROR_FILE_EXISTS, "test");
|
fmt::detail::format_windows_error(actual_message, ERROR_FILE_EXISTS, "test");
|
||||||
@ -55,8 +55,8 @@ TEST(os_test, format_long_windows_error) {
|
|||||||
LocalFree(message);
|
LocalFree(message);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
fmt::detail::unicode_to_utf8<wchar_t> utf8_message(
|
auto utf8_message =
|
||||||
wstring_view(message, result - 2));
|
fmt::detail::to_utf8<wchar_t>(wstring_view(message, result - 2));
|
||||||
LocalFree(message);
|
LocalFree(message);
|
||||||
fmt::memory_buffer actual_message;
|
fmt::memory_buffer actual_message;
|
||||||
fmt::detail::format_windows_error(actual_message, provisioning_not_allowed,
|
fmt::detail::format_windows_error(actual_message, provisioning_not_allowed,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user