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

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

Issue 296593003: Make various string_util functions take StringPieces instead of char[]. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Resync Created 6 years, 6 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 | Annotate | Revision Log
OLDNEW
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/phone_number.h" 5 #include "components/autofill/core/browser/phone_number.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/strings/string_number_conversions.h" 8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "components/autofill/core/browser/autofill_country.h" 11 #include "components/autofill/core/browser/autofill_country.h"
12 #include "components/autofill/core/browser/autofill_profile.h" 12 #include "components/autofill/core/browser/autofill_profile.h"
13 #include "components/autofill/core/browser/autofill_type.h" 13 #include "components/autofill/core/browser/autofill_type.h"
14 #include "components/autofill/core/browser/field_types.h" 14 #include "components/autofill/core/browser/field_types.h"
15 #include "components/autofill/core/browser/phone_number_i18n.h" 15 #include "components/autofill/core/browser/phone_number_i18n.h"
16 16
17 namespace autofill { 17 namespace autofill {
18 namespace { 18 namespace {
19 19
20 const base::char16 kPhoneNumberSeparators[] = { ' ', '.', '(', ')', '-', 0 };
21
22 void StripPunctuation(base::string16* number) {
23 base::RemoveChars(*number, kPhoneNumberSeparators, number);
24 }
25
26 // Returns the region code for this phone number, which is an ISO 3166 2-letter 20 // Returns the region code for this phone number, which is an ISO 3166 2-letter
27 // country code. The returned value is based on the |profile|; if the |profile| 21 // country code. The returned value is based on the |profile|; if the |profile|
28 // does not have a country code associated with it, falls back to the country 22 // does not have a country code associated with it, falls back to the country
29 // code corresponding to the |app_locale|. 23 // code corresponding to the |app_locale|.
30 std::string GetRegion(const AutofillProfile& profile, 24 std::string GetRegion(const AutofillProfile& profile,
31 const std::string& app_locale) { 25 const std::string& app_locale) {
32 base::string16 country_code = profile.GetRawInfo(ADDRESS_HOME_COUNTRY); 26 base::string16 country_code = profile.GetRawInfo(ADDRESS_HOME_COUNTRY);
33 if (!country_code.empty()) 27 if (!country_code.empty())
34 return base::UTF16ToASCII(country_code); 28 return base::UTF16ToASCII(country_code);
35 29
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 // Store a formatted (i.e., pretty printed) version of the number. 137 // Store a formatted (i.e., pretty printed) version of the number.
144 UpdateCacheIfNeeded(app_locale); 138 UpdateCacheIfNeeded(app_locale);
145 number_ = cached_parsed_phone_.GetFormattedNumber(); 139 number_ = cached_parsed_phone_.GetFormattedNumber();
146 return !number_.empty(); 140 return !number_.empty();
147 } 141 }
148 142
149 void PhoneNumber::GetMatchingTypes(const base::string16& text, 143 void PhoneNumber::GetMatchingTypes(const base::string16& text,
150 const std::string& app_locale, 144 const std::string& app_locale,
151 ServerFieldTypeSet* matching_types) const { 145 ServerFieldTypeSet* matching_types) const {
152 base::string16 stripped_text = text; 146 base::string16 stripped_text = text;
153 StripPunctuation(&stripped_text); 147 base::RemoveChars(stripped_text, base::ASCIIToUTF16(" .()-"), &stripped_text);
154 FormGroup::GetMatchingTypes(stripped_text, app_locale, matching_types); 148 FormGroup::GetMatchingTypes(stripped_text, app_locale, matching_types);
155 149
156 // For US numbers, also compare to the three-digit prefix and the four-digit 150 // For US numbers, also compare to the three-digit prefix and the four-digit
157 // suffix, since web sites often split numbers into these two fields. 151 // suffix, since web sites often split numbers into these two fields.
158 base::string16 number = GetInfo(AutofillType(PHONE_HOME_NUMBER), app_locale); 152 base::string16 number = GetInfo(AutofillType(PHONE_HOME_NUMBER), app_locale);
159 if (GetRegion(*profile_, app_locale) == "US" && 153 if (GetRegion(*profile_, app_locale) == "US" &&
160 number.size() == (kPrefixLength + kSuffixLength)) { 154 number.size() == (kPrefixLength + kSuffixLength)) {
161 base::string16 prefix = number.substr(kPrefixOffset, kPrefixLength); 155 base::string16 prefix = number.substr(kPrefixOffset, kPrefixLength);
162 base::string16 suffix = number.substr(kSuffixOffset, kSuffixLength); 156 base::string16 suffix = number.substr(kSuffixOffset, kSuffixLength);
163 if (text == prefix || text == suffix) 157 if (text == prefix || text == suffix)
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 225
232 return i18n::ConstructPhoneNumber( 226 return i18n::ConstructPhoneNumber(
233 country_, city_, phone_, GetRegion(profile, app_locale), value); 227 country_, city_, phone_, GetRegion(profile, app_locale), value);
234 } 228 }
235 229
236 bool PhoneNumber::PhoneCombineHelper::IsEmpty() const { 230 bool PhoneNumber::PhoneCombineHelper::IsEmpty() const {
237 return phone_.empty() && whole_number_.empty(); 231 return phone_.empty() && whole_number_.empty();
238 } 232 }
239 233
240 } // namespace autofill 234 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/core/browser/credit_card.cc ('k') | components/autofill/core/browser/validation.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698