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

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

Issue 374053007: Change PhoneNumber::SetInfo to only apply formatting where there is none (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: better way Created 6 years, 5 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_i18n.h" 5 #include "components/autofill/core/browser/phone_number_i18n.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 number->clear(); 93 number->clear();
94 *i18n_number = PhoneNumber(); 94 *i18n_number = PhoneNumber();
95 95
96 std::string number_text(base::UTF16ToUTF8(value)); 96 std::string number_text(base::UTF16ToUTF8(value));
97 97
98 // Parse phone number based on the region. 98 // Parse phone number based on the region.
99 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance(); 99 PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance();
100 100
101 // The |default_region| should already be sanitized. 101 // The |default_region| should already be sanitized.
102 DCHECK_EQ(2U, default_region.size()); 102 DCHECK_EQ(2U, default_region.size());
103 if (phone_util->Parse(number_text, default_region, i18n_number) != 103 if (phone_util->ParseAndKeepRawInput(
104 PhoneNumberUtil::NO_PARSING_ERROR) { 104 number_text, default_region, i18n_number) !=
105 PhoneNumberUtil::NO_PARSING_ERROR) {
105 return false; 106 return false;
106 } 107 }
107 108
108 if (!IsValidPhoneNumber(*i18n_number)) 109 if (!IsValidPhoneNumber(*i18n_number))
109 return false; 110 return false;
110 111
111 std::string national_significant_number; 112 std::string national_significant_number;
112 phone_util->GetNationalSignificantNumber(*i18n_number, 113 phone_util->GetNationalSignificantNumber(*i18n_number,
113 &national_significant_number); 114 &national_significant_number);
114 115
115 int area_length = phone_util->GetLengthOfGeographicalAreaCode(*i18n_number); 116 int area_length = phone_util->GetLengthOfGeographicalAreaCode(*i18n_number);
116 int destination_length = 117 int destination_length =
117 phone_util->GetLengthOfNationalDestinationCode(*i18n_number); 118 phone_util->GetLengthOfNationalDestinationCode(*i18n_number);
118 // Some phones have a destination code in lieu of area code: mobile operators 119 // Some phones have a destination code in lieu of area code: mobile operators
119 // in Europe, toll and toll-free numbers in USA, etc. From our point of view 120 // in Europe, toll and toll-free numbers in USA, etc. From our point of view
120 // these two types of codes are the same. 121 // these two types of codes are the same.
121 if (destination_length > area_length) 122 if (destination_length > area_length)
122 area_length = destination_length; 123 area_length = destination_length;
123 124
124 std::string area_code; 125 std::string area_code;
125 std::string subscriber_number; 126 std::string subscriber_number;
126 if (area_length > 0) { 127 if (area_length > 0) {
127 area_code = national_significant_number.substr(0, area_length); 128 area_code = national_significant_number.substr(0, area_length);
128 subscriber_number = national_significant_number.substr(area_length); 129 subscriber_number = national_significant_number.substr(area_length);
129 } else { 130 } else {
130 subscriber_number = national_significant_number; 131 subscriber_number = national_significant_number;
131 } 132 }
132 *number = base::UTF8ToUTF16(subscriber_number); 133 *number = base::UTF8ToUTF16(subscriber_number);
133 *city_code = base::UTF8ToUTF16(area_code); 134 *city_code = base::UTF8ToUTF16(area_code);
134 *country_code = base::string16();
135
136 phone_util->NormalizeDigitsOnly(&number_text);
137 base::string16 normalized_number(base::UTF8ToUTF16(number_text));
138 135
139 // Check if parsed number has a country code that was not inferred from the 136 // Check if parsed number has a country code that was not inferred from the
140 // region. 137 // region.
141 if (i18n_number->has_country_code()) { 138 if (i18n_number->has_country_code() &&
139 i18n_number->country_code_source() != PhoneNumber::FROM_DEFAULT_COUNTRY) {
142 *country_code = base::UTF8ToUTF16( 140 *country_code = base::UTF8ToUTF16(
143 base::StringPrintf("%d", i18n_number->country_code())); 141 base::StringPrintf("%d", i18n_number->country_code()));
144 if (normalized_number.length() <= national_significant_number.length() &&
145 !StartsWith(normalized_number, *country_code,
146 true /* case_sensitive */)) {
147 country_code->clear();
148 }
149 } 142 }
150 143
151 // The region might be different from what we started with. 144 // The region might be different from what we started with.
152 phone_util->GetRegionCodeForNumber(*i18n_number, inferred_region); 145 phone_util->GetRegionCodeForNumber(*i18n_number, inferred_region);
153 146
154 return true; 147 return true;
155 } 148 }
156 149
157 base::string16 NormalizePhoneNumber(const base::string16& value, 150 base::string16 NormalizePhoneNumber(const base::string16& value,
158 const std::string& region) { 151 const std::string& region) {
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 number_ = other.number_; 293 number_ = other.number_;
301 294
302 formatted_number_ = other.formatted_number_; 295 formatted_number_ = other.formatted_number_;
303 whole_number_ = other.whole_number_; 296 whole_number_ = other.whole_number_;
304 297
305 return *this; 298 return *this;
306 } 299 }
307 300
308 } // namespace i18n 301 } // namespace i18n
309 } // namespace autofill 302 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/core/browser/phone_number.cc ('k') | components/autofill/core/browser/phone_number_i18n_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698