diff --git a/kernel/src/video/TextConsole.cpp b/kernel/src/video/TextConsole.cpp index dd8d0d17..ae1547a6 100644 --- a/kernel/src/video/TextConsole.cpp +++ b/kernel/src/video/TextConsole.cpp @@ -127,12 +127,12 @@ namespace TextConsole { auto guard = make_scope_guard([] { utf8_decoder.reset(); }); - const Option maybe_wchar = TRY(utf8_decoder.feed(c)); + bool is_ready = TRY(utf8_decoder.feed(c)); + + if (is_ready) putwchar(TRY(utf8_decoder.extract())); guard.deactivate(); - if (maybe_wchar.has_value()) putwchar(maybe_wchar.value()); - return {}; } diff --git a/libluna/include/luna/Utf8.h b/libluna/include/luna/Utf8.h index 05faf98a..34c35587 100644 --- a/libluna/include/luna/Utf8.h +++ b/libluna/include/luna/Utf8.h @@ -52,13 +52,16 @@ class Utf8StateDecoder public: Utf8StateDecoder(); - Result> feed(char c); + Result feed(char c); + Result extract(); void reset(); private: char m_state[4]; usize m_state_len = 0; usize m_state_index = 0; + wchar_t m_decoded_character; + bool m_has_character_ready { false }; }; class Utf8Encoder diff --git a/libluna/src/Utf8.cpp b/libluna/src/Utf8.cpp index 0a4873e3..596aa45f 100644 --- a/libluna/src/Utf8.cpp +++ b/libluna/src/Utf8.cpp @@ -217,19 +217,23 @@ Utf8StateDecoder::Utf8StateDecoder() : m_state_len(0), m_state_index(0) { } -Result> Utf8StateDecoder::feed(char c) +Result Utf8StateDecoder::feed(char c) { + check(!m_has_character_ready); + if (m_state_len == 0) { m_state_len = TRY(utf8_char_length(c)); if (m_state_len == 1) { m_state_len = 0; - return Option { c & 0x7f }; + m_decoded_character = c & 0x7f; + m_has_character_ready = true; + return true; } m_state_index = 0; m_state[m_state_index] = c; - return { {} }; + return false; } m_state_index++; @@ -238,12 +242,20 @@ Result> Utf8StateDecoder::feed(char c) if (m_state_index == m_state_len - 1) { usize len = m_state_len; - const wchar_t wc = TRY(encode_utf8_as_wide_char(m_state, len)); + m_decoded_character = TRY(encode_utf8_as_wide_char(m_state, len)); + m_has_character_ready = true; m_state_len = 0; - return Option { wc }; + return true; } - return { {} }; + return false; +} + +Result Utf8StateDecoder::extract() +{ + check(m_has_character_ready); + m_has_character_ready = false; + return m_decoded_character; } void Utf8StateDecoder::reset()