| 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 "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 SubstringPattern::~SubstringPattern() {} |
| 21 |
| 22 bool SubstringPattern::operator<(const SubstringPattern& rhs) const { |
| 23 if (id_ < rhs.id_) return true; |
| 24 if (id_ > rhs.id_) return false; |
| 25 return pattern_ < rhs.pattern_; |
| 26 } |
| 27 |
| 28 // |
| 29 // SubstringSetMatcher |
| 30 // |
| 31 |
| 32 SubstringSetMatcher::SubstringSetMatcher() {} |
| 33 |
| 34 SubstringSetMatcher::~SubstringSetMatcher() {} |
| 35 |
| 36 void SubstringSetMatcher::RegisterPatterns( |
| 37 const std::vector<const SubstringPattern*>& rules) { |
| 38 for (std::vector<const SubstringPattern*>::const_iterator i = |
| 39 rules.begin(); i != rules.end(); ++i) { |
| 40 DCHECK(patterns_.find((*i)->id()) == patterns_.end()); |
| 41 patterns_[(*i)->id()] = *i; |
| 42 } |
| 43 } |
| 44 |
| 45 void SubstringSetMatcher::UnregisterPatterns( |
| 46 const std::vector<const SubstringPattern*>& patterns) { |
| 47 for (std::vector<const SubstringPattern*>::const_iterator i = |
| 48 patterns.begin(); i != patterns.end(); ++i) { |
| 49 patterns_.erase((*i)->id()); |
| 50 } |
| 51 } |
| 52 |
| 53 void SubstringSetMatcher::RegisterAndUnregisterPatterns( |
| 54 const std::vector<const SubstringPattern*>& to_register, |
| 55 const std::vector<const SubstringPattern*>& to_unregister) { |
| 56 // In the Aho-Corasick implementation this will change, the main |
| 57 // implementation will be here, and RegisterPatterns/UnregisterPatterns |
| 58 // will delegate to this version. |
| 59 RegisterPatterns(to_register); |
| 60 UnregisterPatterns(to_unregister); |
| 61 } |
| 62 |
| 63 bool SubstringSetMatcher::Match(const std::string& text, |
| 64 std::set<SubstringPattern::ID>* matches) const { |
| 65 for (SubstringPatternSet::const_iterator i = patterns_.begin(); |
| 66 i != patterns_.end(); ++i) { |
| 67 if (text.find(i->second->pattern()) != std::string::npos) { |
| 68 if (matches) |
| 69 matches->insert(i->second->id()); |
| 70 else |
| 71 return true; |
| 72 } |
| 73 } |
| 74 return matches && !matches->empty(); |
| 75 } |
| 76 |
| 77 } // namespace extensions |
| OLD | NEW |