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..6a4a56a3bd37d37aef552b830da5f3ccd92289bf |
--- /dev/null |
+++ b/components/autofill/core/common/autofill_util.cc |
@@ -0,0 +1,71 @@ |
+// 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 <algorithm> |
+#include <vector> |
+ |
+#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; |
+ |
+ const std::vector<base::string16>& suggestion_tokens = |
+ base::SplitString(suggestion, base::ASCIIToUTF16(kSplitCharacters), |
+ base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
+ |
+ // Check whether the |field_contents| prefixes any of the |suggestion|'s |
+ // tokens. |
+ 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) { |
+ // Loop until we find either the |field_contents| is a prefix of |suggestion| |
+ // or character right before the match is one of the splitting characters. |
+ for (base::string16::const_iterator it = suggestion.begin(); |
+ (it = std::search( |
+ it, suggestion.end(), field_contents.begin(), field_contents.end(), |
+ base::CaseInsensitiveCompare<base::string16::value_type>())) != |
+ suggestion.end(); |
+ ++it) { |
+ if (it == suggestion.begin() || |
+ ContainsOnlyChars(suggestion.substr((it - suggestion.begin() - 1), 1), |
+ base::ASCIIToUTF16(kSplitCharacters))) { |
please use gerrit instead
2015/06/30 19:06:23
It should be easier to dereference the pointer (*i
Pritam Nikam
2015/07/01 17:26:00
Done.
Did you mean?
base::string16 split_cha
please use gerrit instead
2015/07/03 02:05:43
You're right.
|
+ // Returns the character position right after the |field_contents| within |
+ // |suggestion| text as a caret position for text selection. |
+ return it - suggestion.begin() + field_contents.size(); |
+ } |
+ } |
+ |
+ // Unable to find the |field_contents| in |suggestion| text. |
+ NOTREACHED(); |
+ return base::string16::npos; |
+} |
+ |
+} // namespace autofill |