diff --git a/src/core/Command.cc b/src/core/Command.cc index b5ef84f..41dcb06 100644 --- a/src/core/Command.cc +++ b/src/core/Command.cc @@ -1,28 +1,12 @@ #include "Command.h" #include -static const struct +Command::Command() { - const char * name; - uint32_t flags; -} Commands[] = { - {"nop", 0u}, - {"forward-up-to-char", Command::FOLLOWING_CHAR}, - {"forward-on-to-char", Command::FOLLOWING_CHAR}, - {"back-up-to-char", Command::FOLLOWING_CHAR}, - {"back-on-to-char", Command::FOLLOWING_CHAR}, - {"delete", Command::RANGE}, - {"delete-line", 0u}, -}; - -uint32_t Command::find_command_by_name(const char * name) -{ - for (uint32_t i = 0u; i < COMMAND_COUNT; i++) - { - if (strcmp(Commands[i].name, name) == 0u) - { - return i; - } - } - return COMMAND_COUNT; + id = NOP; + count = 0u; + following_char = 0u; + motion.id = Motion::NOP; + motion.count = 0u; + motion.following_char = 0u; } diff --git a/src/core/Command.h b/src/core/Command.h index 1ed0f56..3d9af39 100644 --- a/src/core/Command.h +++ b/src/core/Command.h @@ -6,12 +6,6 @@ class Command { public: - enum : uint32_t - { - FOLLOWING_CHAR = 0x1u, - RANGE = 0x2u, - }; - enum : uint32_t { NOP, @@ -24,7 +18,29 @@ public: COMMAND_COUNT, }; - uint32_t find_command_by_name(const char * name); + class Motion + { + public: + enum : uint32_t + { + NOP, + FORWARD_UP_TO_CHAR, + FORWARD_ON_TO_CHAR, + BACK_UP_TO_CHAR, + BACK_ON_TO_CHAR, + }; + + uint32_t id; + uint32_t count; + uint32_t following_char; + }; + + uint32_t id; + uint32_t count; + uint32_t following_char; + Motion motion; + + Command(); }; #endif diff --git a/src/core/CommandMap.cc b/src/core/CommandMap.cc index 706a382..ca20b0c 100644 --- a/src/core/CommandMap.cc +++ b/src/core/CommandMap.cc @@ -1,65 +1,38 @@ #include "CommandMap.h" #include "Command.h" +#include -CommandMap::CommandMap() +void CommandMap::add(const char * s, uint32_t id, + std::shared_ptr next_map, bool following_char) { - m_commands[0] = m_commands[1] = Command::NOP; -} - -std::shared_ptr CommandMap::access(uint32_t entry, bool insert) -{ - auto it = m_children.find(entry); - if (it == m_children.end()) + Node * node = &m_root_node; + size_t length = strlen(s); + if (length < 1u) + return; + for (size_t i = 0u; i < length; i++) { - if (insert) + uint32_t c = (uint32_t)s[i]; + auto it = node->next_chars.find(c); + if (it != node->next_chars.end()) { - std::shared_ptr new_cm = std::make_shared(); - m_children[entry] = new_cm; - return new_cm; + node = &*it->second; } else { - return nullptr; + auto new_node = std::make_shared(); + node->next_chars[c] = new_node; + node = &*new_node; } } - return it->second; + uint8_t flags = Node::FLAG_TERMINATOR; + if (following_char) + flags |= Node::FLAG_FOLLOWING_CHAR; + node->id = id; + node->flags = flags; + node->next_map = next_map; } -std::shared_ptr CommandMap::access(const std::vector entries, bool insert) +std::shared_ptr CommandMap::get_command(uint32_t * command_characters, uint32_t count) { - if (entries.size() == 0u) - { - return nullptr; - } - std::shared_ptr cm = access(entries[0], insert); - for (size_t i = 1u; i < entries.size(); i++) - { - if (cm == nullptr) - { - break; - } - cm = cm->access(entries[i], insert); - } - return cm; -} - -uint32_t CommandMap::command() const -{ - if (m_commands[1] != Command::NOP) - { - return m_commands[1]; - } - return m_commands[0]; -} - -void CommandMap::set_command(uint32_t command, bool user_mapping) -{ - m_commands[user_mapping ? 1 : 0] = command; -} - -bool CommandMap::empty() const -{ - return (m_commands[0] == Command::NOP) && - (m_commands[1] == Command::NOP) && - (m_children.size() == 0u); + return nullptr; } diff --git a/src/core/CommandMap.h b/src/core/CommandMap.h index 9f3900d..91a9d65 100644 --- a/src/core/CommandMap.h +++ b/src/core/CommandMap.h @@ -4,37 +4,37 @@ #include #include #include +#include "Command.h" class CommandMap { public: - CommandMap(); - std::shared_ptr get(uint32_t entry) - { - return access(entry, false); - } - std::shared_ptr get(const std::vector entries) - { - return access(entries, false); - } - std::shared_ptr put(uint32_t entry) - { - return access(entry, true); - } - std::shared_ptr put(const std::vector entries) - { - return access(entries, true); - } - uint32_t command() const; - void set_command(uint32_t command, bool user_mapping); - bool empty() const; + 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); protected: - uint32_t m_commands[2]; - std::unordered_map> m_children; + struct Node + { + enum : uint8_t + { + FLAG_FOLLOWING_CHAR = 0x1u, + FLAG_TERMINATOR = 0x2u, + }; - std::shared_ptr access(uint32_t entry, bool insert); - std::shared_ptr access(const std::vector entries, bool insert); + std::unordered_map> next_chars; + std::shared_ptr next_map; + uint32_t id; + uint8_t flags; + + Node() + { + id = 0u; + flags = 0u; + } + }; + + Node m_root_node; }; #endif