OLD | NEW |
| (Empty) |
1 // Copyright (C) 2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | |
15 // Utility for international phone numbers. | |
16 // | |
17 // Author: Shaopeng Jia | |
18 // Open-sourced by: Philippe Liard | |
19 | |
20 #ifndef I18N_PHONENUMBERS_PHONENUMBERUTIL_H_ | |
21 #define I18N_PHONENUMBERS_PHONENUMBERUTIL_H_ | |
22 | |
23 #include <list> | |
24 #include <map> | |
25 #include <set> | |
26 #include <string> | |
27 #include <utility> | |
28 #include <vector> | |
29 | |
30 #include "base/basictypes.h" | |
31 #include "base/memory/scoped_ptr.h" | |
32 #include "base/memory/singleton.h" | |
33 #include "phonenumber.pb.h" | |
34 | |
35 class TelephoneNumber; | |
36 | |
37 namespace i18n { | |
38 namespace phonenumbers { | |
39 | |
40 using std::list; | |
41 using std::map; | |
42 using std::pair; | |
43 using std::set; | |
44 using std::string; | |
45 using std::vector; | |
46 | |
47 using google::protobuf::RepeatedPtrField; | |
48 | |
49 class Logger; | |
50 class NumberFormat; | |
51 class PhoneMetadata; | |
52 class PhoneMetadataCollection; | |
53 class PhoneNumber; | |
54 | |
55 // NOTE: A lot of methods in this class require Region Code strings. These must | |
56 // be provided using ISO 3166-1 two-letter country-code format. The list of the | |
57 // codes can be found here: | |
58 // http://www.iso.org/iso/english_country_names_and_code_elements | |
59 | |
60 class PhoneNumberUtil { | |
61 friend struct DefaultSingletonTraits<PhoneNumberUtil>; | |
62 friend class PhoneNumberUtilTest; | |
63 public: | |
64 ~PhoneNumberUtil(); | |
65 | |
66 // INTERNATIONAL and NATIONAL formats are consistent with the definition | |
67 // in ITU-T Recommendation E. 123. For example, the number of the Google | |
68 // Zurich office will be written as "+41 44 668 1800" in INTERNATIONAL | |
69 // format, and as "044 668 1800" in NATIONAL format. E164 format is as per | |
70 // INTERNATIONAL format but with no formatting applied e.g. +41446681800. | |
71 // RFC3966 is as per INTERNATIONAL format, but with all spaces and other | |
72 // separating symbols replaced with a hyphen, and with any phone number | |
73 // extension appended with ";ext=". | |
74 enum PhoneNumberFormat { | |
75 E164, | |
76 INTERNATIONAL, | |
77 NATIONAL, | |
78 RFC3966 | |
79 }; | |
80 | |
81 // Type of phone numbers. | |
82 enum PhoneNumberType { | |
83 FIXED_LINE, | |
84 MOBILE, | |
85 // In some regions (e.g. the USA), it is impossible to distinguish between | |
86 // fixed-line and mobile numbers by looking at the phone number itself. | |
87 FIXED_LINE_OR_MOBILE, | |
88 // Freephone lines | |
89 TOLL_FREE, | |
90 PREMIUM_RATE, | |
91 // The cost of this call is shared between the caller and the recipient, and | |
92 // is hence typically less than PREMIUM_RATE calls. See | |
93 // http://en.wikipedia.org/wiki/Shared_Cost_Service for more information. | |
94 SHARED_COST, | |
95 // Voice over IP numbers. This includes TSoIP (Telephony Service over IP). | |
96 VOIP, | |
97 // A personal number is associated with a particular person, and may be | |
98 // routed to either a MOBILE or FIXED_LINE number. Some more information can | |
99 // be found here: http://en.wikipedia.org/wiki/Personal_Numbers | |
100 PERSONAL_NUMBER, | |
101 PAGER, | |
102 // Used for "Universal Access Numbers" or "Company Numbers". They may be | |
103 // further routed to specific offices, but allow one number to be used for a | |
104 // company. | |
105 UAN, | |
106 // A phone number is of type UNKNOWN when it does not fit any of the known | |
107 // patterns for a specific region. | |
108 UNKNOWN | |
109 }; | |
110 | |
111 // Types of phone number matches. See detailed description beside the | |
112 // IsNumberMatch() method. | |
113 enum MatchType { | |
114 INVALID_NUMBER, // NOT_A_NUMBER in the java version. | |
115 NO_MATCH, | |
116 SHORT_NSN_MATCH, | |
117 NSN_MATCH, | |
118 EXACT_MATCH, | |
119 }; | |
120 | |
121 enum ErrorType { | |
122 NO_PARSING_ERROR, | |
123 INVALID_COUNTRY_CODE_ERROR, // INVALID_COUNTRY_CODE in the java version. | |
124 NOT_A_NUMBER, | |
125 TOO_SHORT_AFTER_IDD, | |
126 TOO_SHORT_NSN, | |
127 TOO_LONG_NSN, // TOO_LONG in the java version. | |
128 }; | |
129 | |
130 // Possible outcomes when testing if a PhoneNumber is possible. | |
131 enum ValidationResult { | |
132 IS_POSSIBLE, | |
133 INVALID_COUNTRY_CODE, | |
134 TOO_SHORT, | |
135 TOO_LONG, | |
136 }; | |
137 | |
138 // Gets a PhoneNumberUtil instance to carry out international phone number | |
139 // formatting, parsing, or validation. The instance is loaded with phone | |
140 // number metadata for a number of most commonly used regions, as specified by | |
141 // DEFAULT_REGIONS_. | |
142 // | |
143 // The PhoneNumberUtil is implemented as a singleton. Therefore, calling | |
144 // getInstance multiple times will only result in one instance being created. | |
145 static PhoneNumberUtil* GetInstance(); | |
146 | |
147 // Initialisation helper function used to populate the regular expressions in | |
148 // a defined order. | |
149 void CreateRegularExpressions() const; | |
150 | |
151 // Returns true if the number is a valid vanity (alpha) number such as 800 | |
152 // MICROSOFT. A valid vanity number will start with at least 3 digits and will | |
153 // have three or more alpha characters. This does not do region-specific | |
154 // checks - to work out if this number is actually valid for a region, it | |
155 // should be parsed and methods such as IsPossibleNumberWithReason or | |
156 // IsValidNumber should be used. | |
157 bool IsAlphaNumber(const string& number) const; | |
158 | |
159 // Converts all alpha characters in a number to their respective digits on | |
160 // a keypad, but retains existing formatting. | |
161 void ConvertAlphaCharactersInNumber(string* number) const; | |
162 | |
163 // Normalizes a string of characters representing a phone number. This | |
164 // converts wide-ascii and arabic-indic numerals to European numerals, and | |
165 // strips punctuation and alpha characters. | |
166 static void NormalizeDigitsOnly(string* number); | |
167 | |
168 // Gets the national significant number of a phone number. Note a national | |
169 // significant number doesn't contain a national prefix or any formatting. | |
170 void GetNationalSignificantNumber(const PhoneNumber& number, | |
171 string* national_significant_num) const; | |
172 | |
173 // Gets the length of the geographical area code from the PhoneNumber object | |
174 // passed in, so that clients could use it to split a national significant | |
175 // number into geographical area code and subscriber number. It works in such | |
176 // a way that the resultant subscriber number should be diallable, at least on | |
177 // some devices. An example of how this could be used: | |
178 // | |
179 // const PhoneNumberUtil& phone_util(PhoneNumberUtil::GetInstance()); | |
180 // PhoneNumber number; | |
181 // phone_util.Parse("16502530000", "US", &number); | |
182 // string national_significant_number; | |
183 // phone_util.GetNationalSignificantNumber(number, | |
184 // &national_significant_number); | |
185 // string area_code; | |
186 // string subscriber_number; | |
187 // | |
188 // int area_code_length = phone_util.GetLengthOfGeographicalAreaCode(number); | |
189 // if (area_code_length > 0) { | |
190 // area_code = national_significant_number.substring(0, area_code_length); | |
191 // subscriber_number = national_significant_number.substring( | |
192 // area_code_length, string::npos); | |
193 // else { | |
194 // area_code = ""; | |
195 // subscriber_number = national_significant_number; | |
196 // } | |
197 // | |
198 // N.B.: area code is a very ambiguous concept, so the I18N team generally | |
199 // recommends against using it for most purposes, but recommends using the | |
200 // more general national_number instead. Read the following carefully before | |
201 // deciding to use this method: | |
202 // | |
203 // - geographical area codes change over time, and this method honors those | |
204 // changes; therefore, it doesn't guarantee the stability of the result it | |
205 // produces. | |
206 // - subscriber numbers may not be diallable from all devices (notably mobile | |
207 // devices, which typically requires the full national_number to be dialled | |
208 // in most regions). | |
209 // - most non-geographical numbers have no area codes. | |
210 // - some geographical numbers have no area codes. | |
211 int GetLengthOfGeographicalAreaCode(const PhoneNumber& number) const; | |
212 | |
213 // Gets the length of the national destination code (NDC) from the PhoneNumber | |
214 // object passed in, so that clients could use it to split a national | |
215 // significant number into NDC and subscriber number. The NDC of a phone | |
216 // number is normally the first group of digit(s) right after the country | |
217 // calling code when the number is formatted in the international format, if | |
218 // there is a subscriber number part that follows. An example of how this | |
219 // could be used: | |
220 // | |
221 // const PhoneNumberUtil& phone_util(PhoneNumberUtil::GetInstance()); | |
222 // PhoneNumber number; | |
223 // phone_util.Parse("16502530000", "US", &number); | |
224 // string national_significant_number; | |
225 // phone_util.GetNationalSignificantNumber(number, | |
226 // &national_significant_number); | |
227 // string national_destination_code; | |
228 // string subscriber_number; | |
229 // | |
230 // int national_destination_code_length = | |
231 // phone_util.GetLengthOfGeographicalAreaCode(number); | |
232 // if (national_destination_code_length > 0) { | |
233 // national_destination_code = national_significant_number.substring( | |
234 // 0, national_destination_code_length); | |
235 // subscriber_number = national_significant_number.substring( | |
236 // national_destination_code_length, string::npos); | |
237 // else { | |
238 // national_destination_code = ""; | |
239 // subscriber_number = national_significant_number; | |
240 // } | |
241 // | |
242 // Refer to the unittests to see the difference between this function and | |
243 // GetLengthOfGeographicalAreaCode(). | |
244 int GetLengthOfNationalDestinationCode(const PhoneNumber& number) const; | |
245 | |
246 // Formats a phone number in the specified format using default rules. Note | |
247 // that this does not promise to produce a phone number that the user can | |
248 // dial from where they are - although we do format in either NATIONAL or | |
249 // INTERNATIONAL format depending on what the client asks for, we do not | |
250 // currently support a more abbreviated format, such as for users in the | |
251 // same area who could potentially dial the number without area code. | |
252 void Format(const PhoneNumber& number, | |
253 PhoneNumberFormat number_format, | |
254 string* formatted_number) const; | |
255 | |
256 // Formats a phone number in the specified format using client-defined | |
257 // formatting rules. | |
258 void FormatByPattern( | |
259 const PhoneNumber& number, | |
260 PhoneNumberFormat number_format, | |
261 const RepeatedPtrField<NumberFormat>& user_defined_formats, | |
262 string* formatted_number) const; | |
263 | |
264 // Formats a phone number in national format for dialing using the carrier as | |
265 // specified in the carrier_code. The carrier_code will always be used | |
266 // regardless of whether the phone number already has a preferred domestic | |
267 // carrier code stored. If carrier_code contains an empty string, return the | |
268 // number in national format without any carrier code. | |
269 void FormatNationalNumberWithCarrierCode(const PhoneNumber& number, | |
270 const string& carrier_code, | |
271 string* formatted_number) const; | |
272 | |
273 // Formats a phone number in national format for dialing using the carrier as | |
274 // specified in the preferred_domestic_carrier_code field of the PhoneNumber | |
275 // object passed in. If that is missing, use the fallback_carrier_code passed | |
276 // in instead. If there is no preferred_domestic_carrier_code, and the | |
277 // fallback_carrier_code contains an empty string, return the number in | |
278 // national format without any carrier code. | |
279 // | |
280 // Use FormatNationalNumberWithCarrierCode instead if the carrier code passed | |
281 // in should take precedence over the number's preferred_domestic_carrier_code | |
282 // when formatting. | |
283 void FormatNationalNumberWithPreferredCarrierCode( | |
284 const PhoneNumber& number, | |
285 const string& fallback_carrier_code, | |
286 string* formatted_number) const; | |
287 | |
288 // Formats a phone number for out-of-country dialing purposes. | |
289 // | |
290 // Note this function takes care of the case for calling inside of NANPA | |
291 // and between Russia and Kazakhstan (who share the same country calling | |
292 // code). In those cases, no international prefix is used. For regions which | |
293 // have multiple international prefixes, the number in its INTERNATIONAL | |
294 // format will be returned instead. | |
295 void FormatOutOfCountryCallingNumber( | |
296 const PhoneNumber& number, | |
297 const string& calling_from, | |
298 string* formatted_number) const; | |
299 | |
300 // Formats a phone number using the original phone number format that the | |
301 // number is parsed from. The original format is embedded in the | |
302 // country_code_source field of the PhoneNumber object passed in. If such | |
303 // information is missing, the number will be formatted into the NATIONAL | |
304 // format by default. | |
305 void FormatInOriginalFormat(const PhoneNumber& number, | |
306 const string& region_calling_from, | |
307 string* formatted_number) const; | |
308 | |
309 // Formats a phone number for out-of-country dialing purposes. | |
310 // | |
311 // Note that in this version, if the number was entered originally using alpha | |
312 // characters and this version of the number is stored in raw_input, this | |
313 // representation of the number will be used rather than the digit | |
314 // representation. Grouping information, as specified by characters such as | |
315 // "-" and " ", will be retained. | |
316 // | |
317 // Caveats: | |
318 // 1) This will not produce good results if the country calling code is both | |
319 // present in the raw input _and_ is the start of the national number. This | |
320 // is not a problem in the regions which typically use alpha numbers. | |
321 // 2) This will also not produce good results if the raw input has any | |
322 // grouping information within the first three digits of the national number, | |
323 // and if the function needs to strip preceding digits/words in the raw input | |
324 // before these digits. Normally people group the first three digits together | |
325 // so this is not a huge problem - and will be fixed if it proves to be so. | |
326 void FormatOutOfCountryKeepingAlphaChars( | |
327 const PhoneNumber& number, | |
328 const string& calling_from, | |
329 string* formatted_number) const; | |
330 | |
331 // Attempts to extract a valid number from a phone number that is too long to | |
332 // be valid, and resets the PhoneNumber object passed in to that valid | |
333 // version. If no valid number could be extracted, the PhoneNumber object | |
334 // passed in will not be modified. It returns true if a valid phone number can | |
335 // be successfully extracted. | |
336 bool TruncateTooLongNumber(PhoneNumber* number) const; | |
337 | |
338 // Gets the type of a phone number. | |
339 PhoneNumberType GetNumberType(const PhoneNumber& number) const; | |
340 | |
341 // Tests whether a phone number matches a valid pattern. Note this doesn't | |
342 // verify the number is actually in use, which is impossible to tell by just | |
343 // looking at a number itself. | |
344 bool IsValidNumber(const PhoneNumber& number) const; | |
345 | |
346 // Tests whether a phone number is valid for a certain region. Note this | |
347 // doesn't verify the number is actually in use, which is impossible to tell | |
348 // by just looking at a number itself. If the country calling code is not the | |
349 // same as the country calling code for the region, this immediately exits | |
350 // with false. After this, the specific number pattern rules for the region | |
351 // are examined. | |
352 // This is useful for determining for example whether a particular number is | |
353 // valid for Canada, rather than just a valid NANPA number. | |
354 bool IsValidNumberForRegion( | |
355 const PhoneNumber& number, | |
356 const string& region_code) const; | |
357 | |
358 // Returns the region where a phone number is from. This could be used for | |
359 // geo-coding at the region level. | |
360 void GetRegionCodeForNumber(const PhoneNumber& number, | |
361 string* region_code) const; | |
362 | |
363 // Returns the country calling code for a specific region. For example, | |
364 // this would be 1 for the United States, and 64 for New Zealand. | |
365 int GetCountryCodeForRegion(const string& region_code) const; | |
366 | |
367 // Returns the region code that matches the specific country code. Note that | |
368 // it is possible that several regions share the same country code (e.g. US | |
369 // and Canada), and in that case, only one of the regions (normally the one | |
370 // with the largest population) is returned. | |
371 void GetRegionCodeForCountryCode(int country_code, string* region_code) const; | |
372 | |
373 // Checks if this is a region under the North American Numbering Plan | |
374 // Administration (NANPA). | |
375 bool IsNANPACountry(const string& region_code) const; | |
376 | |
377 // Checks whether a phone number is a possible number. It provides a more | |
378 // lenient check than IsValidNumber() in the following sense: | |
379 // 1. It only checks the length of phone numbers. In particular, it doesn't | |
380 // check starting digits of the number. | |
381 // 2. It doesn't attempt to figure out the type of the number, but uses | |
382 // general rules which applies to all types of phone numbers in a | |
383 // region. Therefore, it is much faster than IsValidNumber(). | |
384 // 3. For fixed line numbers, many regions have the concept of area code, | |
385 // which together with subscriber number constitute the national | |
386 // significant number. It is sometimes okay to dial the subscriber | |
387 // number only when dialing in the same area. This function will return | |
388 // true if the subscriber-number-only version is passed in. On the other | |
389 // hand, because IsValidNumber() validates using information on both | |
390 // starting digits (for fixed line numbers, that would most likely be | |
391 // area codes) and length (obviously includes the length of area codes | |
392 // for fixed line numbers), it will return false for the | |
393 // subscriber-number-only version. | |
394 ValidationResult IsPossibleNumberWithReason(const PhoneNumber& number) const; | |
395 | |
396 // Convenience wrapper around IsPossibleNumberWithReason. Instead of returning | |
397 // the reason for failure, this method returns a boolean value. | |
398 bool IsPossibleNumber(const PhoneNumber& number) const; | |
399 | |
400 // Checks whether a phone number is a possible number given a number in the | |
401 // form of a string, and the country where the number could be dialed from. | |
402 // It provides a more lenient check than IsValidNumber(). See | |
403 // IsPossibleNumber(const PhoneNumber& number) for details. | |
404 // | |
405 // This method first parses the number, then invokes | |
406 // IsPossibleNumber(const PhoneNumber& number) with the resultant PhoneNumber | |
407 // object. | |
408 // | |
409 // region_dialing_from represents the region that we are expecting the number | |
410 // to be dialed from. Note this is different from the region where the number | |
411 // belongs. For example, the number +1 650 253 0000 is a number that belongs | |
412 // to US. When written in this form, it could be dialed from any region. When | |
413 // it is written as 00 1 650 253 0000, it could be dialed from any region | |
414 // which uses an international dialling prefix of 00. When it is written as | |
415 // 650 253 0000, it could only be dialed from within the US, and when written | |
416 // as 253 0000, it could only be dialed from within a smaller area in the US | |
417 // (Mountain View, CA, to be more specific). | |
418 bool IsPossibleNumberForString( | |
419 const string& number, | |
420 const string& region_dialing_from) const; | |
421 | |
422 // Gets a valid fixed-line number for the specified region. Returns false if | |
423 // the region was unknown. | |
424 bool GetExampleNumber(const string& region_code, | |
425 PhoneNumber* number) const; | |
426 | |
427 // Gets a valid number of the specified type for the specified region. | |
428 // Returns false if the region was unknown or if no example number of that | |
429 // type could be found. | |
430 bool GetExampleNumberForType(const string& region_code, | |
431 PhoneNumberType type, | |
432 PhoneNumber* number) const; | |
433 | |
434 // Parses a string and returns it in proto buffer format. This method will | |
435 // return an error like INVALID_COUNTRY_CODE if the number is not considered | |
436 // to be a possible number, and NO_PARSING_ERROR if it parsed correctly. Note | |
437 // that validation of whether the number is actually a valid number for a | |
438 // particular region is not performed. This can be done separately with | |
439 // IsValidNumber(). | |
440 // | |
441 // default_region represents the country that we are expecting the number to | |
442 // be from. This is only used if the number being parsed is not written in | |
443 // international format. The country_code for the number in this case would be | |
444 // stored as that of the default country supplied. If the number is guaranteed | |
445 // to start with a '+' followed by the country calling code, then | |
446 // "ZZ" can be supplied. | |
447 ErrorType Parse(const string& number_to_parse, | |
448 const string& default_region, | |
449 PhoneNumber* number) const; | |
450 // Parses a string and returns it in proto buffer format. This method differs | |
451 // from Parse() in that it always populates the raw_input field of the | |
452 // protocol buffer with number_to_parse as well as the country_code_source | |
453 // field. | |
454 ErrorType ParseAndKeepRawInput(const string& number_to_parse, | |
455 const string& default_region, | |
456 PhoneNumber* number) const; | |
457 | |
458 // Takes two phone numbers and compares them for equality. | |
459 // | |
460 // Returns EXACT_MATCH if the country calling code, NSN, presence of a leading | |
461 // zero for Italian numbers and any extension present are the same. | |
462 // Returns NSN_MATCH if either or both has no country calling code specified, | |
463 // and the NSNs and extensions are the same. | |
464 // Returns SHORT_NSN_MATCH if either or both has no country calling code | |
465 // specified, or the country calling code specified is the same, and one NSN | |
466 // could be a shorter version of the other number. This includes the case | |
467 // where one has an extension specified, and the other does not. | |
468 // Returns NO_MATCH otherwise. | |
469 // For example, the numbers +1 345 657 1234 and 657 1234 are a | |
470 // SHORT_NSN_MATCH. The numbers +1 345 657 1234 and 345 657 are a NO_MATCH. | |
471 MatchType IsNumberMatch(const PhoneNumber& first_number, | |
472 const PhoneNumber& second_number) const; | |
473 | |
474 // Takes two phone numbers as strings and compares them for equality. This | |
475 // is a convenience wrapper for IsNumberMatch(PhoneNumber firstNumber, | |
476 // PhoneNumber secondNumber). No default region is known. | |
477 // Returns INVALID_NUMBER if either number cannot be parsed into a phone | |
478 // number. | |
479 MatchType IsNumberMatchWithTwoStrings(const string& first_number, | |
480 const string& second_number) const; | |
481 | |
482 // Takes two phone numbers and compares them for equality. This is a | |
483 // convenience wrapper for IsNumberMatch(PhoneNumber firstNumber, | |
484 // PhoneNumber secondNumber). No default region is known. | |
485 // Returns INVALID_NUMBER if second_number cannot be parsed into a phone | |
486 // number. | |
487 MatchType IsNumberMatchWithOneString(const PhoneNumber& first_number, | |
488 const string& second_number) const; | |
489 | |
490 // Overrides the default logging system. The provided logger destruction is | |
491 // handled by this class (i.e don't delete it). | |
492 static void SetLogger(Logger* logger); | |
493 | |
494 friend bool ConvertFromTelephoneNumberProto( | |
495 const TelephoneNumber& proto_to_convert, | |
496 PhoneNumber* new_proto); | |
497 friend bool ConvertToTelephoneNumberProto(const PhoneNumber& proto_to_convert, | |
498 TelephoneNumber* resulting_proto); | |
499 | |
500 protected: | |
501 // Check whether the country_calling_code is from a country whose national | |
502 // significant number could contain a leading zero. An example of such a | |
503 // country is Italy. | |
504 bool IsLeadingZeroPossible(int country_calling_code) const; | |
505 | |
506 private: | |
507 typedef pair<int, list<string>*> IntRegionsPair; | |
508 | |
509 // The minimum and maximum length of the national significant number. | |
510 static const size_t kMinLengthForNsn = 3; | |
511 static const size_t kMaxLengthForNsn = 15; | |
512 // The maximum length of the country calling code. | |
513 static const size_t kMaxLengthCountryCode = 3; | |
514 | |
515 static const char kPlusChars[]; | |
516 // Regular expression of acceptable punctuation found in phone numbers. This | |
517 // excludes punctuation found as a leading character only. This consists of | |
518 // dash characters, white space characters, full stops, slashes, square | |
519 // brackets, parentheses and tildes. It also includes the letter 'x' as that | |
520 // is found as a placeholder for carrier information in some phone numbers. | |
521 // Full-width variants are also present. | |
522 static const char kValidPunctuation[]; | |
523 | |
524 // A mapping from a country calling code to a region code which denotes the | |
525 // region represented by that country calling code. Note countries under | |
526 // NANPA share the country calling code 1 and Russia and Kazakhstan share the | |
527 // country calling code 7. Under this map, 1 is mapped to region code "US" and | |
528 // 7 is mapped to region code "RU". This is implemented as a sorted vector to | |
529 // achieve better performance. | |
530 scoped_ptr<vector<IntRegionsPair> > country_calling_code_to_region_code_map_; | |
531 | |
532 // The set of regions that share country calling code 1. | |
533 scoped_ptr<set<string> > nanpa_regions_; | |
534 static const int kNanpaCountryCode = 1; | |
535 | |
536 // A mapping from a region code to a PhoneMetadata for that region. | |
537 scoped_ptr<map<string, PhoneMetadata> > region_to_metadata_map_; | |
538 | |
539 PhoneNumberUtil(); | |
540 | |
541 // Returns a regular expression for the possible extensions that may be found | |
542 // in a number. | |
543 const string& GetExtnPatterns() const; | |
544 | |
545 // Trims unwanted end characters from a phone number string. | |
546 void TrimUnwantedEndChars(string* number) const; | |
547 | |
548 // Gets all the supported regions. | |
549 void GetSupportedRegions(set<string>* regions) const; | |
550 | |
551 // Returns the national dialling prefix for a specific region. For example, | |
552 // this would be 1 for the United States, and 0 for New Zealand. Set | |
553 // stripNonDigits to true to strip symbols like "~" (which indicates a wait | |
554 // for a dialling tone) from the prefix returned. If no national prefix is | |
555 // present, we return an empty string. | |
556 // | |
557 // Set strip_non_digits to true to strip non-digits from the national | |
558 // dialling prefix. | |
559 void GetNddPrefixForRegion(const string& region_code, | |
560 bool strip_non_digits, | |
561 string* national_prefix) const; | |
562 | |
563 // Helper function to check region code is not unknown or null. | |
564 bool IsValidRegionCode(const string& region_code) const; | |
565 | |
566 // Helper function to check region code is not unknown. The | |
567 // country_calling_code and number supplied is used only for the resultant log | |
568 // message. | |
569 bool HasValidRegionCode(const string& region_code, | |
570 int country_code, | |
571 const string& number) const; | |
572 | |
573 const i18n::phonenumbers::PhoneMetadata* GetMetadataForRegion( | |
574 const string& region_code) const; | |
575 | |
576 void GetRegionCodesForCountryCallingCode( | |
577 int country_calling_code, | |
578 list<string>* region_codes) const; | |
579 | |
580 // Simple wrapper of FormatNationalNumberWithCarrier for the common case of | |
581 // no carrier code. | |
582 void FormatNationalNumber(const string& number, | |
583 const string& region_code, | |
584 PhoneNumberFormat number_format, | |
585 string* formatted_number) const; | |
586 | |
587 void FormatNationalNumberWithCarrier(const string& number, | |
588 const string& region_code, | |
589 PhoneNumberFormat number_format, | |
590 const string& carrier_code, | |
591 string* formatted_number) const; | |
592 void MaybeGetFormattedExtension( | |
593 const PhoneNumber& number, | |
594 const string& region_code, | |
595 PhoneNumberFormat number_format, | |
596 string* extension) const; | |
597 | |
598 void FormatExtension(const string& extension_digits, | |
599 const string& region_code, | |
600 string* extension) const; | |
601 | |
602 void GetRegionCodeForNumberFromRegionList( | |
603 const PhoneNumber& number, | |
604 const list<string>& region_codes, | |
605 string* region_code) const; | |
606 | |
607 void Normalize(string* number) const; | |
608 PhoneNumber::CountryCodeSource MaybeStripInternationalPrefixAndNormalize( | |
609 const string& possible_idd_prefix, | |
610 string* number) const; | |
611 | |
612 void MaybeStripNationalPrefixAndCarrierCode( | |
613 const PhoneMetadata& metadata, | |
614 string* number, | |
615 string* carrier_code) const; | |
616 | |
617 void ExtractPossibleNumber(const string& number, | |
618 string* extracted_number) const; | |
619 | |
620 bool IsViablePhoneNumber(const string& number) const; | |
621 | |
622 bool MaybeStripExtension(string* number, string* extension) const; | |
623 | |
624 int ExtractCountryCode(string* national_number) const; | |
625 ErrorType MaybeExtractCountryCode( | |
626 const PhoneMetadata* default_region_metadata, | |
627 bool keepRawInput, | |
628 string* national_number, | |
629 PhoneNumber* phone_number) const; | |
630 | |
631 bool CheckRegionForParsing( | |
632 const string& number_to_parse, | |
633 const string& default_region) const; | |
634 | |
635 ErrorType ParseHelper(const string& number_to_parse, | |
636 const string& default_region, | |
637 bool keep_raw_input, | |
638 bool check_region, | |
639 PhoneNumber* phone_number) const; | |
640 | |
641 DISALLOW_COPY_AND_ASSIGN(PhoneNumberUtil); | |
642 }; | |
643 | |
644 } // namespace phonenumbers | |
645 } // namespace i18n | |
646 | |
647 #endif // I18N_PHONENUMBERS_PHONENUMBERUTIL_H_ | |
OLD | NEW |