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..2b8e8e06e2f1cebe43d99a19e4f4ee2deaec0b3d |
--- /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; |
+ |
+ 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) { |
+ 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; |
+ // Loop until we find either the |field_contents| begins with the |suggestion| |
+ // 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), 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. |
Evan Stade
2015/05/22 18:26:48
notreached()
Pritam Nikam
2015/05/25 18:14:52
Done.
|
+ return base::string16::npos; |
+} |
+ |
+} // namespace autofill |