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 <algorithm> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/command_line.h" |
| 11 #include "base/i18n/case_conversion.h" |
| 12 #include "base/strings/string_piece.h" |
| 13 #include "base/strings/string_split.h" |
| 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "components/autofill/core/common/autofill_switches.h" |
| 17 |
| 18 namespace autofill { |
| 19 |
| 20 namespace { |
| 21 |
| 22 const char kSplitCharacters[] = " .,-_@"; |
| 23 |
| 24 template <typename Char> |
| 25 struct Compare : base::CaseInsensitiveCompareASCII<Char> { |
| 26 explicit Compare(bool case_sensitive) : case_sensitive_(case_sensitive) {} |
| 27 bool operator()(Char x, Char y) const { |
| 28 return case_sensitive_ ? (x == y) |
| 29 : base::CaseInsensitiveCompareASCII<Char>:: |
| 30 operator()(x, y); |
| 31 } |
| 32 |
| 33 private: |
| 34 bool case_sensitive_; |
| 35 }; |
| 36 |
| 37 } // namespace |
| 38 |
| 39 bool IsFeatureSubstringMatchEnabled() { |
| 40 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 41 switches::kEnableSuggestionsWithSubstringMatch); |
| 42 } |
| 43 |
| 44 bool FieldIsSuggestionSubstringStartingOnTokenBoundary( |
| 45 const base::string16& suggestion, |
| 46 const base::string16& field_contents, |
| 47 bool case_sensitive) { |
| 48 if (!IsFeatureSubstringMatchEnabled()) { |
| 49 return base::StartsWith(suggestion, field_contents, |
| 50 case_sensitive |
| 51 ? base::CompareCase::SENSITIVE |
| 52 : base::CompareCase::INSENSITIVE_ASCII); |
| 53 } |
| 54 |
| 55 return suggestion.length() >= field_contents.length() && |
| 56 GetTextSelectionStart(suggestion, field_contents, case_sensitive) != |
| 57 base::string16::npos; |
| 58 } |
| 59 |
| 60 size_t GetTextSelectionStart(const base::string16& suggestion, |
| 61 const base::string16& field_contents, |
| 62 bool case_sensitive) { |
| 63 const base::string16 kSplitChars = base::ASCIIToUTF16(kSplitCharacters); |
| 64 |
| 65 // Loop until we find either the |field_contents| is a prefix of |suggestion| |
| 66 // or character right before the match is one of the splitting characters. |
| 67 for (base::string16::const_iterator it = suggestion.begin(); |
| 68 (it = std::search( |
| 69 it, suggestion.end(), field_contents.begin(), field_contents.end(), |
| 70 Compare<base::string16::value_type>(case_sensitive))) != |
| 71 suggestion.end(); |
| 72 ++it) { |
| 73 if (it == suggestion.begin() || |
| 74 kSplitChars.find(*(it - 1)) != std::string::npos) { |
| 75 // Returns the character position right after the |field_contents| within |
| 76 // |suggestion| text as a caret position for text selection. |
| 77 return it - suggestion.begin() + field_contents.size(); |
| 78 } |
| 79 } |
| 80 |
| 81 // Unable to find the |field_contents| in |suggestion| text. |
| 82 return base::string16::npos; |
| 83 } |
| 84 |
| 85 } // namespace autofill |
OLD | NEW |