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

Side by Side Diff: chrome/browser/extensions/api/declarative/substring_set_matcher.cc

Issue 9390018: Implementation of a Matching strategy for URLs in the Declarative WebRequest API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Refactored for memory improvements Created 8 years, 10 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/browser/extensions/api/declarative/substring_set_matcher.h"
6
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9
10 namespace extensions {
11
12 //
13 // SubstringPattern
14 //
15
16 SubstringPattern::SubstringPattern(const std::string& pattern,
17 SubstringPattern::ID id)
18 : pattern_(pattern), id_(id) {}
19
20 //
21 // SubstringSetMatcher
22 //
23
24 SubstringSetMatcher::SubstringSetMatcher() {}
25
26 SubstringSetMatcher::~SubstringSetMatcher() {}
27
28 void SubstringSetMatcher::RegisterPatterns(
29 const std::vector<const SubstringPattern*>& rules) {
30 for (std::vector<const SubstringPattern*>::const_iterator i =
31 rules.begin(); i != rules.end(); ++i) {
32 DCHECK(patterns_.find((*i)->id()) == patterns_.end());
33 patterns_[(*i)->id()] = *i;
34 }
35 }
36
37 void SubstringSetMatcher::UnregisterPatterns(
38 const std::vector<const SubstringPattern*>& patterns) {
39 for (std::vector<const SubstringPattern*>::const_iterator i =
40 patterns.begin(); i != patterns.end(); ++i) {
41 patterns_.erase((*i)->id());
42 }
43 }
44
45 void SubstringSetMatcher::RegisterAndUnregisterPatterns(
46 const std::vector<const SubstringPattern*>& to_register,
47 const std::vector<const SubstringPattern*>& to_unregister) {
48 // In the Aho-Corasick implementation this will change, the main
49 // implementation will be here, and RegisterPatterns/UnregisterPatterns
50 // will delegate to this version.
51 RegisterPatterns(to_register);
52 UnregisterPatterns(to_unregister);
53 }
54
55 bool SubstringSetMatcher::Match(const std::string& text,
56 std::set<SubstringPattern::ID>* matches) const {
57 for (SubstringPatternSet::const_iterator i = patterns_.begin();
58 i != patterns_.end(); ++i) {
59 if (text.find(i->second->pattern()) != std::string::npos) {
60 if (matches)
61 matches->insert(i->second->id());
62 else
63 return true;
64 }
65 }
66 return matches && !matches->empty();
67 }
68
69 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698