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..6be327384d610ab16c71b9ef15dcd47acd05ce0d |
--- /dev/null |
+++ b/components/autofill/core/common/autofill_util.cc |
@@ -0,0 +1,85 @@ |
+// 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 { |
+ |
+// Returns offset to the beginning of first occurence of |search| in the |str|. |
+// If no such subsequence is found, |base::string16::npos| is returned. |
+size_t StartsAt(const base::string16& str, const base::string16& search) { |
+ base::string16::const_iterator it = |
+ std::search(str.begin(), str.end(), search.begin(), search.end(), |
+ base::CaseInsensitiveCompare<base::string16::value_type>()); |
+ return (it != str.end()) ? (it - str.begin()) : base::string16::npos; |
+} |
+ |
+} // namespace |
+ |
+bool IsFeatureSubstringMatchEnabled() { |
+ return base::CommandLine::ForCurrentProcess()->HasSwitch( |
+ switches::kEnableSuggestionsWithSubstringMatch); |
+} |
+ |
+bool IsContentsPrefixOfSuggestionToken(const base::string16& field_suggestion, |
+ const base::string16& field_contents, |
+ bool case_sensitive) { |
+ if (!IsFeatureSubstringMatchEnabled()) { |
+ return false; |
+ } |
+ |
+ bool result = false; |
+ std::vector<base::string16> suggestion_tokens; |
+ Tokenize(field_suggestion, base::ASCIIToUTF16(" .,-_@"), &suggestion_tokens); |
+ |
+ // Check whether the |field_contents| prefixes any of the |field_suggestion|'s |
+ // token. |
+ for (auto token : suggestion_tokens) { |
Evan Stade
2015/04/23 20:21:18
nit: don't use auto for simple types (like const b
Pritam Nikam
2015/04/27 07:53:41
Done.
|
+ result = StartsWith(token, field_contents, case_sensitive); |
+ if (result) |
+ break; |
+ } |
+ |
+ return result; |
+} |
+ |
+size_t ComputeRange(const base::string16& field_suggestion, |
+ const base::string16& field_contents, |
+ size_t* start, |
+ size_t* end) { |
+ size_t offset = 0; |
+ size_t next_offset = 0; |
+ do { |
Evan Stade
2015/04/23 20:21:18
please write this loop in a more orthodox way.
1.
Pritam Nikam
2015/04/27 07:53:41
Done.
Change this API to
size_t GetTextSelectio
|
+ // Loop through until we find either the |field_contents| begins with the |
+ // |suggestion| or character right before |offset| is one of the splitting |
+ // characters. |
+ base::string16 suggestion(field_suggestion.substr(next_offset)); |
+ offset = StartsAt(suggestion, field_contents); |
+ if (base::string16::npos != offset) { |
+ next_offset = offset + next_offset + 1; |
+ if ((offset == 0) || ContainsOnlyChars(suggestion.substr(offset - 1, 1), |
+ base::ASCIIToUTF16(" .,-_@"))) { |
+ offset = next_offset - 1; |
+ *start = offset + field_contents.size(); |
+ *end = field_suggestion.size(); |
+ break; |
+ } |
+ } |
+ } while (base::string16::npos != offset); |
+ |
+ return offset; |
+} |
+ |
+} // namespace autofill |