Skip to content

Commit

Permalink
console: Implemented Windows support for displaying UTF-16 strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonmux committed Nov 1, 2023
1 parent a43f71c commit aba9e3c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
23 changes: 23 additions & 0 deletions impl/console.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ using charTraits = std::char_traits<char>;
#ifdef _WIN32
using wcharTraits = std::char_traits<wchar_t>;
#endif
using char16Traits = std::char_traits<char16_t>;

static const std::string errorPrefix{"[ERR]"_s};
static const std::string warningPrefix{"[WRN]"_s};
Expand Down Expand Up @@ -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<const void *>(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); }

Expand Down
4 changes: 4 additions & 0 deletions substrate/console
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename T> enable_if_t<!std::is_array<T>::value>
write(const std::unique_ptr<T> &value) const noexcept
Expand All @@ -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()); }
Expand Down

0 comments on commit aba9e3c

Please sign in to comment.