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 } | |
Evan Stade
2015/04/27 19:25:21
no curlies
Pritam Nikam
2015/04/28 14:45:55
Done.
| |
35 | |
36 bool result = false; | |
37 std::vector<base::string16> suggestion_tokens; | |
38 Tokenize(suggestion, kSplitCharacters, &suggestion_tokens); | |
39 | |
40 // Check whether the |field_contents| prefixes any of the |suggestion|'s | |
41 // token. | |
42 for (const base::string16& token : suggestion_tokens) { | |
43 result = StartsWith(token, field_contents, case_sensitive); | |
44 if (result) | |
45 break; | |
46 } | |
47 | |
48 return result; | |
49 } | |
50 | |
51 size_t GetTextSelectionStart(const base::string16& suggestion, | |
52 const base::string16& field_contents) { | |
53 size_t offset = 0; | |
54 base::string16::const_iterator it = suggestion.end(); | |
55 // Loop until we find either the |field_contents| begins with the |suggestion| | |
56 // or character right before |offset| is one of the splitting characters. | |
57 while ((it = std::search( | |
58 offset + suggestion.begin(), suggestion.end(), | |
59 field_contents.begin(), field_contents.end(), | |
60 base::CaseInsensitiveCompare<base::string16::value_type>())) != | |
61 suggestion.end()) { | |
62 offset = it - suggestion.begin(); | |
63 if ((it == suggestion.begin()) || | |
Evan Stade
2015/04/27 19:25:21
offset == 0
Pritam Nikam
2015/04/28 14:45:55
Done.
| |
64 ContainsOnlyChars(suggestion.substr(offset - 1, 1), kSplitCharacters)) { | |
65 offset += field_contents.size(); | |
66 break; | |
67 } | |
68 | |
69 ++offset; | |
Evan Stade
2015/04/27 19:25:21
what is the purpose of this?
Pritam Nikam
2015/04/28 14:45:55
It will advance the suggestion iterator (or first1
| |
70 } | |
71 | |
72 return (it != suggestion.end()) ? offset : base::string16::npos; | |
Evan Stade
2015/04/27 19:25:21
is it valid if it == suggestion.end()? It seems li
Pritam Nikam
2015/04/28 14:45:55
it == suggestion.end() is equivalent to std::searc
Evan Stade
2015/05/11 19:07:12
in which case you probably shouldn't have called G
Pritam Nikam
2015/05/21 10:58:22
Yes, thats right.
But there is no restriction on d
Evan Stade
2015/05/22 18:26:48
then you should DCHECK and not attempt to handle t
| |
73 } | |
74 | |
75 } // namespace autofill | |
OLD | NEW |