Chromium Code Reviews| Index: chrome/browser/extensions/api/declarative/substring_set_matcher.cc |
| diff --git a/chrome/browser/extensions/api/declarative/substring_set_matcher.cc b/chrome/browser/extensions/api/declarative/substring_set_matcher.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..82db0db7e689274f21bcca8da789e5ef24090e08 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/declarative/substring_set_matcher.cc |
| @@ -0,0 +1,102 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/extensions/api/declarative/substring_set_matcher.h" |
| + |
| +#include <algorithm> |
| +#include <string.h> |
| + |
| +namespace extensions { |
| + |
| +// |
| +// SubstringPattern |
| +// |
| + |
| +SubstringPattern::SubstringPattern(const std::string& pattern, |
| + SubstringPattern::ID id) |
| + : pattern_(pattern), id_(id) {} |
| + |
| +SubstringPattern::SubstringPattern(const SubstringPattern& other) |
| + : pattern_(other.pattern_), id_(other.id_) {} |
| + |
| +SubstringPattern::SubstringPattern() |
| + : pattern_(), id_(-1) {} |
| + |
| +SubstringPattern::~SubstringPattern() {} |
| + |
| +SubstringPattern& SubstringPattern::operator=(const SubstringPattern& other) { |
| + pattern_ = other.pattern_; |
| + id_ = other.id_; |
| + return *this; |
| +} |
| + |
| +bool SubstringPattern::operator==(const SubstringPattern& other) const { |
| + 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.
|
| +} |
| + |
| +bool SubstringPattern::operator!=(const SubstringPattern& other) const { |
| + return !operator==(other); |
| +} |
| + |
| +bool SubstringPattern::operator<(const SubstringPattern& other) const { |
| + if (id_ < other.id_) return true; |
| + if (id_ > other.id_) return false; |
| + return pattern_ < other.pattern_; |
| +} |
| + |
| +// |
| +// SubstringSetMatcher |
| +// |
| + |
| +SubstringSetMatcher::SubstringSetMatcher() {} |
| + |
| +SubstringSetMatcher::~SubstringSetMatcher() {} |
| + |
| +void SubstringSetMatcher::RegisterPatterns( |
| + const std::vector<SubstringPattern>& rules) { |
| + for (std::vector<SubstringPattern>::const_iterator i = |
| + rules.begin(); i != rules.end(); ++i) { |
| + DCHECK(std::find(patterns_.begin(), patterns_.end(), *i) == |
| + patterns_.end()); |
| + patterns_.push_back(*i); |
| + } |
| +} |
| + |
| +void SubstringSetMatcher::UnregisterPatterns( |
| + const std::vector<SubstringPattern>& patterns) { |
| + for (std::vector<SubstringPattern>::const_iterator i = patterns.begin(); |
| + i != patterns.end(); ++i) { |
| + std::remove(patterns_.begin(), patterns_.end(), *i); |
| + } |
| +} |
| + |
| +void SubstringSetMatcher::RegisterAndUnregisterPatterns( |
| + const std::vector<SubstringPattern>& to_register, |
| + const std::vector<SubstringPattern>& to_unregister) { |
| + // In the Aho-Corasick implementation this will change, the main |
| + // implementation will be here, and RegisterPatterns/UnregisterPatterns |
| + // will delegate to this version. |
| + RegisterPatterns(to_register); |
| + UnregisterPatterns(to_unregister); |
| +} |
| + |
| +bool SubstringSetMatcher::Match(const std::string& text, |
| + std::set<SubstringPattern::ID>* matches) const { |
| + for (std::vector<SubstringPattern>::const_iterator i = patterns_.begin(); |
| + i != patterns_.end(); ++i) { |
| + if (text.find(i->pattern()) != std::string::npos) { |
| + if (matches) |
| + matches->insert(i->id()); |
| + else |
| + return true; |
| + } |
| + } |
| + return matches && !matches->empty(); |
| +} |
| + |
| +void SubstringSetMatcher::Clear() { |
| + patterns_.clear(); |
| +} |
| + |
| +} // namespace extensions |