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

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: Modified to resolve bot breakages. 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 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);
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(
vabr (Chromium) 2015/07/09 06:17:51 nit: Modifying iterator in the exit condition look
Pritam Nikam 2015/07/10 16:58:46 Done. This was changed to for(...) loop, original
vabr (Chromium) 2015/07/13 14:04:25 Ah, sorry about the back and forth, I did not real
Pritam Nikam 2015/07/14 10:30:07 Done. Restored to for (...) cluase.
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698