Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: components/autofill/core/common/autofill_util.cc

Issue 962673004: [Autofill/Autocomplete Feature] Substring matching instead of prefix matching. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 : base::CaseInsensitiveCompare<Char> {
24 Compare(bool case_sensitive) : case_sensitive_(case_sensitive) {}
please use gerrit instead 2015/07/06 20:15:18 1) Mark this constructor "explicit", please. This
Pritam Nikam 2015/07/07 16:18:32 Done.
25 bool operator()(Char x, Char y) const {
26 return case_sensitive_ ? (x == y) : (tolower(x) == tolower(y));
please use gerrit instead 2015/07/06 20:15:18 You forgot to change tolower() to base::CaseInsens
Pritam Nikam 2015/07/07 16:18:31 Done.
27 }
28
29 private:
30 bool case_sensitive_;
31 };
32
33 } // namespace
34
35 static const char kSplitCharacters[] = " .,-_@";
please use gerrit instead 2015/07/06 20:15:18 Put this into unnamed namespace please.
Pritam Nikam 2015/07/07 16:18:31 Done.
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 const base::string16 kSplitChars = base::ASCIIToUTF16(kSplitCharacters);
55
56 // Loop until we find either the |field_contents| is a prefix of |suggestion|
57 // or character right before the match is one of the splitting characters.
58 for (base::string16::const_iterator it = suggestion.begin();
59 (it = std::search(
60 it, suggestion.end(), field_contents.begin(), field_contents.end(),
61 Compare<base::string16::value_type>(case_sensitive))) !=
62 suggestion.end();
63 ++it) {
64 if (it == suggestion.begin() ||
65 kSplitChars.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 size_t GetTextSelectionStart(const base::string16& suggestion,
77 const base::string16& field_contents) {
78 // Ignores case.
79 return GetTextSelectionStart(suggestion, field_contents, false);
80 }
81
82 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698