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..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(" .,-_@"), |
|
Evan Stade
2015/03/26 18:54:38
can we just check if the character right before of
Pritam Nikam
2015/03/27 14:57:42
Acknowledged.
This seems much cleaner, I'll take
|
| + &suggestion_tokens); |
| + Tokenize(search_string, base::ASCIIToUTF16(" .,-_@"), &search_tokens); |
|
Evan Stade
2015/03/26 18:54:38
why tokenize the search term?
Pritam Nikam
2015/03/27 14:57:42
To handle cases where user input contains token se
|
| + |
| + // 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); |
|
Evan Stade
2015/03/26 18:54:37
I wouldn't expect this to happen, but it seems lik
|
| + if (base::string16::npos != offset) { |
| + *start = offset + user_input.size(); |
| + *end = suggestion.size(); |
| + } |
| + |
| + return offset; |
| +} |
| + |
| +} // namespace autofill |