Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: components/autofill/core/browser/autofill_data_util.cc

Issue 1868003003: Preserving first/middle/last names when an Autofill profile is submitted (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleaning up refactor cruft Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/autofill/core/browser/autofill_data_util.h" 5 #include "components/autofill/core/browser/autofill_data_util.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/strings/string_split.h" 9 #include "base/strings/string_split.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "components/autofill/core/browser/field_types.h"
12 13
13 namespace autofill { 14 namespace autofill {
14 namespace data_util { 15 namespace data_util {
15 16
16 namespace { 17 namespace {
17 const char* const name_prefixes[] = { 18 const char* const name_prefixes[] = {
18 "1lt", "1st", "2lt", "2nd", "3rd", "admiral", "capt", 19 "1lt", "1st", "2lt", "2nd", "3rd", "admiral", "capt",
19 "captain", "col", "cpt", "dr", "gen", "general", "lcdr", 20 "captain", "col", "cpt", "dr", "gen", "general", "lcdr",
20 "lt", "ltc", "ltg", "ltjg", "maj", "major", "mg", 21 "lt", "ltc", "ltg", "ltjg", "maj", "major", "mg",
21 "mr", "mrs", "ms", "pastor", "prof", "rep", "reverend", 22 "mr", "mrs", "ms", "pastor", "prof", "rep", "reverend",
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 void StripSuffixes(std::vector<base::string16>* name_tokens) { 67 void StripSuffixes(std::vector<base::string16>* name_tokens) {
67 while (!name_tokens->empty()) { 68 while (!name_tokens->empty()) {
68 if (!ContainsString(name_suffixes, arraysize(name_suffixes), 69 if (!ContainsString(name_suffixes, arraysize(name_suffixes),
69 name_tokens->back())) { 70 name_tokens->back())) {
70 break; 71 break;
71 } 72 }
72 name_tokens->pop_back(); 73 name_tokens->pop_back();
73 } 74 }
74 } 75 }
75 76
77 // Concatenates two strings, adding a space and, optionally, a period.
78 base::string16 StringConcatenationHelper(base::string16 first,
79 base::string16 second,
80 bool period) {
81 base::string16 delimiter =
82 period ? base::UTF8ToUTF16(". ") : base::UTF8ToUTF16(" ");
83 return first + delimiter + second;
84 }
85
76 } // namespace 86 } // namespace
77 87
78 NameParts SplitName(const base::string16& name) { 88 NameParts SplitName(const base::string16& name) {
79 std::vector<base::string16> name_tokens = 89 std::vector<base::string16> name_tokens =
80 base::SplitString(name, base::ASCIIToUTF16(" ,"), base::KEEP_WHITESPACE, 90 base::SplitString(name, base::ASCIIToUTF16(" ,"), base::KEEP_WHITESPACE,
81 base::SPLIT_WANT_NONEMPTY); 91 base::SPLIT_WANT_NONEMPTY);
82 StripPrefixes(&name_tokens); 92 StripPrefixes(&name_tokens);
83 93
84 // Don't assume "Ma" is a suffix in John Ma. 94 // Don't assume "Ma" is a suffix in John Ma.
85 if (name_tokens.size() > 2) 95 if (name_tokens.size() > 2)
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 parts.middle = name_tokens.back(); 131 parts.middle = name_tokens.back();
122 name_tokens.pop_back(); 132 name_tokens.pop_back();
123 } 133 }
124 134
125 // Remainder is given name. 135 // Remainder is given name.
126 parts.given = base::JoinString(name_tokens, base::ASCIIToUTF16(" ")); 136 parts.given = base::JoinString(name_tokens, base::ASCIIToUTF16(" "));
127 137
128 return parts; 138 return parts;
129 } 139 }
130 140
141 bool ProfileMatchesFullName(const base::string16 full_name,
142 const autofill::AutofillProfile& profile) {
143 // First Last
144 base::string16 candidate =
145 StringConcatenationHelper(profile.GetRawInfo(autofill::NAME_FIRST),
Mathieu 2016/04/13 18:12:24 Not sure this StringConcatenationHelper is helping
tmartino 2016/04/13 19:43:25 Agreed.
146 profile.GetRawInfo(autofill::NAME_LAST), false);
147 if (!full_name.compare(candidate)) {
148 return true;
149 }
150
151 // First Middle Last
152 candidate = StringConcatenationHelper(
153 StringConcatenationHelper(profile.GetRawInfo(autofill::NAME_FIRST),
154 profile.GetRawInfo(autofill::NAME_MIDDLE),
155 false),
156 profile.GetRawInfo(autofill::NAME_LAST), false);
157 if (!full_name.compare(candidate)) {
158 return true;
159 }
160
161 // First M Last
162 candidate = StringConcatenationHelper(
163 StringConcatenationHelper(
164 profile.GetRawInfo(autofill::NAME_FIRST),
165 profile.GetRawInfo(autofill::NAME_MIDDLE_INITIAL), false),
166 profile.GetRawInfo(autofill::NAME_LAST), false);
167 if (!full_name.compare(candidate)) {
168 return true;
169 }
170
171 // First M. Last
172 candidate = StringConcatenationHelper(
173 StringConcatenationHelper(
174 profile.GetRawInfo(autofill::NAME_FIRST),
175 profile.GetRawInfo(autofill::NAME_MIDDLE_INITIAL), false),
176 profile.GetRawInfo(autofill::NAME_LAST), true);
177 if (!full_name.compare(candidate)) {
178 return true;
179 }
180
181 return false;
182 }
183
131 } // namespace data_util 184 } // namespace data_util
132 } // namespace autofill 185 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698