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 static const char kSplitCharacters[] = " .,-_@"; | |
23 | |
24 template <typename Char> | |
25 struct Compare : base::CaseInsensitiveCompare<Char> { | |
26 explicit Compare(bool case_sensitive) : case_sensitive_(case_sensitive) {} | |
27 bool operator()(Char x, Char y) const { | |
28 return case_sensitive_ ? (x == y) : base::CaseInsensitiveCompare<Char>:: | |
29 operator()(x, y); | |
please use gerrit instead
2015/07/07 18:09:12
nit: is this clang formatted?
Pritam Nikam
2015/07/08 06:23:06
Yes, Its a clang formatted.
| |
30 } | |
31 | |
32 private: | |
33 bool case_sensitive_; | |
34 }; | |
35 | |
36 } // namespace | |
37 | |
38 bool IsFeatureSubstringMatchEnabled() { | |
39 return base::CommandLine::ForCurrentProcess()->HasSwitch( | |
40 switches::kEnableSuggestionsWithSubstringMatch); | |
41 } | |
42 | |
43 bool ContainsTokenThatStartsWith(const base::string16& suggestion, | |
44 const base::string16& field_contents, | |
45 bool case_sensitive) { | |
46 return IsFeatureSubstringMatchEnabled() && | |
47 suggestion.length() >= field_contents.length() && | |
48 GetTextSelectionStart(suggestion, field_contents, case_sensitive) != | |
49 base::string16::npos; | |
50 } | |
51 | |
52 size_t GetTextSelectionStart(const base::string16& suggestion, | |
53 const base::string16& field_contents, | |
54 bool case_sensitive) { | |
55 const base::string16 kSplitChars = base::ASCIIToUTF16(kSplitCharacters); | |
56 | |
57 // Loop until we find either the |field_contents| is a prefix of |suggestion| | |
58 // or character right before the match is one of the splitting characters. | |
59 for (base::string16::const_iterator it = suggestion.begin(); | |
60 (it = std::search( | |
61 it, suggestion.end(), field_contents.begin(), field_contents.end(), | |
62 Compare<base::string16::value_type>(case_sensitive))) != | |
63 suggestion.end(); | |
64 ++it) { | |
65 if (it == suggestion.begin() || | |
66 kSplitChars.find(*(it - 1)) != std::string::npos) { | |
67 // Returns the character position right after the |field_contents| within | |
68 // |suggestion| text as a caret position for text selection. | |
69 return it - suggestion.begin() + field_contents.size(); | |
70 } | |
71 } | |
72 | |
73 // Unable to find the |field_contents| in |suggestion| text. | |
74 return base::string16::npos; | |
75 } | |
76 | |
77 } // namespace autofill | |
OLD | NEW |