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 namespace { | |
21 | |
22 template <typename Char> | |
23 struct Compare { | |
24 Compare(bool case_sensitive) : case_sensitive_(case_sensitive) {} | |
25 bool operator()(Char x, Char y) const { | |
26 return case_sensitive_ ? (x == y) : (tolower(x) == tolower(y)); | |
27 } | |
28 | |
29 private: | |
30 bool case_sensitive_; | |
31 }; | |
32 | |
33 } // namespace | |
34 | |
35 static const char kSplitCharacters[] = " .,-_@"; | |
36 | |
37 bool IsFeatureSubstringMatchEnabled() { | |
38 return base::CommandLine::ForCurrentProcess()->HasSwitch( | |
39 switches::kEnableSuggestionsWithSubstringMatch); | |
40 } | |
41 | |
42 bool ContainsTokenThatStartsWith(const base::string16& suggestion, | |
43 const base::string16& field_contents, | |
44 bool case_sensitive) { | |
45 return IsFeatureSubstringMatchEnabled() && | |
46 suggestion.length() >= field_contents.length() && | |
47 GetTextSelectionStart(suggestion, field_contents, case_sensitive) != | |
48 base::string16::npos; | |
49 } | |
50 | |
51 size_t GetTextSelectionStart(const base::string16& suggestion, | |
52 const base::string16& field_contents, | |
53 bool case_sensitive) { | |
54 // Loop until we find either the |field_contents| is a prefix of |suggestion| | |
55 // or character right before the match is one of the splitting characters. | |
56 for (base::string16::const_iterator it = suggestion.begin(); | |
57 (it = std::search( | |
58 it, suggestion.end(), field_contents.begin(), field_contents.end(), | |
59 Compare<base::string16::value_type>(case_sensitive))) != | |
please use gerrit instead
2015/07/03 02:05:43
case_sensitive ? Compare<...>() : base::CaseInsens
Pritam Nikam
2015/07/03 16:21:27
This will not compile.
In that case, how about:
please use gerrit instead
2015/07/03 22:05:47
Looks good.
| |
60 suggestion.end(); | |
61 ++it) { | |
62 static const base::string16 split_chars( | |
please use gerrit instead
2015/07/03 02:05:43
1) No need to make it static.
2) Constants are nam
Pritam Nikam
2015/07/03 16:21:27
Done.
| |
63 base::ASCIIToUTF16(kSplitCharacters)); | |
64 if (it == suggestion.begin() || | |
65 split_chars.find(*(it - 1)) != std::string::npos) { | |
66 // Returns the character position right after the |field_contents| within | |
67 // |suggestion| text as a caret position for text selection. | |
68 return it - suggestion.begin() + field_contents.size(); | |
69 } | |
70 } | |
71 | |
72 // Unable to find the |field_contents| in |suggestion| text. | |
73 return base::string16::npos; | |
74 } | |
75 | |
76 } // namespace autofill | |
OLD | NEW |