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