OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_COMMON_EXTENSIONS_MATCHER_REGEX_SET_MATCHER_H_ | |
6 #define CHROME_COMMON_EXTENSIONS_MATCHER_REGEX_SET_MATCHER_H_ | |
7 | |
8 #include <map> | |
9 #include <set> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "chrome/common/extensions/matcher/string_pattern.h" | |
15 #include "chrome/common/extensions/matcher/substring_set_matcher.h" | |
16 | |
17 namespace re2 { | |
18 class FilteredRE2; | |
19 } | |
20 | |
21 namespace extensions { | |
22 | |
23 // Efficiently matches URLs against a collection of regular expressions, | |
24 // using FilteredRE2 to reduce the number of regexes that must be matched | |
25 // by pre-filtering with substring matching. See: | |
26 // http://swtch.com/~rsc/regexp/regexp3.html#analysis | |
27 class RegexSetMatcher { | |
28 public: | |
29 RegexSetMatcher(); | |
30 virtual ~RegexSetMatcher(); | |
31 | |
32 // Adds the regex patterns in |regex_list| to the matcher. Also rebuilds | |
33 // the FilteredRE2 matcher; thus, for efficiency, prefer adding multiple | |
34 // patterns at once. | |
battre
2012/09/11 21:49:48
I think for consistency to SubStringMatcher, the o
Yoyo Zhou
2012/09/11 22:35:37
It does. This is perhaps confusing -- the regex pa
| |
35 void AddPatterns(const std::vector<const StringPattern*>& regex_list); | |
36 | |
37 // Removes all regex patterns. | |
38 void ClearPatterns(); | |
39 | |
40 // Appends the IDs of regular expressions in our set that match the |text| | |
41 // to |matches|. | |
42 bool Match(const std::string& text, | |
43 std::set<StringPattern::ID>* matches) const; | |
44 | |
45 private: | |
46 // Use Aho-Corasick SubstringSetMatcher to find which literal patterns | |
47 // match the |text|. | |
48 std::vector<int> FindSubstringMatches(const std::string& text) const; | |
battre
2012/09/11 21:49:48
int -> StringPattern::ID? I am not sure whether th
Yoyo Zhou
2012/09/11 22:35:37
These ints are the FilteredRE2 IDs. I can make a t
| |
49 | |
50 // Rebuild FilteredRE2 from scratch. Needs to be called whenever | |
51 // our set of regexes changes. | |
52 // TODO(yoz): investigate if it could be done incrementally; | |
53 // apparently not supported by FilteredRE2. | |
54 void RebuildMatcher(); | |
55 | |
56 // Clean up StringPatterns in |substring_patterns_|. | |
57 void DeleteSubstringPatterns(); | |
58 | |
59 // Mapping of regex IDs to regexes. | |
60 typedef std::map<StringPattern::ID, const StringPattern*> RegexMap; | |
61 // Mapping of IDs from FilteredRE2 (which are assigned in order) | |
62 // to regex IDs. | |
63 typedef std::vector<StringPattern::ID> RE2IdMap; | |
64 RegexMap regexes_; | |
65 RE2IdMap re2_id_map_; | |
66 | |
67 scoped_ptr<re2::FilteredRE2> filtered_re2_; | |
68 scoped_ptr<SubstringSetMatcher> substring_matcher_; | |
69 | |
70 // The substring patterns from FilteredRE2, which are used in | |
71 // |substring_matcher_| but whose lifetime is managed here. | |
battre
2012/09/11 21:49:48
doesn't lifetime management imply that we delete t
Yoyo Zhou
2012/09/11 22:35:37
DeleteSubstringPatterns does that.
| |
72 std::vector<const StringPattern*> substring_patterns_; | |
73 }; | |
74 | |
75 } // namespace extensions | |
76 | |
77 #endif // CHROME_COMMON_EXTENSIONS_MATCHER_REGEX_SET_MATCHER_H_ | |
OLD | NEW |