77#include < sstream>
88#include < vector>
99#include < map>
10+ #include < iostream>
1011#include " etld/internal/public_suffix_rule.h"
1112#include " etld/internal/public_suffix_rule_set.h"
1213#include " etld/domain.h"
@@ -20,14 +21,14 @@ namespace internal {
2021
2122PublicSuffixRuleSet::PublicSuffixRuleSet () {}
2223
23- PublicSuffixRuleSet::PublicSuffixRuleSet (const PublicSuffixRuleSet & rule_set) {
24+ PublicSuffixRuleSet::PublicSuffixRuleSet (const PublicSuffixRuleSet& rule_set) {
2425 for (auto &elm : rule_set.rules_ ) {
2526 AddRule (*elm);
2627 }
2728}
2829
2930PublicSuffixRuleSet::PublicSuffixRuleSet (
30- const std::vector<PublicSuffixRule> & rules) {
31+ const std::vector<PublicSuffixRule>& rules) {
3132 for (auto &elm : rules) {
3233 AddRule (elm);
3334 }
@@ -37,7 +38,7 @@ PublicSuffixRuleSet::~PublicSuffixRuleSet() {
3738 delete root_;
3839}
3940
40- bool PublicSuffixRuleSet::Equal (const PublicSuffixRuleSet & rule_set) const {
41+ bool PublicSuffixRuleSet::Equal (const PublicSuffixRuleSet& rule_set) const {
4142 if (rules_.size () != rule_set.rules_ .size ()) {
4243 return false ;
4344 }
@@ -88,7 +89,7 @@ SerializationResult PublicSuffixRuleSet::Serialize() const {
8889}
8990
9091PublicSuffixRuleSetMatchResult PublicSuffixRuleSet::Match (
91- const Domain & domain) const {
92+ const Domain& domain) const {
9293 if (root_ == nullptr ) {
9394 return {false , nullptr };
9495 }
@@ -98,57 +99,44 @@ PublicSuffixRuleSetMatchResult PublicSuffixRuleSet::Match(
9899 return {false , nullptr };
99100 }
100101
101- std::vector< const PublicSuffixRule*>* matches = new std::vector< const PublicSuffixRule*>() ;
102- MatchRecursions (domain, domain_length - 1 , matches , root_);
102+ const PublicSuffixRule* match = nullptr ;
103+ MatchRecursions (domain, domain_length - 1 , &match , root_);
103104
104- if (matches->size () == 0 ) {
105- delete matches;
105+ if (match == nullptr ) {
106106 return {false , nullptr };
107107 }
108108
109- size_t longest_length = 0 ;
110- size_t found_length;
111- const PublicSuffixRule* longest_match_result = nullptr ;
112- for (auto &elm : *matches) {
113- found_length = elm->Length ();
114- if (found_length > longest_length) {
115- longest_match_result = elm;
116- longest_length = found_length;
117- }
118- }
119-
120- delete matches;
121- if (longest_match_result == nullptr ) {
122- return {false , nullptr };
123- }
124-
125- return {true , longest_match_result};
109+ return {true , match};
126110}
127111
128- void PublicSuffixRuleSet::MatchRecursions (const Domain & domain,
129- const size_t label_index, std::vector< const PublicSuffixRule*>* matches ,
130- PublicSuffixRuleMapNode * node) const {
112+ void PublicSuffixRuleSet::MatchRecursions (const Domain& domain,
113+ const size_t label_index, const PublicSuffixRule** match ,
114+ PublicSuffixRuleMapNode* node) const {
131115 const Label& current_label = domain.Get (label_index);
132116
133117 const auto label_result = node->children ->find (current_label);
134118 if (label_result != node->children ->end ()) {
135119 PublicSuffixRuleMapNode* child_node = node->children ->at (current_label);
136120 if (child_node->rule != nullptr ) {
137- matches->push_back (child_node->rule );
121+ if (*match == nullptr || child_node->rule ->Length () > (*match)->Length ()) {
122+ *match = child_node->rule ;
123+ }
138124 }
139125 if (label_index > 0 ) {
140- MatchRecursions (domain, label_index - 1 , matches , child_node);
126+ MatchRecursions (domain, label_index - 1 , match , child_node);
141127 }
142128 }
143129
144130 const auto wildcard_result = node->children ->find (" *" );
145131 if (wildcard_result != node->children ->end ()) {
146132 PublicSuffixRuleMapNode* child_node = node->children ->at (" *" );
147133 if (child_node->rule != nullptr ) {
148- matches->push_back (child_node->rule );
134+ if (*match == nullptr || child_node->rule ->Length () > (*match)->Length ()) {
135+ *match = child_node->rule ;
136+ }
149137 }
150138 if (label_index > 0 ) {
151- MatchRecursions (domain, label_index - 1 , matches , child_node);
139+ MatchRecursions (domain, label_index - 1 , match , child_node);
152140 }
153141 }
154142}
0 commit comments