| 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 // |
| 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 |
| OLD | NEW |