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 static const char kSplitCharacters[] = " .,-_@"; | |
21 | |
22 bool IsFeatureSubstringMatchEnabled() { | |
23 return base::CommandLine::ForCurrentProcess()->HasSwitch( | |
24 switches::kEnableSuggestionsWithSubstringMatch); | |
25 } | |
26 | |
27 bool ContainsTokenThatStartsWith(const base::string16& suggestion, | |
28 const base::string16& field_contents, | |
29 bool case_sensitive) { | |
30 if (!IsFeatureSubstringMatchEnabled()) | |
31 return false; | |
32 | |
33 const std::vector<base::string16>& suggestion_tokens = | |
34 base::SplitString(suggestion, base::ASCIIToUTF16(kSplitCharacters), | |
35 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
36 | |
37 // Check whether the |field_contents| prefixes any of the |suggestion|'s | |
38 // tokens. | |
39 for (const base::string16& token : suggestion_tokens) { | |
40 if (StartsWith(token, field_contents, case_sensitive)) | |
41 return true; | |
42 } | |
43 | |
44 return false; | |
45 } | |
46 | |
47 size_t GetTextSelectionStart(const base::string16& suggestion, | |
48 const base::string16& field_contents) { | |
49 // Loop until we find either the |field_contents| is a prefix of |suggestion| | |
50 // or character right before the match is one of the splitting characters. | |
51 for (base::string16::const_iterator it = suggestion.begin(); | |
52 (it = std::search( | |
53 it, suggestion.end(), field_contents.begin(), field_contents.end(), | |
54 base::CaseInsensitiveCompare<base::string16::value_type>())) != | |
55 suggestion.end(); | |
56 ++it) { | |
57 if (it == suggestion.begin() || | |
58 ContainsOnlyChars(suggestion.substr((it - suggestion.begin() - 1), 1), | |
59 base::ASCIIToUTF16(kSplitCharacters))) { | |
please use gerrit instead
2015/06/30 19:06:23
It should be easier to dereference the pointer (*i
Pritam Nikam
2015/07/01 17:26:00
Done.
Did you mean?
base::string16 split_cha
please use gerrit instead
2015/07/03 02:05:43
You're right.
| |
60 // Returns the character position right after the |field_contents| within | |
61 // |suggestion| text as a caret position for text selection. | |
62 return it - suggestion.begin() + field_contents.size(); | |
63 } | |
64 } | |
65 | |
66 // Unable to find the |field_contents| in |suggestion| text. | |
67 NOTREACHED(); | |
68 return base::string16::npos; | |
69 } | |
70 | |
71 } // namespace autofill | |
OLD | NEW |