1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
// Copyright (c) 2010 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 "chrome/browser/autofill/address.h"
#include "base/basictypes.h"
#include "base/string_util.h"
#include "chrome/browser/autofill/autofill_type.h"
#include "chrome/browser/autofill/field_types.h"
#define U(x) (UTF16ToUTF8(x).c_str())
static const string16 kAddressSplitChars = ASCIIToUTF16("-,#. ");
static const AutoFillType::FieldTypeSubGroup kAutoFillAddressTypes[] = {
AutoFillType::ADDRESS_LINE1,
AutoFillType::ADDRESS_LINE2,
AutoFillType::ADDRESS_CITY,
AutoFillType::ADDRESS_STATE,
AutoFillType::ADDRESS_ZIP,
AutoFillType::ADDRESS_COUNTRY,
};
static const int kAutoFillAddressLength = arraysize(kAutoFillAddressTypes);
void Address::GetPossibleFieldTypes(const string16& text,
FieldTypeSet* possible_types) const {
DCHECK(possible_types);
if (!possible_types)
return;
// If the text to match against the field types is empty, then no results will
// match.
if (text.empty())
return;
if (IsLine1(text))
possible_types->insert(GetLine1Type());
if (IsLine2(text))
possible_types->insert(GetLine2Type());
if (IsAptNum(text))
possible_types->insert(GetAptNumType());
if (IsCity(text))
possible_types->insert(GetCityType());
if (IsState(text))
possible_types->insert(GetStateType());
if (IsZipCode(text))
possible_types->insert(GetZipCodeType());
if (IsCountry(text))
possible_types->insert(GetCountryType());
}
void Address::FindInfoMatches(const AutoFillType& type,
const string16& info,
std::vector<string16>* matched_text) const {
if (matched_text == NULL) {
DLOG(ERROR) << "NULL matched vector passed in";
return;
}
string16 match;
if (type.field_type() == UNKNOWN_TYPE) {
for (int i = 0; i < kAutoFillAddressLength; ++i) {
if (FindInfoMatchesHelper(kAutoFillAddressTypes[i], info, &match))
matched_text->push_back(match);
}
} else {
if (FindInfoMatchesHelper(type.subgroup(), info, &match))
matched_text->push_back(match);
}
}
string16 Address::GetFieldText(const AutoFillType& type) const {
AutoFillFieldType field_type = type.field_type();
if (field_type == GetLine1Type())
return line1();
if (field_type == GetLine2Type())
return line2();
if (field_type == GetAptNumType())
return apt_num();
if (field_type == GetCityType())
return city();
if (field_type == GetStateType())
return state();
if (field_type == GetZipCodeType())
return zip_code();
if (field_type == GetCountryType())
return country();
return string16();
}
void Address::SetInfo(const AutoFillType& type, const string16& value) {
FieldTypeSubGroup subgroup = type.subgroup();
if (subgroup == AutoFillType::ADDRESS_LINE1)
set_line1(value);
else if (subgroup == AutoFillType::ADDRESS_LINE2)
set_line2(value);
else if (subgroup == AutoFillType::ADDRESS_APT_NUM)
set_apt_num(value);
else if (subgroup == AutoFillType::ADDRESS_CITY)
set_city(value);
else if (subgroup == AutoFillType::ADDRESS_STATE)
set_state(value);
else if (subgroup == AutoFillType::ADDRESS_COUNTRY)
set_country(value);
else if (subgroup == AutoFillType::ADDRESS_ZIP)
set_zip_code(value);
else
NOTREACHED();
}
void Address::Clear() {
line1_tokens_.clear();
line1_.clear();
line2_tokens_.clear();
line2_.clear();
apt_num_.clear();
city_.clear();
state_.clear();
country_.clear();
zip_code_.clear();
}
void Address::Clone(const Address& address) {
set_line1(address.line1());
set_line2(address.line2());
set_apt_num(address.apt_num());
set_city(address.city());
set_state(address.state());
set_country(address.country());
set_zip_code(address.zip_code());
}
Address::Address(const Address& address)
: FormGroup(),
line1_tokens_(address.line1_tokens_),
line2_tokens_(address.line2_tokens_),
line1_(address.line1_),
line2_(address.line2_),
apt_num_(address.apt_num_),
city_(address.city_),
state_(address.state_),
country_(address.country_),
zip_code_(address.zip_code_) {
}
void Address::set_line1(const string16& line1) {
line1_ = line1;
line1_tokens_.clear();
Tokenize(line1, kAddressSplitChars, &line1_tokens_);
LineTokens::iterator iter;
for (iter = line1_tokens_.begin(); iter != line1_tokens_.end(); ++iter)
*iter = StringToLowerASCII(*iter);
}
void Address::set_line2(const string16& line2) {
line2_ = line2;
line2_tokens_.clear();
Tokenize(line2, kAddressSplitChars, &line2_tokens_);
LineTokens::iterator iter;
for (iter = line2_tokens_.begin(); iter != line2_tokens_.end(); ++iter)
*iter = StringToLowerASCII(*iter);
}
bool Address::IsLine1(const string16& text) const {
return IsLineMatch(text, line1_tokens_);
}
bool Address::IsLine2(const string16& text) const {
return IsLineMatch(text, line2_tokens_);
}
bool Address::IsAptNum(const string16& text) const {
return (StringToLowerASCII(apt_num_) == StringToLowerASCII(text));
}
bool Address::IsCity(const string16& text) const {
return (StringToLowerASCII(city_) == StringToLowerASCII(text));
}
bool Address::IsState(const string16& text) const {
return (StringToLowerASCII(state_) == StringToLowerASCII(text));
}
bool Address::IsCountry(const string16& text) const {
return (StringToLowerASCII(country_) == StringToLowerASCII(text));
}
bool Address::IsZipCode(const string16& text) const {
return zip_code_ == text;
}
bool Address::FindInfoMatchesHelper(const FieldTypeSubGroup& subgroup,
const string16& info,
string16* match) const {
if (match == NULL) {
DLOG(ERROR) << "NULL match string passed in";
return false;
}
match->clear();
if (subgroup == AutoFillType::ADDRESS_LINE1 &&
StartsWith(line1(), info, false)) {
*match = line1();
} else if (subgroup == AutoFillType::ADDRESS_LINE2 &&
StartsWith(line2(), info, false)) {
*match = line2();
} else if (subgroup == AutoFillType::ADDRESS_APT_NUM &&
StartsWith(apt_num(), info, true)) {
*match = apt_num();
} else if (subgroup == AutoFillType::ADDRESS_CITY &&
StartsWith(city(), info, false)) {
*match = city();
} else if (subgroup == AutoFillType::ADDRESS_STATE &&
StartsWith(state(), info, false)) {
*match = state();
} else if (subgroup == AutoFillType::ADDRESS_COUNTRY &&
StartsWith(country(), info, false)) {
*match = country();
} else if (subgroup == AutoFillType::ADDRESS_ZIP &&
StartsWith(zip_code(), info, true)) {
*match = zip_code();
}
return !match->empty();
}
bool Address::IsLineMatch(const string16& text,
const LineTokens& line_tokens) const {
size_t line_tokens_size = line_tokens.size();
if (line_tokens_size == 0)
return false;
LineTokens text_tokens;
Tokenize(text, kAddressSplitChars, &text_tokens);
size_t text_tokens_size = text_tokens.size();
if (text_tokens_size == 0)
return false;
if (text_tokens_size > line_tokens_size)
return false;
// If each of the 'words' contained in the text are also present in the line,
// then we will consider the text to match the line.
LineTokens::iterator iter;
for (iter = text_tokens.begin(); iter != text_tokens.end(); ++iter) {
if (!IsWordInLine(*iter, line_tokens))
return false;
}
return true;
}
bool Address::IsWordInLine(const string16& word,
const LineTokens& line_tokens) const {
LineTokens::const_iterator iter;
for (iter = line_tokens.begin(); iter != line_tokens.end(); ++iter) {
if (StringToLowerASCII(word) == *iter)
return true;
}
return false;
}
|