| #include "redfish_to_uhm_trie_node.hpp" |
| |
| #include <memory> |
| #include <string> |
| #include <string_view> |
| #include <utility> |
| |
| #include "re2/re2.h" |
| |
| namespace devpath_plugin { |
| |
| namespace { |
| |
| bool IsRegexSegment(std::string_view segment) { |
| return segment.find_first_of("[]*+?|()^$\\") != std::string_view::npos; |
| } |
| |
| } // namespace |
| |
| RedfishToUhmTrieNode* RedfishToUhmTrieNode::GetOrCreateChild( |
| std::string_view segment) { |
| if (IsRegexSegment(segment)) { |
| return GetOrCreateRegexChild(segment); |
| } |
| return GetOrCreateExactChild(segment); |
| } |
| |
| const RedfishToUhmTrieNode* RedfishToUhmTrieNode::GetChild( |
| std::string_view segment) const { |
| const RedfishToUhmTrieNode* next = GetExactChild(segment); |
| if (next != nullptr) { |
| return next; |
| } |
| return GetRegexChild(segment); |
| } |
| |
| RedfishToUhmTrieNode* RedfishToUhmTrieNode::GetOrCreateExactChild( |
| std::string_view segment) { |
| std::unique_ptr<RedfishToUhmTrieNode>& child = exact_children_[segment]; |
| if (!child) { |
| child = std::make_unique<RedfishToUhmTrieNode>(); |
| } |
| return child.get(); |
| } |
| |
| RedfishToUhmTrieNode* RedfishToUhmTrieNode::GetOrCreateRegexChild( |
| std::string_view pattern) { |
| for (const auto& child : regex_children_) { |
| if (child.pattern == pattern) { |
| return child.node.get(); |
| } |
| } |
| auto new_node = std::make_unique<RedfishToUhmTrieNode>(); |
| RedfishToUhmTrieNode* raw_node = new_node.get(); |
| regex_children_.push_back(RegexChild{ |
| std::string(pattern), |
| std::make_unique<RE2>(pattern), |
| std::move(new_node), |
| }); |
| return raw_node; |
| } |
| |
| const RedfishToUhmTrieNode* RedfishToUhmTrieNode::GetExactChild( |
| std::string_view segment) const { |
| auto it = exact_children_.find(segment); |
| if (it == exact_children_.end()) { |
| return nullptr; |
| } |
| return it->second.get(); |
| } |
| |
| const RedfishToUhmTrieNode* RedfishToUhmTrieNode::GetRegexChild( |
| std::string_view segment) const { |
| for (const auto& child : regex_children_) { |
| if (child.regex && RE2::FullMatch(segment, *child.regex)) { |
| return child.node.get(); |
| } |
| } |
| return nullptr; |
| } |
| |
| } // namespace devpath_plugin |