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> | |
please use gerrit instead
2015/06/29 22:06:29
Also <algorithm> for std::search.
Pritam Nikam
2015/06/30 15:05:50
Done.
| |
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_split.h" | |
13 #include "base/strings/string_util.h" | |
14 #include "base/strings/utf_string_conversions.h" | |
15 #include "components/autofill/core/common/autofill_switches.h" | |
16 | |
17 namespace autofill { | |
18 | |
19 static const char kSplitCharacters[] = " .,-_@"; | |
20 | |
21 bool IsFeatureSubstringMatchEnabled() { | |
22 return base::CommandLine::ForCurrentProcess()->HasSwitch( | |
23 switches::kEnableSuggestionsWithSubstringMatch); | |
24 } | |
25 | |
26 bool ContainsTokenThatStartsWith(const base::string16& suggestion, | |
27 const base::string16& field_contents, | |
28 bool case_sensitive) { | |
29 if (!IsFeatureSubstringMatchEnabled()) | |
30 return false; | |
31 | |
32 std::vector<base::string16> suggestion_tokens = | |
please use gerrit instead
2015/06/29 22:06:29
You can probably save one copy operation by using
Pritam Nikam
2015/06/30 15:05:51
Done.
| |
33 base::SplitString(suggestion, base::ASCIIToUTF16(kSplitCharacters), | |
34 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
35 | |
36 // Check whether the |field_contents| prefixes any of the |suggestion|'s | |
37 // token. | |
please use gerrit instead
2015/06/29 22:06:29
s/token/tokens
Pritam Nikam
2015/06/30 15:05:51
Done.
| |
38 for (const base::string16& token : suggestion_tokens) { | |
39 if (StartsWith(token, field_contents, case_sensitive)) | |
40 return true; | |
41 } | |
42 | |
43 return false; | |
44 } | |
45 | |
46 size_t GetTextSelectionStart(const base::string16& suggestion, | |
47 const base::string16& field_contents) { | |
48 size_t offset = 0; | |
please use gerrit instead
2015/06/29 22:06:29
You don't need |offset| variable. You can use "std
Pritam Nikam
2015/06/30 15:05:50
Done.
| |
49 // Loop until we find either the |field_contents| begins with the |suggestion| | |
please use gerrit instead
2015/06/29 22:06:29
s/begins with/is a prefix of
Right?
Pritam Nikam
2015/06/30 15:05:50
Done.
| |
50 // or character right before |offset| is one of the splitting characters. | |
51 for (base::string16::const_iterator it = suggestion.begin(); | |
52 (it = std::search( | |
53 offset + suggestion.begin(), suggestion.end(), | |
54 field_contents.begin(), field_contents.end(), | |
55 base::CaseInsensitiveCompare<base::string16::value_type>())) != | |
56 suggestion.end();) { | |
57 offset = it - suggestion.begin(); | |
58 if (offset == 0 || | |
59 ContainsOnlyChars(suggestion.substr(offset - 1, 1), | |
60 base::ASCIIToUTF16(kSplitCharacters))) { | |
61 // Set caret position to the end of the |field_contents|. | |
62 offset += field_contents.size(); | |
63 return offset; | |
64 } | |
65 | |
66 // Search didn't meet the criteria, advance the suggestion range. | |
67 ++offset; | |
68 } | |
69 | |
70 // Unable to find the |field_contents| in |suggestion| text. | |
71 NOTREACHED(); | |
72 return base::string16::npos; | |
73 } | |
74 | |
75 } // namespace autofill | |
OLD | NEW |