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 CaseSensitivity case_sensitivity) { |
| 26 if (field_suggestion.empty() || field_contents.empty()) { |
| 27 return false; |
| 28 } |
| 29 |
| 30 bool result = false; |
| 31 bool case_sensitive = (CASE_SENSITIVE == case_sensitivity); |
| 32 base::string16 search_string = |
| 33 case_sensitive ? field_contents : base::i18n::ToLower(field_contents); |
| 34 base::string16 suggestion_string = |
| 35 case_sensitive ? field_suggestion : base::i18n::ToLower(field_suggestion); |
| 36 base::string16::size_type offset = suggestion_string.find(search_string); |
| 37 if (base::string16::npos != offset) { |
| 38 std::vector<base::string16> suggestion_tokens, search_tokens; |
| 39 Tokenize(suggestion_string, base::ASCIIToUTF16(" .,-_@"), |
| 40 &suggestion_tokens); |
| 41 Tokenize(search_string, base::ASCIIToUTF16(" .,-_@"), &search_tokens); |
| 42 |
| 43 // Check whether the |search_string|'s first token matches any of the |
| 44 // |suggestion_string|'s token. |
| 45 if (suggestion_tokens.size() != 0 && search_tokens.size() != 0) { |
| 46 for (auto token : suggestion_tokens) { |
| 47 result = StartsWith(token, search_tokens[0], case_sensitive); |
| 48 if (result) |
| 49 break; |
| 50 } |
| 51 } |
| 52 } |
| 53 |
| 54 return result; |
| 55 } |
| 56 |
| 57 base::string16::size_type ComputeRange(const base::string16& field_suggestion, |
| 58 const base::string16& field_contents, |
| 59 size_t* start, |
| 60 size_t* end) { |
| 61 base::string16 user_input = |
| 62 base::i18n::ToLower(base::string16(field_contents)); |
| 63 base::string16 suggestion = |
| 64 base::i18n::ToLower(base::string16(field_suggestion)); |
| 65 base::string16::size_type offset = suggestion.find(user_input); |
| 66 if (base::string16::npos != offset) { |
| 67 *start = offset + user_input.size(); |
| 68 *end = suggestion.size(); |
| 69 } |
| 70 |
| 71 return offset; |
| 72 } |
| 73 |
| 74 } // namespace autofill |
OLD | NEW |