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