From 6d487daaec8190be22d4b72971fcbb5f6b5a3b27 Mon Sep 17 00:00:00 2001 From: dragonmux Date: Tue, 31 Oct 2023 23:30:04 +0000 Subject: [PATCH] console: Implemented Windows support for displaying UTF-16 strings --- impl/console.cxx | 23 +++++++++++++++++++++++ substrate/console | 4 ++++ 2 files changed, 27 insertions(+) diff --git a/impl/console.cxx b/impl/console.cxx index f685b2d2..9d7e26b6 100644 --- a/impl/console.cxx +++ b/impl/console.cxx @@ -21,6 +21,7 @@ using charTraits = std::char_traits; #ifdef _WIN32 using wcharTraits = std::char_traits; #endif +using char16Traits = std::char_traits; static const std::string errorPrefix{"[ERR]"_s}; static const std::string warningPrefix{"[WRN]"_s}; @@ -108,6 +109,28 @@ namespace substrate } #endif + void consoleStream_t::write(const char16_t *const value) const noexcept + { write(value, value ? char16Traits::length(value) : 0U); } + + void consoleStream_t::write(const char16_t *const value, const size_t valueLen) const noexcept + { + if (value) + { + // If there's nothing to convert (0-length string), fast-exit doing nothing. + if (!valueLen) + return; +#ifdef _WIN32 + const auto consoleMode{_setmode(fd, _O_U16TEXT)}; + write(static_cast(value), sizeof(char16_t) * valueLen); + _setmode(fd, consoleMode); +#else + // +#endif + } + else + write(nullString); + } + void consoleStream_t::write(const bool value) const noexcept { write(value ? trueString : falseString); } diff --git a/substrate/console b/substrate/console index 40c3d44e..dc8707ec 100644 --- a/substrate/console +++ b/substrate/console @@ -71,6 +71,8 @@ namespace substrate void write(const wchar_t *value) const noexcept; void write(const wchar_t *value, size_t valueLen) const noexcept; #endif + void write(const char16_t *value) const noexcept; + void write(const char16_t *value, size_t valueLen) const noexcept; void write(const char value) const noexcept { write(&value, 1U); } template enable_if_t::value> write(const std::unique_ptr &value) const noexcept @@ -84,6 +86,8 @@ namespace substrate void write(const std::wstring &value) const noexcept { write(value.data(), value.length()); } #endif + void write(const std::u16string &value) const noexcept + { write(value.data(), value.length()); } #if __cplusplus >= 201703L void write(const std::string_view &value) const noexcept { write(value.data(), value.length()); }