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

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: Fixed unittest for Android bot. 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 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 FieldIsSuggestionSubstringStartingOnTokenBoundary(
44 const base::string16& suggestion,
45 const base::string16& field_contents,
46 bool case_sensitive) {
47 return IsFeatureSubstringMatchEnabled() &&
48 suggestion.length() >= field_contents.length() &&
49 GetTextSelectionStart(suggestion, field_contents, case_sensitive) !=
50 base::string16::npos;
51 }
52
53 size_t GetTextSelectionStart(const base::string16& suggestion,
54 const base::string16& field_contents,
55 bool case_sensitive) {
56 const base::string16 kSplitChars = base::ASCIIToUTF16(kSplitCharacters);
57 base::string16::const_iterator it = suggestion.begin();
58
59 // Loop until we find either the |field_contents| is a prefix of |suggestion|
60 // or character right before the match is one of the splitting characters.
61 do {
62 it = std::search(it, suggestion.end(), field_contents.begin(),
63 field_contents.end(),
64 Compare<base::string16::value_type>(case_sensitive));
65 if (it == suggestion.end())
66 break;
vabr (Chromium) 2015/07/13 14:04:26 nit: Why not just return base::string16::npos;
Pritam Nikam 2015/07/14 10:30:07 Restored to for(...) instead.
67
68 if (it == suggestion.begin() ||
69 kSplitChars.find(*(it - 1)) != std::string::npos) {
70 // Returns the character position right after the |field_contents| within
71 // |suggestion| text as a caret position for text selection.
72 return it - suggestion.begin() + field_contents.size();
73 }
74
75 ++it;
76 } while (it != suggestion.end());
77
78 // Unable to find the |field_contents| in |suggestion| text.
79 return base::string16::npos;
80 }
81
82 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698