poaflocParser Of Arguments For Lines Of Commands |
git clone git://git.dimitrijedobrota.com/poafloc.git |
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |
trie.cpp (1182B)
0 #include <algorithm> 1 #include <cstdint> 2 3 #include "poafloc/poafloc.hpp" 4 5 namespace poafloc { 6 7 bool Parser::trie_t::insert(const std::string& option, int key) 8 { 9 trie_t* crnt = this; 10 11 if (!is_valid(option)) return false; 12 13 for (const char chr : option) 14 { 15 if (!crnt->m_terminal) crnt->m_key = key; 16 crnt->m_count++; 17 18 const size_t idx = static_cast<unsigned>(chr) - 'a'; 19 if (!crnt->m_children.at(idx)) 20 crnt->m_children.at(idx) = std::make_unique<trie_t>(); 21 22 crnt = crnt->m_children.at(idx).get(); 23 } 24 25 crnt->m_terminal = true; 26 crnt->m_key = key; 27 28 return true; 29 } 30 31 int Parser::trie_t::get(const std::string& option) const 32 { 33 const trie_t* crnt = this; 34 35 if (!is_valid(option)) return 0; 36 37 for (const char chr : option) 38 { 39 const size_t idx = static_cast<unsigned>(chr) - 'a'; 40 if (!crnt->m_children.at(idx)) return 0; 41 42 crnt = crnt->m_children.at(idx).get(); 43 } 44 45 if (!crnt->m_terminal && crnt->m_count > 1) return 0; 46 return crnt->m_key; 47 } 48 49 bool Parser::trie_t::is_valid(const std::string& option) 50 { 51 return std::all_of( 52 begin(option), end(option), [](char chr) { return std::islower(chr); }); 53 } 54 55 } // namespace poafloc