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

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: Removed changes from "base/strings". Created 5 years, 8 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 // Returns offset to the beginning of first occurence of |search| in the |str|.
21 // If no such subsequence is found, |base::string16::npos| is returned.
22 size_t StartsAt(const base::string16& str, const base::string16& search) {
23 base::string16::const_iterator it =
24 std::search(str.begin(), str.end(), search.begin(), search.end(),
25 base::CaseInsensitiveCompare<base::string16::value_type>());
26 return (it != str.end()) ? (it - str.begin()) : base::string16::npos;
27 }
28
29 } // namespace
30
31 bool IsFeatureSubstringMatchEnabled() {
32 return base::CommandLine::ForCurrentProcess()->HasSwitch(
33 switches::kEnableSuggestionsWithSubstringMatch);
34 }
35
36 bool IsContentsPrefixOfSuggestionToken(const base::string16& field_suggestion,
37 const base::string16& field_contents,
38 bool case_sensitive) {
39 if (!IsFeatureSubstringMatchEnabled()) {
40 return false;
41 }
42
43 bool result = false;
44 std::vector<base::string16> suggestion_tokens;
45 Tokenize(field_suggestion, base::ASCIIToUTF16(" .,-_@"), &suggestion_tokens);
46
47 // Check whether the |field_contents| prefixes any of the |field_suggestion|'s
48 // token.
49 for (auto token : suggestion_tokens) {
Evan Stade 2015/04/23 20:21:18 nit: don't use auto for simple types (like const b
Pritam Nikam 2015/04/27 07:53:41 Done.
50 result = StartsWith(token, field_contents, case_sensitive);
51 if (result)
52 break;
53 }
54
55 return result;
56 }
57
58 size_t ComputeRange(const base::string16& field_suggestion,
59 const base::string16& field_contents,
60 size_t* start,
61 size_t* end) {
62 size_t offset = 0;
63 size_t next_offset = 0;
64 do {
Evan Stade 2015/04/23 20:21:18 please write this loop in a more orthodox way. 1.
Pritam Nikam 2015/04/27 07:53:41 Done. Change this API to size_t GetTextSelectio
65 // Loop through until we find either the |field_contents| begins with the
66 // |suggestion| or character right before |offset| is one of the splitting
67 // characters.
68 base::string16 suggestion(field_suggestion.substr(next_offset));
69 offset = StartsAt(suggestion, field_contents);
70 if (base::string16::npos != offset) {
71 next_offset = offset + next_offset + 1;
72 if ((offset == 0) || ContainsOnlyChars(suggestion.substr(offset - 1, 1),
73 base::ASCIIToUTF16(" .,-_@"))) {
74 offset = next_offset - 1;
75 *start = offset + field_contents.size();
76 *end = field_suggestion.size();
77 break;
78 }
79 }
80 } while (base::string16::npos != offset);
81
82 return offset;
83 }
84
85 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698