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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: components/autofill/core/common/autofill_util.cc
diff --git a/components/autofill/core/common/autofill_util.cc b/components/autofill/core/common/autofill_util.cc
new file mode 100644
index 0000000000000000000000000000000000000000..0b402082f737b8181d7a13cb841873771ff26214
--- /dev/null
+++ b/components/autofill/core/common/autofill_util.cc
@@ -0,0 +1,77 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/autofill/core/common/autofill_util.h"
+
+#include <algorithm>
+#include <vector>
+
+#include "base/command_line.h"
+#include "base/i18n/case_conversion.h"
+#include "base/strings/string_piece.h"
+#include "base/strings/string_split.h"
+#include "base/strings/string_util.h"
+#include "base/strings/utf_string_conversions.h"
+#include "components/autofill/core/common/autofill_switches.h"
+
+namespace autofill {
+
+namespace {
+
+static const char kSplitCharacters[] = " .,-_@";
+
+template <typename Char>
+struct Compare : base::CaseInsensitiveCompare<Char> {
+ explicit Compare(bool case_sensitive) : case_sensitive_(case_sensitive) {}
+ bool operator()(Char x, Char y) const {
+ return case_sensitive_ ? (x == y) : base::CaseInsensitiveCompare<Char>::
+ operator()(x, y);
+ }
+
+ private:
+ bool case_sensitive_;
+};
+
+} // namespace
+
+bool IsFeatureSubstringMatchEnabled() {
+ return base::CommandLine::ForCurrentProcess()->HasSwitch(
+ switches::kEnableSuggestionsWithSubstringMatch);
+}
+
+bool ContainsTokenThatStartsWith(const base::string16& suggestion,
+ const base::string16& field_contents,
+ bool case_sensitive) {
+ return IsFeatureSubstringMatchEnabled() &&
+ suggestion.length() >= field_contents.length() &&
+ GetTextSelectionStart(suggestion, field_contents, case_sensitive) !=
+ base::string16::npos;
+}
+
+size_t GetTextSelectionStart(const base::string16& suggestion,
+ const base::string16& field_contents,
+ bool case_sensitive) {
+ const base::string16 kSplitChars = base::ASCIIToUTF16(kSplitCharacters);
+
+ // Loop until we find either the |field_contents| is a prefix of |suggestion|
+ // or character right before the match is one of the splitting characters.
+ for (base::string16::const_iterator it = suggestion.begin();
+ (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.
+ it, suggestion.end(), field_contents.begin(), field_contents.end(),
+ Compare<base::string16::value_type>(case_sensitive))) !=
+ suggestion.end();
+ ++it) {
+ if (it == suggestion.begin() ||
+ kSplitChars.find(*(it - 1)) != std::string::npos) {
+ // Returns the character position right after the |field_contents| within
+ // |suggestion| text as a caret position for text selection.
+ return it - suggestion.begin() + field_contents.size();
+ }
+ }
+
+ // Unable to find the |field_contents| in |suggestion| text.
+ return base::string16::npos;
+}
+
+} // namespace autofill

Powered by Google App Engine
This is Rietveld 408576698