OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/contact_info.h" | 5 #include "components/autofill/core/browser/contact_info.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <ostream> | 8 #include <ostream> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/strings/string_split.h" | |
14 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
15 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "components/autofill/core/browser/autofill_data_util.h" |
16 #include "components/autofill/core/browser/autofill_type.h" | 16 #include "components/autofill/core/browser/autofill_type.h" |
17 #include "components/autofill/core/common/autofill_l10n_util.h" | 17 #include "components/autofill/core/common/autofill_l10n_util.h" |
18 | 18 |
19 namespace autofill { | 19 namespace autofill { |
20 | 20 |
21 namespace { | |
22 | |
23 const char* const name_prefixes[] = { | |
24 "1lt", "1st", "2lt", "2nd", "3rd", "admiral", "capt", "captain", "col", | |
25 "cpt", "dr", "gen", "general", "lcdr", "lt", "ltc", "ltg", "ltjg", "maj", | |
26 "major", "mg", "mr", "mrs", "ms", "pastor", "prof", "rep", "reverend", | |
27 "rev", "sen", "st" }; | |
28 | |
29 const char* const name_suffixes[] = { | |
30 "b.a", "ba", "d.d.s", "dds", "i", "ii", "iii", "iv", "ix", "jr", "m.a", | |
31 "m.d", "ma", "md", "ms", "ph.d", "phd", "sr", "v", "vi", "vii", "viii", | |
32 "x" }; | |
33 | |
34 const char* const family_name_prefixes[] = { | |
35 "d'", "de", "del", "der", "di", "la", "le", "mc", "san", "st", "ter", | |
36 "van", "von" }; | |
37 | |
38 // Returns true if |set| contains |element|, modulo a final period. | |
39 bool ContainsString(const char* const set[], | |
40 size_t set_size, | |
41 const base::string16& element) { | |
42 if (!base::IsStringASCII(element)) | |
43 return false; | |
44 | |
45 base::string16 trimmed_element; | |
46 base::TrimString(element, base::ASCIIToUTF16("."), &trimmed_element); | |
47 | |
48 for (size_t i = 0; i < set_size; ++i) { | |
49 if (base::LowerCaseEqualsASCII(trimmed_element, set[i])) | |
50 return true; | |
51 } | |
52 | |
53 return false; | |
54 } | |
55 | |
56 // Removes common name prefixes from |name_tokens|. | |
57 void StripPrefixes(std::vector<base::string16>* name_tokens) { | |
58 std::vector<base::string16>::iterator iter = name_tokens->begin(); | |
59 while(iter != name_tokens->end()) { | |
60 if (!ContainsString(name_prefixes, arraysize(name_prefixes), *iter)) | |
61 break; | |
62 ++iter; | |
63 } | |
64 | |
65 std::vector<base::string16> copy_vector; | |
66 copy_vector.assign(iter, name_tokens->end()); | |
67 *name_tokens = copy_vector; | |
68 } | |
69 | |
70 // Removes common name suffixes from |name_tokens|. | |
71 void StripSuffixes(std::vector<base::string16>* name_tokens) { | |
72 while(!name_tokens->empty()) { | |
73 if (!ContainsString(name_suffixes, arraysize(name_suffixes), | |
74 name_tokens->back())) { | |
75 break; | |
76 } | |
77 name_tokens->pop_back(); | |
78 } | |
79 } | |
80 | |
81 struct NameParts { | |
82 base::string16 given; | |
83 base::string16 middle; | |
84 base::string16 family; | |
85 }; | |
86 | |
87 // TODO(estade): This does Western name splitting. It should do different | |
88 // splitting based on the app locale. | |
89 NameParts SplitName(const base::string16& name) { | |
90 std::vector<base::string16> name_tokens = base::SplitString( | |
91 name, base::ASCIIToUTF16(" ,"), base::KEEP_WHITESPACE, | |
92 base::SPLIT_WANT_NONEMPTY); | |
93 StripPrefixes(&name_tokens); | |
94 | |
95 // Don't assume "Ma" is a suffix in John Ma. | |
96 if (name_tokens.size() > 2) | |
97 StripSuffixes(&name_tokens); | |
98 | |
99 NameParts parts; | |
100 | |
101 if (name_tokens.empty()) { | |
102 // Bad things have happened; just assume the whole thing is a given name. | |
103 parts.given = name; | |
104 return parts; | |
105 } | |
106 | |
107 // Only one token, assume given name. | |
108 if (name_tokens.size() == 1) { | |
109 parts.given = name_tokens[0]; | |
110 return parts; | |
111 } | |
112 | |
113 // 2 or more tokens. Grab the family, which is the last word plus any | |
114 // recognizable family prefixes. | |
115 std::vector<base::string16> reverse_family_tokens; | |
116 reverse_family_tokens.push_back(name_tokens.back()); | |
117 name_tokens.pop_back(); | |
118 while (name_tokens.size() >= 1 && | |
119 ContainsString(family_name_prefixes, | |
120 arraysize(family_name_prefixes), | |
121 name_tokens.back())) { | |
122 reverse_family_tokens.push_back(name_tokens.back()); | |
123 name_tokens.pop_back(); | |
124 } | |
125 | |
126 std::vector<base::string16> family_tokens(reverse_family_tokens.rbegin(), | |
127 reverse_family_tokens.rend()); | |
128 parts.family = base::JoinString(family_tokens, base::ASCIIToUTF16(" ")); | |
129 | |
130 // Take the last remaining token as the middle name (if there are at least 2 | |
131 // tokens). | |
132 if (name_tokens.size() >= 2) { | |
133 parts.middle = name_tokens.back(); | |
134 name_tokens.pop_back(); | |
135 } | |
136 | |
137 // Remainder is given name. | |
138 parts.given = base::JoinString(name_tokens, base::ASCIIToUTF16(" ")); | |
139 | |
140 return parts; | |
141 } | |
142 | |
143 } // namespace | |
144 | |
145 NameInfo::NameInfo() {} | 21 NameInfo::NameInfo() {} |
146 | 22 |
147 NameInfo::NameInfo(const NameInfo& info) : FormGroup() { | 23 NameInfo::NameInfo(const NameInfo& info) : FormGroup() { |
148 *this = info; | 24 *this = info; |
149 } | 25 } |
150 | 26 |
151 NameInfo::~NameInfo() {} | 27 NameInfo::~NameInfo() {} |
152 | 28 |
153 NameInfo& NameInfo::operator=(const NameInfo& info) { | 29 NameInfo& NameInfo::operator=(const NameInfo& info) { |
154 if (this == &info) | 30 if (this == &info) |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 } | 152 } |
277 | 153 |
278 void NameInfo::SetFullName(const base::string16& full) { | 154 void NameInfo::SetFullName(const base::string16& full) { |
279 full_ = full; | 155 full_ = full; |
280 | 156 |
281 // If |full| is empty, leave the other name parts alone. This might occur | 157 // If |full| is empty, leave the other name parts alone. This might occur |
282 // due to a migrated database with an empty |full_name| value. | 158 // due to a migrated database with an empty |full_name| value. |
283 if (full.empty()) | 159 if (full.empty()) |
284 return; | 160 return; |
285 | 161 |
286 NameParts parts = SplitName(full); | 162 data_util::NameParts parts = data_util::SplitName(full); |
287 given_ = parts.given; | 163 given_ = parts.given; |
288 middle_ = parts.middle; | 164 middle_ = parts.middle; |
289 family_ = parts.family; | 165 family_ = parts.family; |
290 } | 166 } |
291 | 167 |
292 EmailInfo::EmailInfo() {} | 168 EmailInfo::EmailInfo() {} |
293 | 169 |
294 EmailInfo::EmailInfo(const EmailInfo& info) : FormGroup() { | 170 EmailInfo::EmailInfo(const EmailInfo& info) : FormGroup() { |
295 *this = info; | 171 *this = info; |
296 } | 172 } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 return base::string16(); | 224 return base::string16(); |
349 } | 225 } |
350 | 226 |
351 void CompanyInfo::SetRawInfo(ServerFieldType type, | 227 void CompanyInfo::SetRawInfo(ServerFieldType type, |
352 const base::string16& value) { | 228 const base::string16& value) { |
353 DCHECK_EQ(COMPANY_NAME, type); | 229 DCHECK_EQ(COMPANY_NAME, type); |
354 company_name_ = value; | 230 company_name_ = value; |
355 } | 231 } |
356 | 232 |
357 } // namespace autofill | 233 } // namespace autofill |
OLD | NEW |