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.
5 #include "components/autofill/core/common/autofill_util.h"
10 #include "base/command_line.h"
11 #include "base/i18n/case_conversion.h"
12 #include "base/metrics/field_trial.h"
13 #include "base/strings/string_piece.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "components/autofill/core/common/autofill_switches.h"
23 const char kSplitCharacters
[] = " .,-_@";
25 template <typename Char
>
26 struct Compare
: base::CaseInsensitiveCompareASCII
<Char
> {
27 explicit Compare(bool case_sensitive
) : case_sensitive_(case_sensitive
) {}
28 bool operator()(Char x
, Char y
) const {
29 return case_sensitive_
? (x
== y
)
30 : base::CaseInsensitiveCompareASCII
<Char
>::
40 bool IsFeatureSubstringMatchEnabled() {
41 return base::CommandLine::ForCurrentProcess()->HasSwitch(
42 switches::kEnableSuggestionsWithSubstringMatch
);
45 bool IsKeyboardAccessoryEnabled() {
46 #if defined(OS_ANDROID)
47 return base::CommandLine::ForCurrentProcess()->HasSwitch(
48 switches::kEnableAccessorySuggestionView
) ||
49 (base::FieldTrialList::FindFullName("AutofillKeyboardAccessory") ==
51 !base::CommandLine::ForCurrentProcess()->HasSwitch(
52 switches::kDisableAccessorySuggestionView
));
53 #else // !defined(OS_ANDROID)
58 bool FieldIsSuggestionSubstringStartingOnTokenBoundary(
59 const base::string16
& suggestion
,
60 const base::string16
& field_contents
,
61 bool case_sensitive
) {
62 if (!IsFeatureSubstringMatchEnabled()) {
63 return base::StartsWith(suggestion
, field_contents
,
65 ? base::CompareCase::SENSITIVE
66 : base::CompareCase::INSENSITIVE_ASCII
);
69 return suggestion
.length() >= field_contents
.length() &&
70 GetTextSelectionStart(suggestion
, field_contents
, case_sensitive
) !=
74 size_t GetTextSelectionStart(const base::string16
& suggestion
,
75 const base::string16
& field_contents
,
76 bool case_sensitive
) {
77 const base::string16 kSplitChars
= base::ASCIIToUTF16(kSplitCharacters
);
79 // Loop until we find either the |field_contents| is a prefix of |suggestion|
80 // or character right before the match is one of the splitting characters.
81 for (base::string16::const_iterator it
= suggestion
.begin();
83 it
, suggestion
.end(), field_contents
.begin(), field_contents
.end(),
84 Compare
<base::string16::value_type
>(case_sensitive
))) !=
87 if (it
== suggestion
.begin() ||
88 kSplitChars
.find(*(it
- 1)) != std::string::npos
) {
89 // Returns the character position right after the |field_contents| within
90 // |suggestion| text as a caret position for text selection.
91 return it
- suggestion
.begin() + field_contents
.size();
95 // Unable to find the |field_contents| in |suggestion| text.
96 return base::string16::npos
;
99 } // namespace autofill