start on CommandMap::get_command()

This commit is contained in:
Josh Holtrop 2017-10-24 21:45:35 -04:00
parent 25806993f9
commit a07e9eb46b
2 changed files with 49 additions and 2 deletions

View File

@ -32,7 +32,53 @@ void CommandMap::add(const char * s, uint32_t id,
node->next_map = next_map; node->next_map = next_map;
} }
std::shared_ptr<Command> CommandMap::get_command(uint32_t * command_characters, uint32_t count) std::shared_ptr<Command> 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>(command);
}
}
else if (node->next_map)
{
in_motion = true;
node = &node->next_map->m_root_node;
}
else
{
return std::make_shared<Command>(command);
}
}
}
else
{ {
return nullptr; return nullptr;
} }
}
return nullptr;
}

View File

@ -11,7 +11,8 @@ class CommandMap
public: public:
void add(const char * s, uint32_t id, std::shared_ptr<CommandMap> next_map, void add(const char * s, uint32_t id, std::shared_ptr<CommandMap> next_map,
bool following_char); bool following_char);
std::shared_ptr<Command> get_command(uint32_t * command_characters, uint32_t count); std::shared_ptr<Command> get_command(uint32_t * command_characters,
uint32_t length) const;
protected: protected:
struct Node struct Node