From cc876f7107f33a860e7379992aefd3f8659b948f Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 29 Oct 2012 18:59:41 +0100 Subject: [PATCH] validate key in InputHandler before sending to mode, and minor refactor --- src/input_handler.cc | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/input_handler.cc b/src/input_handler.cc index 56879e98..c6c782af 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -62,6 +62,14 @@ private: int m_count = 0; }; +String codepoint_to_str(Codepoint cp) +{ + std::string str; + auto it = back_inserter(str); + utf8::dump(it, cp); + return String(str); +} + class LineEditor { public: @@ -91,10 +99,7 @@ public: } else { - std::string keystr; - auto inserter = back_inserter(keystr); - utf8::dump(inserter, key.key); - m_line = m_line.substr(0, m_cursor_pos) + keystr + m_line = m_line.substr(0, m_cursor_pos) + codepoint_to_str(key.key) + m_line.substr(m_cursor_pos); ++m_cursor_pos; } @@ -467,14 +472,6 @@ private: int m_current_completion = -1; }; -String codepoint_to_str(Codepoint cp) -{ - std::string str; - auto it = back_inserter(str); - utf8::dump(it, cp); - return String(str); -} - class Insert : public InputMode { public: @@ -625,12 +622,17 @@ void InputHandler::on_next_key(KeyCallback callback) m_mode.reset(new InputModes::NextKey(*this, callback)); } +bool is_valid(const Key& key) +{ + return key != Key::Invalid and key.key <= 0x10FFFF; +} + void InputHandler::handle_available_inputs(Context& context) { while (context.ui().is_key_available()) { Key key = context.ui().get_key(); - if (key != Key::Invalid) + if (is_valid(key)) m_mode->on_key(key, context); } context.draw_ifn();