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..0bcd448a8d7ff545b2f022395ef2af3a59326a32 |
--- /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> |
+ |
+#include "base/command_line.h" |
+#include "base/i18n/case_conversion.h" |
+#include "base/strings/string_piece.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 { |
+ |
+const base::string16 kSplitCharacters = base::ASCIIToUTF16(" .,-_@"); |
+ |
+} // namespace |
+ |
+bool IsFeatureSubstringMatchEnabled() { |
+ return base::CommandLine::ForCurrentProcess()->HasSwitch( |
+ switches::kEnableSuggestionsWithSubstringMatch); |
+} |
+ |
+bool IsContentsPrefixOfSuggestionToken(const base::string16& suggestion, |
+ const base::string16& field_contents, |
+ bool case_sensitive) { |
+ if (!IsFeatureSubstringMatchEnabled()) { |
+ return false; |
+ } |
Evan Stade
2015/04/27 19:25:21
no curlies
Pritam Nikam
2015/04/28 14:45:55
Done.
|
+ |
+ bool result = false; |
+ std::vector<base::string16> suggestion_tokens; |
+ Tokenize(suggestion, kSplitCharacters, &suggestion_tokens); |
+ |
+ // Check whether the |field_contents| prefixes any of the |suggestion|'s |
+ // token. |
+ for (const base::string16& token : suggestion_tokens) { |
+ result = StartsWith(token, field_contents, case_sensitive); |
+ if (result) |
+ break; |
+ } |
+ |
+ return result; |
+} |
+ |
+size_t GetTextSelectionStart(const base::string16& suggestion, |
+ const base::string16& field_contents) { |
+ size_t offset = 0; |
+ base::string16::const_iterator it = suggestion.end(); |
+ // Loop until we find either the |field_contents| begins with the |suggestion| |
+ // or character right before |offset| is one of the splitting characters. |
+ while ((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 ((it == suggestion.begin()) || |
Evan Stade
2015/04/27 19:25:21
offset == 0
Pritam Nikam
2015/04/28 14:45:55
Done.
|
+ ContainsOnlyChars(suggestion.substr(offset - 1, 1), kSplitCharacters)) { |
+ offset += field_contents.size(); |
+ break; |
+ } |
+ |
+ ++offset; |
Evan Stade
2015/04/27 19:25:21
what is the purpose of this?
Pritam Nikam
2015/04/28 14:45:55
It will advance the suggestion iterator (or first1
|
+ } |
+ |
+ return (it != suggestion.end()) ? offset : base::string16::npos; |
Evan Stade
2015/04/27 19:25:21
is it valid if it == suggestion.end()? It seems li
Pritam Nikam
2015/04/28 14:45:55
it == suggestion.end() is equivalent to std::searc
Evan Stade
2015/05/11 19:07:12
in which case you probably shouldn't have called G
Pritam Nikam
2015/05/21 10:58:22
Yes, thats right.
But there is no restriction on d
Evan Stade
2015/05/22 18:26:48
then you should DCHECK and not attempt to handle t
|
+} |
+ |
+} // namespace autofill |