Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(218)

Side by Side Diff: components/subresource_filter/core/common/fuzzy_pattern_matching.cc

Issue 2793993002: [subresource_filter] Replace KMP by std::search. (Closed)
Patch Set: Fix DCHECK. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/subresource_filter/core/common/fuzzy_pattern_matching.h" 5 #include "components/subresource_filter/core/common/fuzzy_pattern_matching.h"
6 6
7 #include <algorithm>
8
7 namespace subresource_filter { 9 namespace subresource_filter {
8 10
9 namespace { 11 namespace {
10 12
11 bool StartsWithFuzzyImpl(base::StringPiece text, base::StringPiece subpattern) { 13 bool StartsWithFuzzyImpl(base::StringPiece text, base::StringPiece subpattern) {
12 DCHECK_LE(subpattern.size(), text.size()); 14 DCHECK_LE(subpattern.size(), text.size());
13 15
14 for (size_t i = 0; i != subpattern.size(); ++i) { 16 for (size_t i = 0; i != subpattern.size(); ++i) {
15 const char text_char = text[i]; 17 const char text_char = text[i];
16 const char pattern_char = subpattern[i]; 18 const char pattern_char = subpattern[i];
(...skipping 11 matching lines...) Expand all
28 return subpattern.size() <= text.size() && 30 return subpattern.size() <= text.size() &&
29 StartsWithFuzzyImpl(text, subpattern); 31 StartsWithFuzzyImpl(text, subpattern);
30 } 32 }
31 33
32 bool EndsWithFuzzy(base::StringPiece text, base::StringPiece subpattern) { 34 bool EndsWithFuzzy(base::StringPiece text, base::StringPiece subpattern) {
33 return subpattern.size() <= text.size() && 35 return subpattern.size() <= text.size() &&
34 StartsWithFuzzyImpl(text.substr(text.size() - subpattern.size()), 36 StartsWithFuzzyImpl(text.substr(text.size() - subpattern.size()),
35 subpattern); 37 subpattern);
36 } 38 }
37 39
40 size_t FindFuzzy(base::StringPiece text,
41 base::StringPiece subpattern,
42 size_t from) {
43 if (from > text.size())
44 return base::StringPiece::npos;
45 if (subpattern.empty())
46 return from;
47
48 auto fuzzy_compare = [](char text_char, char subpattern_char) {
49 return text_char == subpattern_char ||
50 (subpattern_char == kSeparatorPlaceholder && IsSeparator(text_char));
51 };
52
53 base::StringPiece::const_iterator found =
54 std::search(text.begin() + from, text.end(), subpattern.begin(),
55 subpattern.end(), fuzzy_compare);
56 return found == text.end() ? base::StringPiece::npos : found - text.begin();
57 }
58
38 } // namespace subresource_filter 59 } // namespace subresource_filter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698