OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "components/autofill/core/common/autofill_util.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/command_line.h" |
| 10 #include "base/i18n/case_conversion.h" |
| 11 #include "base/strings/string_piece.h" |
| 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "components/autofill/core/common/autofill_switches.h" |
| 15 |
| 16 namespace autofill { |
| 17 |
| 18 bool IsFeatureSubstringMatchEnabled() { |
| 19 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 20 switches::kEnableSuggestionsWithSubstringMatch); |
| 21 } |
| 22 |
| 23 bool IsContentsPrefixOfSuggestionToken(const base::string16& field_suggestion, |
| 24 const base::string16& field_contents, |
| 25 bool case_sensitive) { |
| 26 if (!IsFeatureSubstringMatchEnabled()) { |
| 27 return false; |
| 28 } |
| 29 |
| 30 bool result = false; |
| 31 std::vector<base::string16> suggestion_tokens; |
| 32 Tokenize(field_suggestion, base::ASCIIToUTF16(" .,-_@"), &suggestion_tokens); |
| 33 |
| 34 // Check whether the |field_contents| prefixes any of the |field_suggestion|'s |
| 35 // token. |
| 36 for (auto token : suggestion_tokens) { |
| 37 result = StartsWith(token, field_contents, case_sensitive); |
| 38 if (result) |
| 39 break; |
| 40 } |
| 41 |
| 42 return result; |
| 43 } |
| 44 |
| 45 base::string16::size_type ComputeRange(const base::string16& field_suggestion, |
| 46 const base::string16& field_contents, |
| 47 size_t* start, |
| 48 size_t* end) { |
| 49 base::string16::size_type offset = 0; |
| 50 base::string16::size_type next_offset = 0; |
| 51 do { |
| 52 // Loop through until we find either the |field_contents| begins with the |
| 53 // |suggestion| or character right before |offset| is one of the splitting |
| 54 // characters. |
| 55 base::string16 suggestion(field_suggestion.substr(next_offset)); |
| 56 offset = StartsAt(suggestion, field_contents, false); |
| 57 if (base::string16::npos != offset) { |
| 58 next_offset = offset + next_offset + 1; |
| 59 if ((offset == 0) || ContainsOnlyChars(suggestion.substr(offset - 1, 1), |
| 60 base::ASCIIToUTF16(" .,-_@"))) { |
| 61 offset = next_offset - 1; |
| 62 *start = offset + field_contents.size(); |
| 63 *end = field_suggestion.size(); |
| 64 break; |
| 65 } |
| 66 } |
| 67 } while (base::string16::npos != offset); |
| 68 |
| 69 return offset; |
| 70 } |
| 71 |
| 72 } // namespace autofill |
OLD | NEW |