Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Side by Side Diff: chrome/common/extensions/matcher/regex_set_matcher.cc

Issue 10910179: Event matching by regular expression matching on URLs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 #include "chrome/common/extensions/matcher/regex_set_matcher.h"
6
7 #include "base/logging.h"
8 #include "base/string_util.h"
9 #include "base/stl_util.h"
10 #include "chrome/common/extensions/matcher/substring_set_matcher.h"
11 #include "third_party/re2/re2/filtered_re2.h"
12 #include "third_party/re2/re2/re2.h"
13
14 namespace extensions {
15
16 RegexSetMatcher::RegexSetMatcher() {}
17
18 RegexSetMatcher::~RegexSetMatcher() {
19 DeleteSubstringPatterns();
20 }
21
22 void RegexSetMatcher::AddPatterns(
23 const std::vector<const StringPattern*>& regex_list) {
24 if (regex_list.empty())
25 return;
26 for (size_t i = 0; i < regex_list.size(); ++i) {
27 regexes_[regex_list[i]->id()] = regex_list[i];
28 }
29
30 RebuildMatcher();
31 }
32
33 void RegexSetMatcher::ClearPatterns() {
34 regexes_.clear();
35 RebuildMatcher();
36 }
37
38 bool RegexSetMatcher::Match(const std::string& text,
39 std::set<StringPattern::ID>* matches) const {
40 if (regexes_.empty())
41 return false;
42 if (!filtered_re2_.get()) {
43 LOG(ERROR) << "RegexSetMatcher was not initialized";
44 return false;
45 }
46
47 // FilteredRE2 expects lowercase for prefiltering (but we still
48 // match case-sensitively).
49 std::vector<int> atoms(FindSubstringMatches(
50 StringToLowerASCII(text)));
51
52 std::vector<int> re2_ids;
53 filtered_re2_->AllMatches(text, atoms, &re2_ids);
54
55 std::set<StringPattern::ID> matched_ids;
56 for (size_t i = 0; i < re2_ids.size(); ++i) {
57 StringPattern::ID id = re2_id_map_[re2_ids[i]];
58 matches->insert(id);
59 }
60 return !matched_ids.empty();
61 }
62
63 std::vector<int> RegexSetMatcher::FindSubstringMatches(
64 const std::string& text) const {
65 std::set<int> atoms_set;
66 substring_matcher_->Match(text, &atoms_set);
67 return std::vector<int>(atoms_set.begin(), atoms_set.end());
68 }
69
70 void RegexSetMatcher::RebuildMatcher() {
71 re2_id_map_.clear();
72 filtered_re2_.reset(new re2::FilteredRE2());
73 if (regexes_.empty())
74 return;
75
76 for (RegexMap::iterator it = regexes_.begin(); it != regexes_.end(); ++it) {
77 int re2_id;
78 RE2::ErrorCode error = filtered_re2_->Add(
79 it->second->pattern(), RE2::DefaultOptions, &re2_id);
80 if (error == RE2::NoError) {
81 DCHECK_EQ((int)re2_id_map_.size(), re2_id);
82 re2_id_map_.push_back(it->first);
83 } else {
84 // TODO(yoz): Return an unparseable regex error as soon as possible.
85 LOG(ERROR) << "Could not parse regex (id=" << it->first << ", "
86 << it->second->pattern() << ")";
87 }
88 }
89
90 std::vector<std::string> strings_to_match;
91 filtered_re2_->Compile(&strings_to_match);
92 DeleteSubstringPatterns();
93
94 // Build SubstringSetMatcher from |strings_to_match|.
95 for (size_t i = 0; i < strings_to_match.size(); ++i) {
96 substring_patterns_.push_back(
97 new StringPattern(strings_to_match[i], i));
98 }
99 substring_matcher_.reset(new SubstringSetMatcher);
100 substring_matcher_->RegisterPatterns(substring_patterns_);
101 }
102
103 void RegexSetMatcher::DeleteSubstringPatterns() {
104 STLDeleteElements(&substring_patterns_);
105 }
106
107 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698