OLD | NEW |
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 Loading... |
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 |
OLD | NEW |