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

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: Added comments in autocomplete_history_manager.cc. 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 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..827a05071b14e8ea486a8cea2b5bb8fb65995219
--- /dev/null
+++ b/components/autofill/core/common/autofill_util.cc
@@ -0,0 +1,75 @@
+// 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 <vector>
please use gerrit instead 2015/06/29 22:06:29 Also <algorithm> for std::search.
Pritam Nikam 2015/06/30 15:05:50 Done.
+
+#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 {
+
+static const char kSplitCharacters[] = " .,-_@";
+
+bool IsFeatureSubstringMatchEnabled() {
+ return base::CommandLine::ForCurrentProcess()->HasSwitch(
+ switches::kEnableSuggestionsWithSubstringMatch);
+}
+
+bool ContainsTokenThatStartsWith(const base::string16& suggestion,
+ const base::string16& field_contents,
+ bool case_sensitive) {
+ if (!IsFeatureSubstringMatchEnabled())
+ return false;
+
+ std::vector<base::string16> suggestion_tokens =
please use gerrit instead 2015/06/29 22:06:29 You can probably save one copy operation by using
Pritam Nikam 2015/06/30 15:05:51 Done.
+ base::SplitString(suggestion, base::ASCIIToUTF16(kSplitCharacters),
+ base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
+
+ // Check whether the |field_contents| prefixes any of the |suggestion|'s
+ // token.
please use gerrit instead 2015/06/29 22:06:29 s/token/tokens
Pritam Nikam 2015/06/30 15:05:51 Done.
+ for (const base::string16& token : suggestion_tokens) {
+ if (StartsWith(token, field_contents, case_sensitive))
+ return true;
+ }
+
+ return false;
+}
+
+size_t GetTextSelectionStart(const base::string16& suggestion,
+ const base::string16& field_contents) {
+ size_t offset = 0;
please use gerrit instead 2015/06/29 22:06:29 You don't need |offset| variable. You can use "std
Pritam Nikam 2015/06/30 15:05:50 Done.
+ // Loop until we find either the |field_contents| begins with the |suggestion|
please use gerrit instead 2015/06/29 22:06:29 s/begins with/is a prefix of Right?
Pritam Nikam 2015/06/30 15:05:50 Done.
+ // or character right before |offset| is one of the splitting characters.
+ for (base::string16::const_iterator it = suggestion.begin();
+ (it = std::search(
+ offset + suggestion.begin(), suggestion.end(),
+ field_contents.begin(), field_contents.end(),
+ base::CaseInsensitiveCompare<base::string16::value_type>())) !=
+ suggestion.end();) {
+ offset = it - suggestion.begin();
+ if (offset == 0 ||
+ ContainsOnlyChars(suggestion.substr(offset - 1, 1),
+ base::ASCIIToUTF16(kSplitCharacters))) {
+ // Set caret position to the end of the |field_contents|.
+ offset += field_contents.size();
+ return offset;
+ }
+
+ // Search didn't meet the criteria, advance the suggestion range.
+ ++offset;
+ }
+
+ // Unable to find the |field_contents| in |suggestion| text.
+ NOTREACHED();
+ return base::string16::npos;
+}
+
+} // namespace autofill

Powered by Google App Engine
This is Rietveld 408576698