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 namespace { |
| 19 |
| 20 const base::string16 kSplitCharacters = base::ASCIIToUTF16(" .,-_@"); |
| 21 |
| 22 } // namespace |
| 23 |
| 24 bool IsFeatureSubstringMatchEnabled() { |
| 25 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 26 switches::kEnableSuggestionsWithSubstringMatch); |
| 27 } |
| 28 |
| 29 bool ContainsTokenThatStartsWith(const base::string16& suggestion, |
| 30 const base::string16& field_contents, |
| 31 bool case_sensitive) { |
| 32 if (!IsFeatureSubstringMatchEnabled()) |
| 33 return false; |
| 34 |
| 35 std::vector<base::string16> suggestion_tokens; |
| 36 Tokenize(suggestion, kSplitCharacters, &suggestion_tokens); |
| 37 |
| 38 // Check whether the |field_contents| prefixes any of the |suggestion|'s |
| 39 // token. |
| 40 for (const base::string16& token : suggestion_tokens) { |
| 41 if (StartsWith(token, field_contents, case_sensitive)) |
| 42 return true; |
| 43 } |
| 44 |
| 45 return false; |
| 46 } |
| 47 |
| 48 size_t GetTextSelectionStart(const base::string16& suggestion, |
| 49 const base::string16& field_contents) { |
| 50 size_t offset = 0; |
| 51 // Loop until we find either the |field_contents| begins with the |suggestion| |
| 52 // or character right before |offset| is one of the splitting characters. |
| 53 for (base::string16::const_iterator it = suggestion.begin(); |
| 54 (it = std::search( |
| 55 offset + suggestion.begin(), suggestion.end(), |
| 56 field_contents.begin(), field_contents.end(), |
| 57 base::CaseInsensitiveCompare<base::string16::value_type>())) != |
| 58 suggestion.end();) { |
| 59 offset = it - suggestion.begin(); |
| 60 if (offset == 0 || |
| 61 ContainsOnlyChars(suggestion.substr(offset - 1, 1), kSplitCharacters)) { |
| 62 // Set caret position to the end of the |field_contents|. |
| 63 offset += field_contents.size(); |
| 64 return offset; |
| 65 } |
| 66 |
| 67 // Search didn't meet the criteria, advance the suggestion range. |
| 68 ++offset; |
| 69 } |
| 70 |
| 71 // Unable to find the |field_contents| in |suggestion| text. |
| 72 NOTREACHED(); |
| 73 return base::string16::npos; |
| 74 } |
| 75 |
| 76 } // namespace autofill |
OLD | NEW |