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..8c0d7be01e525ce2b7fc60ecd681346a6d4b8105 |
| --- /dev/null |
| +++ b/components/autofill/core/common/autofill_util.cc |
| @@ -0,0 +1,72 @@ |
| +// 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, |
| + 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) { |
| + result = StartsWith(token, field_contents, 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::size_type offset = 0; |
| + base::string16::size_type next_offset = 0; |
| + do { |
| + // 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, false); |
|
danakj
2015/04/02 18:31:43
Why not just std::search() here? Is this a common
Pritam Nikam
2015/04/06 06:13:10
Earlier, plan was to use StartsAt at all places wh
|
| + 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 |