diff --git a/src/core/CommandMap.cc b/src/core/CommandMap.cc index ca20b0c..1fee683 100644 --- a/src/core/CommandMap.cc +++ b/src/core/CommandMap.cc @@ -32,7 +32,53 @@ void CommandMap::add(const char * s, uint32_t id, node->next_map = next_map; } -std::shared_ptr CommandMap::get_command(uint32_t * command_characters, uint32_t count) +std::shared_ptr CommandMap::get_command(uint32_t * command_characters, + uint32_t length) const { + if (length < 1u) + return nullptr; + const Node * node = &m_root_node; + bool in_motion = false; + Command command; + for (size_t i = 0u; i < length; i++) + { + uint32_t c = command_characters[i]; + auto it = node->next_chars.find(c); + if (it != node->next_chars.end()) + { + node = &*it->second; + if (node->flags & Node::FLAG_TERMINATOR) + { + if (in_motion) + command.motion.id = node->id; + else + command.id = node->id; + if (node->flags & Node::FLAG_FOLLOWING_CHAR) + { + if (i < (length - 1u)) + { + if (in_motion) + command.motion.following_char = command_characters[i + 1u]; + else + command.following_char = command_characters[i + 1u]; + return std::make_shared(command); + } + } + else if (node->next_map) + { + in_motion = true; + node = &node->next_map->m_root_node; + } + else + { + return std::make_shared(command); + } + } + } + else + { + return nullptr; + } + } return nullptr; } diff --git a/src/core/CommandMap.h b/src/core/CommandMap.h index 91a9d65..f97da33 100644 --- a/src/core/CommandMap.h +++ b/src/core/CommandMap.h @@ -11,7 +11,8 @@ class CommandMap public: void add(const char * s, uint32_t id, std::shared_ptr next_map, bool following_char); - std::shared_ptr get_command(uint32_t * command_characters, uint32_t count); + std::shared_ptr get_command(uint32_t * command_characters, + uint32_t length) const; protected: struct Node