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

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: Addresses Vaclav's & Evan's Inputs. Created 5 years, 9 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 bool IsFeatureSubstringMatchEnabled() {
19 return base::CommandLine::ForCurrentProcess()->HasSwitch(
20 switches::kEnableSuggestionsWithSubstringMatch);
21 }
22
23 bool HasTokenStartsWith(const base::string16& field_suggestion,
24 const base::string16& field_contents,
25 bool case_sensitive) {
26 if (field_suggestion.empty() || field_contents.empty()) {
27 return false;
28 }
29
30 bool result = false;
31 base::string16 search_string =
32 case_sensitive ? field_contents : base::i18n::ToLower(field_contents);
33 base::string16 suggestion_string =
34 case_sensitive ? field_suggestion : base::i18n::ToLower(field_suggestion);
35 base::string16::size_type offset = suggestion_string.find(search_string);
36 if (base::string16::npos != offset) {
37 std::vector<base::string16> suggestion_tokens, search_tokens;
38 Tokenize(suggestion_string, base::ASCIIToUTF16(" .,-_@"),
39 &suggestion_tokens);
40 Tokenize(search_string, base::ASCIIToUTF16(" .,-_@"), &search_tokens);
41
42 // Check whether the |search_string|'s first token matches any of the
43 // |suggestion_string|'s token.
44 if (suggestion_tokens.size() != 0 && search_tokens.size() != 0) {
45 for (auto token : suggestion_tokens) {
46 result = StartsWith(token, search_tokens[0], case_sensitive);
47 if (result)
48 break;
49 }
50 }
51 }
52
53 return result;
54 }
55
56 base::string16::size_type ComputeRange(const base::string16& field_suggestion,
57 const base::string16& field_contents,
58 size_t* start,
59 size_t* end) {
60 base::string16 user_input =
61 base::i18n::ToLower(base::string16(field_contents));
62 base::string16 suggestion =
63 base::i18n::ToLower(base::string16(field_suggestion));
64 base::string16::size_type offset = suggestion.find(user_input);
65 if (base::string16::npos != offset) {
66 *start = offset + user_input.size();
67 *end = suggestion.size();
68 }
69
70 return offset;
71 }
72
73 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698