Chromium Code Reviews| 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 #include "chrome/browser/extensions/api/declarative/substring_set_matcher.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <string.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 SubstringPattern::SubstringPattern(const SubstringPattern& other) | |
| 21 : pattern_(other.pattern_), id_(other.id_) {} | |
| 22 | |
| 23 SubstringPattern::SubstringPattern() | |
| 24 : pattern_(), id_(-1) {} | |
| 25 | |
| 26 SubstringPattern::~SubstringPattern() {} | |
| 27 | |
| 28 SubstringPattern& SubstringPattern::operator=(const SubstringPattern& other) { | |
| 29 pattern_ = other.pattern_; | |
| 30 id_ = other.id_; | |
| 31 return *this; | |
| 32 } | |
| 33 | |
| 34 bool SubstringPattern::operator==(const SubstringPattern& other) const { | |
| 35 return pattern_ == other.pattern_ && id_ == other.id_; | |
|
Matt Perry
2012/02/14 01:38:34
compare IDs first for speed?
battre
2012/02/14 19:32:21
Done.
| |
| 36 } | |
| 37 | |
| 38 bool SubstringPattern::operator!=(const SubstringPattern& other) const { | |
| 39 return !operator==(other); | |
| 40 } | |
| 41 | |
| 42 bool SubstringPattern::operator<(const SubstringPattern& other) const { | |
| 43 if (id_ < other.id_) return true; | |
| 44 if (id_ > other.id_) return false; | |
| 45 return pattern_ < other.pattern_; | |
| 46 } | |
| 47 | |
| 48 // | |
| 49 // SubstringSetMatcher | |
| 50 // | |
| 51 | |
| 52 SubstringSetMatcher::SubstringSetMatcher() {} | |
| 53 | |
| 54 SubstringSetMatcher::~SubstringSetMatcher() {} | |
| 55 | |
| 56 void SubstringSetMatcher::RegisterPatterns( | |
| 57 const std::vector<SubstringPattern>& rules) { | |
| 58 for (std::vector<SubstringPattern>::const_iterator i = | |
| 59 rules.begin(); i != rules.end(); ++i) { | |
| 60 DCHECK(std::find(patterns_.begin(), patterns_.end(), *i) == | |
| 61 patterns_.end()); | |
| 62 patterns_.push_back(*i); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 void SubstringSetMatcher::UnregisterPatterns( | |
| 67 const std::vector<SubstringPattern>& patterns) { | |
| 68 for (std::vector<SubstringPattern>::const_iterator i = patterns.begin(); | |
| 69 i != patterns.end(); ++i) { | |
| 70 std::remove(patterns_.begin(), patterns_.end(), *i); | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 void SubstringSetMatcher::RegisterAndUnregisterPatterns( | |
| 75 const std::vector<SubstringPattern>& to_register, | |
| 76 const std::vector<SubstringPattern>& to_unregister) { | |
| 77 // In the Aho-Corasick implementation this will change, the main | |
| 78 // implementation will be here, and RegisterPatterns/UnregisterPatterns | |
| 79 // will delegate to this version. | |
| 80 RegisterPatterns(to_register); | |
| 81 UnregisterPatterns(to_unregister); | |
| 82 } | |
| 83 | |
| 84 bool SubstringSetMatcher::Match(const std::string& text, | |
| 85 std::set<SubstringPattern::ID>* matches) const { | |
| 86 for (std::vector<SubstringPattern>::const_iterator i = patterns_.begin(); | |
| 87 i != patterns_.end(); ++i) { | |
| 88 if (text.find(i->pattern()) != std::string::npos) { | |
| 89 if (matches) | |
| 90 matches->insert(i->id()); | |
| 91 else | |
| 92 return true; | |
| 93 } | |
| 94 } | |
| 95 return matches && !matches->empty(); | |
| 96 } | |
| 97 | |
| 98 void SubstringSetMatcher::Clear() { | |
| 99 patterns_.clear(); | |
| 100 } | |
| 101 | |
| 102 } // namespace extensions | |
| OLD | NEW |