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 IsContentsPrefixOfSuggestionToken(const base::string16& suggestion, | |
30 const base::string16& field_contents, | |
31 bool case_sensitive) { | |
32 if (!IsFeatureSubstringMatchEnabled()) | |
33 return false; | |
34 | |
35 bool result = false; | |
36 std::vector<base::string16> suggestion_tokens; | |
37 Tokenize(suggestion, kSplitCharacters, &suggestion_tokens); | |
38 | |
39 // Check whether the |field_contents| prefixes any of the |suggestion|'s | |
40 // token. | |
41 for (const base::string16& token : suggestion_tokens) { | |
42 result = StartsWith(token, field_contents, case_sensitive); | |
Evan Stade
2015/05/11 19:07:12
for(...) {
if (StartsWith...)
return true;
}
Pritam Nikam
2015/05/21 10:58:22
Done.
| |
43 if (result) | |
44 break; | |
45 } | |
46 | |
47 return result; | |
48 } | |
49 | |
50 size_t GetTextSelectionStart(const base::string16& suggestion, | |
51 const base::string16& field_contents) { | |
52 size_t offset = 0; | |
53 base::string16::const_iterator it = suggestion.end(); | |
54 // Loop until we find either the |field_contents| begins with the |suggestion| | |
55 // or character right before |offset| is one of the splitting characters. | |
56 while ((it = std::search( | |
Evan Stade
2015/05/11 19:07:12
this should be a for loop. Why is |it| declared ou
Pritam Nikam
2015/05/21 10:58:22
Ok. I've changed this to:
for (base::string16::
| |
57 offset + suggestion.begin(), suggestion.end(), | |
58 field_contents.begin(), field_contents.end(), | |
59 base::CaseInsensitiveCompare<base::string16::value_type>())) != | |
60 suggestion.end()) { | |
61 offset = it - suggestion.begin(); | |
62 if ((offset == 0) || | |
Evan Stade
2015/05/11 19:07:12
no extra parens
Pritam Nikam
2015/05/21 10:58:22
Done.
| |
63 ContainsOnlyChars(suggestion.substr(offset - 1, 1), kSplitCharacters)) { | |
64 // Set caret position to the end of the |field_contents|. | |
65 offset += field_contents.size(); | |
66 return offset; | |
67 } | |
68 | |
69 // Search didn't meet the criteria, advance the suggestion range. | |
70 ++offset; | |
71 } | |
72 | |
73 // Unable to find the |field_contents| in |suggestion| text. | |
74 return base::string16::npos; | |
75 } | |
76 | |
77 } // namespace autofill | |
OLD | NEW |