Chromium Code Reviews| 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..7f3e36737e485904dba6237dfc6880c1b70efead |
| --- /dev/null |
| +++ b/components/autofill/core/common/autofill_util.cc |
| @@ -0,0 +1,77 @@ |
| +// 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; |
| + |
| + 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); |
|
Evan Stade
2015/05/11 19:07:12
for(...) {
if (StartsWith...)
return true;
}
Pritam Nikam
2015/05/21 10:58:22
Done.
|
| + 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( |
|
Evan Stade
2015/05/11 19:07:12
this should be a for loop. Why is |it| declared ou
Pritam Nikam
2015/05/21 10:58:22
Ok. I've changed this to:
for (base::string16::
|
| + 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) || |
|
Evan Stade
2015/05/11 19:07:12
no extra parens
Pritam Nikam
2015/05/21 10:58:22
Done.
|
| + 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. |
| + return base::string16::npos; |
| +} |
| + |
| +} // namespace autofill |