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..c50805a6f7b0b9833f7718476bd03c3cd65cf43a |
--- /dev/null |
+++ b/components/autofill/core/common/autofill_util.cc |
@@ -0,0 +1,74 @@ |
+// 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 { |
+ |
+bool IsFeatureSubstringMatchEnabled() { |
+ return base::CommandLine::ForCurrentProcess()->HasSwitch( |
+ switches::kEnableSuggestionsWithSubstringMatch); |
+} |
+ |
+bool IsContentsPrefixOfSuggestionToken(const base::string16& field_suggestion, |
+ const base::string16& field_contents, |
+ CaseSensitivity case_sensitivity) { |
+ if (field_suggestion.empty() || field_contents.empty()) { |
+ return false; |
+ } |
+ |
+ bool result = false; |
+ bool case_sensitive = (CASE_SENSITIVE == case_sensitivity); |
+ base::string16 search_string = |
+ case_sensitive ? field_contents : base::i18n::ToLower(field_contents); |
+ base::string16 suggestion_string = |
+ case_sensitive ? field_suggestion : base::i18n::ToLower(field_suggestion); |
+ base::string16::size_type offset = suggestion_string.find(search_string); |
+ if (base::string16::npos != offset) { |
+ std::vector<base::string16> suggestion_tokens, search_tokens; |
+ Tokenize(suggestion_string, base::ASCIIToUTF16(" .,-_@"), |
+ &suggestion_tokens); |
+ Tokenize(search_string, base::ASCIIToUTF16(" .,-_@"), &search_tokens); |
+ |
+ // Check whether the |search_string|'s first token matches any of the |
+ // |suggestion_string|'s token. |
+ if (suggestion_tokens.size() != 0 && search_tokens.size() != 0) { |
+ for (auto token : suggestion_tokens) { |
+ result = StartsWith(token, search_tokens[0], case_sensitive); |
+ if (result) |
+ break; |
+ } |
+ } |
+ } |
+ |
+ return result; |
+} |
+ |
+base::string16::size_type ComputeRange(const base::string16& field_suggestion, |
+ const base::string16& field_contents, |
+ size_t* start, |
+ size_t* end) { |
+ base::string16 user_input = |
+ base::i18n::ToLower(base::string16(field_contents)); |
+ base::string16 suggestion = |
+ base::i18n::ToLower(base::string16(field_suggestion)); |
+ base::string16::size_type offset = suggestion.find(user_input); |
+ if (base::string16::npos != offset) { |
+ *start = offset + user_input.size(); |
+ *end = suggestion.size(); |
+ } |
+ |
+ return offset; |
+} |
+ |
+} // namespace autofill |