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

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: Just code rebase. Created 5 years, 6 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 <vector>
8
9 #include "base/command_line.h"
10 #include "base/i18n/case_conversion.h"
11 #include "base/strings/string_piece.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "components/autofill/core/common/autofill_switches.h"
15
16 namespace autofill {
17
18 namespace {
19
20 const base::string16 kSplitCharacters = base::ASCIIToUTF16(" .,-_@");
please use gerrit instead 2015/06/28 01:29:39 Wouldn't this statement will require an exit-time
Pritam Nikam 2015/06/29 15:38:21 Done. Do I need to set "-Wexit-time-destructors"
please use gerrit instead 2015/06/29 17:06:45 There's some bot that runs on your code once it's
Pritam Nikam 2015/06/30 15:05:50 Done.
21
22 } // namespace
23
24 bool IsFeatureSubstringMatchEnabled() {
25 return base::CommandLine::ForCurrentProcess()->HasSwitch(
26 switches::kEnableSuggestionsWithSubstringMatch);
27 }
28
29 bool ContainsTokenThatStartsWith(const base::string16& suggestion,
30 const base::string16& field_contents,
31 bool case_sensitive) {
32 if (!IsFeatureSubstringMatchEnabled())
33 return false;
34
35 std::vector<base::string16> suggestion_tokens;
36 Tokenize(suggestion, kSplitCharacters, &suggestion_tokens);
please use gerrit instead 2015/06/28 01:29:39 Tokenize() is deprecated. Use base::SplitString().
Pritam Nikam 2015/06/29 15:38:21 Done.
37
38 // Check whether the |field_contents| prefixes any of the |suggestion|'s
39 // token.
40 for (const base::string16& token : suggestion_tokens) {
41 if (StartsWith(token, field_contents, case_sensitive))
42 return true;
43 }
44
45 return false;
46 }
47
48 size_t GetTextSelectionStart(const base::string16& suggestion,
49 const base::string16& field_contents) {
50 size_t offset = 0;
51 // Loop until we find either the |field_contents| begins with the |suggestion|
52 // or character right before |offset| is one of the splitting characters.
53 for (base::string16::const_iterator it = suggestion.begin();
54 (it = std::search(
55 offset + suggestion.begin(), suggestion.end(),
56 field_contents.begin(), field_contents.end(),
57 base::CaseInsensitiveCompare<base::string16::value_type>())) !=
58 suggestion.end();) {
59 offset = it - suggestion.begin();
60 if (offset == 0 ||
61 ContainsOnlyChars(suggestion.substr(offset - 1, 1), kSplitCharacters)) {
62 // Set caret position to the end of the |field_contents|.
63 offset += field_contents.size();
64 return offset;
65 }
66
67 // Search didn't meet the criteria, advance the suggestion range.
68 ++offset;
69 }
70
71 // Unable to find the |field_contents| in |suggestion| text.
72 NOTREACHED();
73 return base::string16::npos;
74 }
75
76 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698