From fcefab4383935d318f5a7b77601992c72fd32504 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 21 Dec 2022 19:41:13 +0100 Subject: [PATCH] TextConsole: Add wide-character overloads for print() and println() These can't fail from UTF-8 decoding errors. --- kernel/src/Log.cpp | 11 +---------- kernel/src/video/TextConsole.cpp | 11 +++++++++++ kernel/src/video/TextConsole.h | 2 ++ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/kernel/src/Log.cpp b/kernel/src/Log.cpp index d13d9e0e..713666fa 100644 --- a/kernel/src/Log.cpp +++ b/kernel/src/Log.cpp @@ -58,16 +58,7 @@ static void log_text_console(LogLevel level, const char* format, va_list origin) auto rc = cstyle_format( format, [](char c, void*) -> Result { return TextConsole::putchar(c); }, nullptr, ap); - if (rc.has_error()) - { - // FIXME: Find a way to do this in a simpler way (TextConsole::wprint maybe?) - const wchar_t* s = L"Invalid UTF-8 in log message"; - while (*s) - { - TextConsole::putwchar(*s); - s++; - } - } + if (rc.has_error()) { TextConsole::wprint(L"Invalid UTF-8 in log message"); } TextConsole::putwchar(L'\n'); diff --git a/kernel/src/video/TextConsole.cpp b/kernel/src/video/TextConsole.cpp index abd1c139..8ccea5cf 100644 --- a/kernel/src/video/TextConsole.cpp +++ b/kernel/src/video/TextConsole.cpp @@ -172,6 +172,11 @@ namespace TextConsole return {}; } + void wprint(const wchar_t* str) + { + while (*str) putwchar(*str++); + } + Result println(const char* str) { TRY(print(str)); @@ -179,6 +184,12 @@ namespace TextConsole return {}; } + void wprintln(const wchar_t* str) + { + wprint(str); + putwchar(L'\n'); + } + Result printf(const char* format, ...) { va_list ap; diff --git a/kernel/src/video/TextConsole.h b/kernel/src/video/TextConsole.h index 464f34d9..6614108e 100644 --- a/kernel/src/video/TextConsole.h +++ b/kernel/src/video/TextConsole.h @@ -14,6 +14,8 @@ namespace TextConsole u32 background(); void move_to(u32 x, u32 y); Result print(const char* str); + void wprint(const wchar_t* str); Result println(const char* str); + void wprintln(const wchar_t* str); Result printf(const char* format, ...) _format(1, 2); } \ No newline at end of file