OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/common/matcher/substring_set_matcher.h" | 5 #include "extensions/common/matcher/substring_set_matcher.h" |
6 | 6 |
7 #include <queue> | 7 #include <queue> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 i != patterns_.end(); ++i) { | 94 i != patterns_.end(); ++i) { |
95 InsertPatternIntoAhoCorasickTree(i->second); | 95 InsertPatternIntoAhoCorasickTree(i->second); |
96 } | 96 } |
97 | 97 |
98 CreateFailureEdges(); | 98 CreateFailureEdges(); |
99 } | 99 } |
100 | 100 |
101 void SubstringSetMatcher::InsertPatternIntoAhoCorasickTree( | 101 void SubstringSetMatcher::InsertPatternIntoAhoCorasickTree( |
102 const StringPattern* pattern) { | 102 const StringPattern* pattern) { |
103 const std::string& text = pattern->pattern(); | 103 const std::string& text = pattern->pattern(); |
104 size_t text_length = text.length(); | 104 const std::string::const_iterator text_end = text.end(); |
105 | 105 |
106 // Iterators on the tree and the text. | 106 std::string::const_iterator text_iter = text.begin(); |
107 int current_node = 0; | 107 int current_node = 0; |
108 size_t text_pos = 0; | |
109 | 108 |
110 // Follow existing paths for as long as possible. | 109 // Follow existing paths for as long as possible. |
111 while (text_pos < text_length && | 110 int edge_to; |
112 tree_[current_node].HasEdge(text[text_pos])) { | 111 while (text_iter != text_end && |
113 current_node = tree_[current_node].GetEdge(text[text_pos]); | 112 (edge_to = tree_[current_node].GetEdge(*text_iter)) != -1) { |
114 ++text_pos; | 113 current_node = edge_to; |
| 114 ++text_iter; |
115 } | 115 } |
116 | 116 |
117 // Create new nodes if necessary. | 117 // Create new nodes if necessary. |
118 while (text_pos < text_length) { | 118 int new_node_position = tree_.size(); |
119 AhoCorasickNode new_node; | 119 while (text_iter != text_end) { |
120 tree_.push_back(new_node); | 120 tree_.push_back(AhoCorasickNode()); |
121 tree_[current_node].SetEdge(text[text_pos], tree_.size() - 1); | 121 tree_[current_node].SetEdge(*text_iter, new_node_position); |
122 current_node = tree_.size() - 1; | 122 current_node = new_node_position; |
123 ++text_pos; | 123 ++text_iter; |
| 124 ++new_node_position; |
124 } | 125 } |
125 | 126 |
126 // Register match. | 127 // Register match. |
127 tree_[current_node].AddMatch(pattern->id()); | 128 tree_[current_node].AddMatch(pattern->id()); |
128 } | 129 } |
129 | 130 |
130 void SubstringSetMatcher::CreateFailureEdges() { | 131 void SubstringSetMatcher::CreateFailureEdges() { |
131 typedef AhoCorasickNode::Edges Edges; | 132 typedef AhoCorasickNode::Edges Edges; |
132 | 133 |
133 std::queue<int> queue; | 134 std::queue<int> queue; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 void SubstringSetMatcher::AhoCorasickNode::AddMatch(StringPattern::ID id) { | 200 void SubstringSetMatcher::AhoCorasickNode::AddMatch(StringPattern::ID id) { |
200 matches_.insert(id); | 201 matches_.insert(id); |
201 } | 202 } |
202 | 203 |
203 void SubstringSetMatcher::AhoCorasickNode::AddMatches( | 204 void SubstringSetMatcher::AhoCorasickNode::AddMatches( |
204 const SubstringSetMatcher::AhoCorasickNode::Matches& matches) { | 205 const SubstringSetMatcher::AhoCorasickNode::Matches& matches) { |
205 matches_.insert(matches.begin(), matches.end()); | 206 matches_.insert(matches.begin(), matches.end()); |
206 } | 207 } |
207 | 208 |
208 } // namespace extensions | 209 } // namespace extensions |
OLD | NEW |