Lines Matching +full:is +full:- +full:number
7 // http://www.apache.org/licenses/LICENSE-2.0
10 // distributed under the License is distributed on an "AS IS" BASIS,
69 // find numbers in text and to decide what is a viable phone number. This
73 // 'x' as that is found as a placeholder for carrier information in some phone
74 // numbers. Full-width variants are also present.
75 // To find out the unicode code-point of the characters below in vim, highlight
76 // the character and type 'ga'. Note that the - is used to express ranges of
77 // full-width punctuation below, as well as being present in the expression
78 // itself. In emacs, you can use M-x unicode-what to query information about the
82 /* "-x‐-―−ー--/ <U+200B><U+2060> ()()[].\\[\\]/~⁓∼" */
83 "-x\xE2\x80\x90-\xE2\x80\x95\xE2\x88\x92\xE3\x83\xBC\xEF\xBC\x8D-\xEF\xBC"
102 const char kRfc3966PhoneContext[] = ";phone-context=";
104 const char kRfc3966VisualSeparator[] = "[\\-\\.\\(\\)]?";
107 // We accept alpha characters in phone numbers, ASCII only. We store lower-case
108 // here only since our regular expressions are case-insensitive.
109 const char kValidAlpha[] = "a-z";
110 const char kValidAlphaInclUppercase[] = "A-Za-z";
113 // any extension component of the number, after the main national number is
116 // prefix. This can be overridden by region-specific preferences.
125 "[:\\.\xEF\xBC\x8E]?[ \xC2\xA0\\t,-]*";
129 if (!metadata->ParseFromArray(metadata_get(), metadata_size())) { in LoadCompiledInMetadata()
168 // A helper function that is used by Format and FormatByPattern.
175 formatted_number->insert(0, StrCat(kPlusSign, country_calling_code)); in PrefixNumberWithCountryCallingCode()
178 formatted_number->insert(0, StrCat(kPlusSign, country_calling_code, " ")); in PrefixNumberWithCountryCallingCode()
181 formatted_number->insert(0, StrCat(kRfc3966Prefix, kPlusSign, in PrefixNumberWithCountryCallingCode()
182 country_calling_code, "-")); in PrefixNumberWithCountryCallingCode()
191 // Returns true when one national number is the suffix of the other or both are
218 // Helper initialiser method to create the regular-expression pattern to match
220 // - There are currently six capturing groups for the extension itself. If this
221 // number is changed, MaybeStripExtension needs to be updated.
222 // - The only capturing groups should be around the digits that you want to
226 // way the extension is prefixed. As per ITU, the officially allowed in CreateExtnPattern()
227 // length for extensions is actually 40, but we don't support this since we in CreateExtnPattern()
235 // Canonical-equivalence doesn't seem to be an option with RE2, so we allow in CreateExtnPattern()
236 // two options for representing any non-ASCII character like ó - the character in CreateExtnPattern()
240 // Here the extension is called out in a more explicit way, i.e mentioning it in CreateExtnPattern()
246 // One-character symbols that can be used to indicate an extension, and less in CreateExtnPattern()
251 // When extension is not separated clearly. in CreateExtnPattern()
252 string ambiguous_separator = "[- ]+"; in CreateExtnPattern()
269 // The first regular expression covers RFC 3966 format, where the extension is in CreateExtnPattern()
270 // added using ";ext=". The second more generic where extension is mentioned in CreateExtnPattern()
276 // other as a number + extension. The fourth one covers the special case of in CreateExtnPattern()
277 // American numbers where the extension is written with a hash at the end, in CreateExtnPattern()
278 // such as "- 503#". in CreateExtnPattern()
284 // Additional pattern that is supported when parsing extensions, not when in CreateExtnPattern()
287 // ",," is commonly used for auto dialling the extension when connected. in CreateExtnPattern()
288 // Semi-colon works in Iphone and also in Android to pop up a button with in CreateExtnPattern()
289 // the extension number following. in CreateExtnPattern()
291 // This is same as kPossibleSeparatorsBetweenNumberAndExtLabel, but not in CreateExtnPattern()
305 // Here the first pattern is exclusive for extension autodialling formats in CreateExtnPattern()
307 // extensions. However, the second pattern is more liberal on number of in CreateExtnPattern()
308 // commas that acts as extension labels, so we have strict cap on number of in CreateExtnPattern()
317 // Normalizes a string of characters representing a phone number by replacing
319 // stripping all other characters if remove_non_matches is true.
321 // number - a pointer to a string of characters representing a phone number to
323 // normalization_replacements - a mapping of characters to what they should be
324 // replaced by in the normalized version of the phone number
325 // remove_non_matches - indicates whether characters that are not able to be
326 // replaced should be stripped from the number. If this is false, they will be
327 // left unchanged in the number.
330 string* number) { in NormalizeHelper() argument
331 DCHECK(number); in NormalizeHelper()
333 number_as_unicode.PointToUTF8(number->data(), static_cast<int>(number->size())); in NormalizeHelper()
335 // The input wasn't valid UTF-8. Produce an empty string to indicate an error. in NormalizeHelper()
336 number->clear(); in NormalizeHelper()
347 normalized_number.push_back(found_glyph_pair->second); in NormalizeHelper()
349 // Find out how long this unicode char is so we can append it all. in NormalizeHelper()
355 number->assign(normalized_number); in NormalizeHelper()
358 // Returns true if there is any possible number data set for a particular
361 // If this is empty, it means numbers of this type inherit from the "general in DescHasPossibleNumberData()
362 // desc" -> the value "-1" means that no numbers exist for this type. in DescHasPossibleNumberData()
363 return desc.possible_length_size() != 1 || desc.possible_length(0) != -1; in DescHasPossibleNumberData()
369 // excludableChildFields are still being checked. If your change is safe simply
371 // Returns true if there is any data set for a particular PhoneNumberDesc.
376 // since if this is the only thing that's present we don't really support the in DescHasData()
377 // type at all: no type-specific methods will work with only this data. in DescHasData()
393 // Never return FIXED_LINE_OR_MOBILE (it is a convenience type, and in GetSupportedTypesForMetadata()
394 // represents that a particular number type can't be in GetSupportedTypesForMetadata()
395 // determined) or UNKNOWN (the non-type). in GetSupportedTypesForMetadata()
399 types->insert(type); in GetSupportedTypesForMetadata()
404 // Helper method to check a number against possible lengths for this number
405 // type, and determine whether it matches, or is too short or too long.
407 const string& number, const PhoneMetadata& metadata, in TestNumberLength() argument
410 // There should always be "possibleLengths" set for every element. This is in TestNumberLength()
411 // declared in the XML schema which is verified by in TestNumberLength()
413 // sub-description (e.g. fixed-line) has the same possibleLengths as the in TestNumberLength()
414 // parent, this is missing, so we fall back to the general desc (where no in TestNumberLength()
415 // numbers of the type exist at all, there is one possible length (-1) which in TestNumberLength()
416 // is guaranteed not to match the length of any real phone number). in TestNumberLength()
418 desc_for_type->possible_length_size() == 0 in TestNumberLength()
420 : desc_for_type->possible_length(); in TestNumberLength()
422 desc_for_type->possible_length_local_only(); in TestNumberLength()
427 // The rare case has been encountered where no fixedLine data is available in TestNumberLength()
428 // (true for some non-geographical entities), so we just check mobile. in TestNumberLength()
429 return TestNumberLength(number, metadata, PhoneNumberUtil::MOBILE); in TestNumberLength()
439 mobile_desc->possible_length_size() == 0 in TestNumberLength()
441 : mobile_desc->possible_length()); in TestNumberLength()
445 local_lengths = mobile_desc->possible_length_local_only(); in TestNumberLength()
447 local_lengths.MergeFrom(mobile_desc->possible_length_local_only()); in TestNumberLength()
454 // If the type is not suported at all (indicated by the possible lengths in TestNumberLength()
455 // containing -1 at this point) we return invalid length. in TestNumberLength()
456 if (possible_lengths.Get(0) == -1) { in TestNumberLength()
460 int actual_length = static_cast<int>(number.length()); in TestNumberLength()
461 // This is safe because there is never an overlap beween the possible lengths in TestNumberLength()
462 // and the local-only lengths; this is checked at build time. in TestNumberLength()
472 } else if (*(possible_lengths.end() - 1) < actual_length) { in TestNumberLength()
482 // Helper method to check a number against possible lengths for this region,
484 // is too short or too long.
486 const string& number, const PhoneMetadata& metadata) { in TestNumberLength() argument
487 return TestNumberLength(number, metadata, PhoneNumberUtil::UNKNOWN); in TestNumberLength()
490 // Returns a new phone number containing only the fields needed to uniquely
491 // identify a phone number, rather than any fields that capture the context in
492 // which the phone number was created.
495 void CopyCoreFieldsOnly(const PhoneNumber& number, PhoneNumber* pruned_number) { in CopyCoreFieldsOnly() argument
496 pruned_number->set_country_code(number.country_code()); in CopyCoreFieldsOnly()
497 pruned_number->set_national_number(number.national_number()); in CopyCoreFieldsOnly()
498 if (!number.extension().empty()) { in CopyCoreFieldsOnly()
499 pruned_number->set_extension(number.extension()); in CopyCoreFieldsOnly()
501 if (number.italian_leading_zero()) { in CopyCoreFieldsOnly()
502 pruned_number->set_italian_leading_zero(true); in CopyCoreFieldsOnly()
503 // This field is only relevant if there are leading zeros at all. in CopyCoreFieldsOnly()
504 pruned_number->set_number_of_leading_zeros( in CopyCoreFieldsOnly()
505 number.number_of_leading_zeros()); in CopyCoreFieldsOnly()
509 // Determines whether the given number is a national number match for the given
512 const string& number, const PhoneNumberDesc& desc) { in IsMatch() argument
513 return matcher_api.MatchNationalNumber(number, desc, false); in IsMatch()
530 // formatting alpha numbers, as they show the intended number groupings. in InitializeMapsAndSets()
532 std::make_pair(ToUnicodeCodepoint("-"), '-')); in InitializeMapsAndSets()
534 std::make_pair(ToUnicodeCodepoint("\xEF\xBC\x8D" /* "-" */), '-')); in InitializeMapsAndSets()
536 std::make_pair(ToUnicodeCodepoint("\xE2\x80\x90" /* "‐" */), '-')); in InitializeMapsAndSets()
538 std::make_pair(ToUnicodeCodepoint("\xE2\x80\x91" /* "‑" */), '-')); in InitializeMapsAndSets()
540 std::make_pair(ToUnicodeCodepoint("\xE2\x80\x92" /* "‒" */), '-')); in InitializeMapsAndSets()
542 std::make_pair(ToUnicodeCodepoint("\xE2\x80\x93" /* "–" */), '-')); in InitializeMapsAndSets()
544 std::make_pair(ToUnicodeCodepoint("\xE2\x80\x94" /* "—" */), '-')); in InitializeMapsAndSets()
546 std::make_pair(ToUnicodeCodepoint("\xE2\x80\x95" /* "―" */), '-')); in InitializeMapsAndSets()
548 std::make_pair(ToUnicodeCodepoint("\xE2\x88\x92" /* "−" */), '-')); in InitializeMapsAndSets()
563 // Only the upper-case letters are added here - the lower-case versions are in InitializeMapsAndSets()
596 // Convert all the upper-case ASCII letters to lower-case. in InitializeMapsAndSets()
597 if (it->first < 128) { in InitializeMapsAndSets()
598 char letter_as_upper = static_cast<char>(it->first); in InitializeMapsAndSets()
600 lower_case_mappings.insert(std::make_pair(letter_as_lower, it->second)); in InitializeMapsAndSets()
602 // pairs each letter with its upper-case representation so that it can in InitializeMapsAndSets()
605 alpha_letters.insert(std::make_pair(it->first, letter_as_upper)); in InitializeMapsAndSets()
608 // In the Java version we don't insert the lower-case mappings in the map, in InitializeMapsAndSets()
637 // Regular expression of viable phone numbers. This is location independent.
639 // alpha characters and digits in the phone number. Does not include extension
640 // data. The symbol 'x' is allowed here as valid punctuation since it is often
642 // numbers. We also allow multiple plus-signs at the start.
648 // The first reg-ex is to allow short numbers (two digits long) to be parsed
649 // if they are entered as "15" etc, but only if there is no punctuation in
650 // them. The second expression restricts the number of digits to three or
652 // alpha-characters and punctuation.
656 // will be run as a case-insensitive regexp match. Wide character versions are
663 // Regular expressions of different parts of the phone-context parameter,
675 // any of the characters in this map must not be removed from a number when
686 // numbers. This includes digits, ascii letters and number grouping symbols
687 // such as "-" and " ".
691 // One example of when this is relevant is when determining the length of the
699 // that are geographically assigned: this carrier indicator is not considered
721 // Regular expression of acceptable characters that may start a phone number
724 // of digits, the plus symbol and arabic-indic digits. This does not contain
725 // alpha characters, although they may be used later in the number. It also
727 // parsing and is of no information value when parsing a number. The string
728 // starting with this valid character is captured.
733 // a second number.
738 // character is retained here, as it may signify the previous block was an
740 // number if this was a match.
748 // numbers in text. This will be run as a case-insensitive regexp match. Wide
757 // phone number may have an extension prefix appended, followed by 1 or more
761 // We use this pattern to check if the phone number has at least three letters
762 // in it - if so, then we treat it as a number where some phone-number digits
772 // Regular expression of valid global-number-digits for the phone-context
776 // Regular expression of valid domainname for the phone-context parameter,
792 StrCat("[", alphanum_, "]+((\\-)*[", alphanum_, "])*")), in PhoneNumberRegExpsAndMappings()
794 "]+((\\-)*[", alphanum_, "])*")), in PhoneNumberRegExpsAndMappings()
804 single_international_prefix_(regexp_factory_->CreateRegExp( in PhoneNumberRegExpsAndMappings()
808 regexp_factory_->CreateRegExp(StrCat("[", kDigits, "]*"))), in PhoneNumberRegExpsAndMappings()
810 regexp_factory_->CreateRegExp(StrCat("([", kDigits, "])"))), in PhoneNumberRegExpsAndMappings()
812 regexp_factory_->CreateRegExp("(\\d+)")), in PhoneNumberRegExpsAndMappings()
813 valid_start_char_pattern_(regexp_factory_->CreateRegExp( in PhoneNumberRegExpsAndMappings()
816 regexp_factory_->CreateRegExp( in PhoneNumberRegExpsAndMappings()
819 regexp_factory_->CreateRegExp("[^\\p{N}\\p{L}#]")), in PhoneNumberRegExpsAndMappings()
820 separator_pattern_(regexp_factory_->CreateRegExp( in PhoneNumberRegExpsAndMappings()
824 extn_pattern_(regexp_factory_->CreateRegExp( in PhoneNumberRegExpsAndMappings()
826 valid_phone_number_pattern_(regexp_factory_->CreateRegExp( in PhoneNumberRegExpsAndMappings()
829 valid_alpha_phone_pattern_(regexp_factory_->CreateRegExp( in PhoneNumberRegExpsAndMappings()
832 // are some countries for which the first group is not used in the in PhoneNumberRegExpsAndMappings()
837 regexp_factory_->CreateRegExp("(\\$\\d)")), in PhoneNumberRegExpsAndMappings()
838 carrier_code_pattern_(regexp_factory_->CreateRegExp("\\$CC")), in PhoneNumberRegExpsAndMappings()
839 plus_chars_pattern_(regexp_factory_->CreateRegExp( in PhoneNumberRegExpsAndMappings()
841 rfc3966_global_number_digits_pattern_(regexp_factory_->CreateRegExp( in PhoneNumberRegExpsAndMappings()
844 rfc3966_domainname_pattern_(regexp_factory_->CreateRegExp(StrCat( in PhoneNumberRegExpsAndMappings()
849 // This type is neither copyable nor movable.
871 LOG(DFATAL) << "Could not parse compiled-in metadata."; in PhoneNumberUtil()
881 const string& region_code = it->id(); in PhoneNumberUtil()
886 int country_calling_code = it->country_code(); in PhoneNumberUtil()
888 … country_code_to_non_geographical_metadata_map_->insert(std::make_pair(country_calling_code, *it)); in PhoneNumberUtil()
890 region_to_metadata_map_->insert(std::make_pair(region_code, *it)); in PhoneNumberUtil()
895 if (it->main_country_for_code()) { in PhoneNumberUtil()
896 calling_code_in_map->second->push_front(region_code); in PhoneNumberUtil()
898 calling_code_in_map->second->push_back(region_code); in PhoneNumberUtil()
903 list_with_region_code->push_back(region_code); in PhoneNumberUtil()
908 nanpa_regions_->insert(region_code); in PhoneNumberUtil()
912 country_calling_code_to_region_code_map_->insert( in PhoneNumberUtil()
913 country_calling_code_to_region_code_map_->begin(), in PhoneNumberUtil()
917 std::sort(country_calling_code_to_region_code_map_->begin(), in PhoneNumberUtil()
918 country_calling_code_to_region_code_map_->end(), OrderByFirst()); in PhoneNumberUtil()
928 country_calling_code_to_region_code_map_->begin(), in ~PhoneNumberUtil()
929 country_calling_code_to_region_code_map_->end()); in ~PhoneNumberUtil()
936 region_to_metadata_map_->begin(); it != region_to_metadata_map_->end(); in GetSupportedRegions()
938 regions->insert(it->first); in GetSupportedRegions()
946 country_code_to_non_geographical_metadata_map_->begin(); in GetSupportedGlobalNetworkCallingCodes()
947 it != country_code_to_non_geographical_metadata_map_->end(); ++it) { in GetSupportedGlobalNetworkCallingCodes()
948 calling_codes->insert(it->first); in GetSupportedGlobalNetworkCallingCodes()
956 country_calling_code_to_region_code_map_->begin(); in GetSupportedCallingCodes()
957 it != country_calling_code_to_region_code_map_->end(); ++it) { in GetSupportedCallingCodes()
958 calling_codes->insert(it->first); in GetSupportedCallingCodes()
981 LOG(WARNING) << "Unknown country calling code for a non-geographical " in GetSupportedTypesForNonGeoEntity()
997 return reg_exps_->extn_patterns_for_matching_; in GetExtnPatternsForMatching()
1000 bool PhoneNumberUtil::StartsWithPlusCharsPattern(const string& number) in StartsWithPlusCharsPattern()
1003 reg_exps_->regexp_factory_->CreateInput(number)); in StartsWithPlusCharsPattern()
1004 return reg_exps_->plus_chars_pattern_->Consume(number_string_piece.get()); in StartsWithPlusCharsPattern()
1008 return reg_exps_->digits_pattern_->FullMatch(s); in ContainsOnlyValidDigits()
1011 void PhoneNumberUtil::TrimUnwantedEndChars(string* number) const { in TrimUnwantedEndChars()
1012 DCHECK(number); in TrimUnwantedEndChars()
1014 number_as_unicode.PointToUTF8(number->data(), static_cast<int>(number->size())); in TrimUnwantedEndChars()
1016 // The input wasn't valid UTF-8. Produce an empty string to indicate an error. in TrimUnwantedEndChars()
1017 number->clear(); in TrimUnwantedEndChars()
1026 if (!reg_exps_->unwanted_end_char_pattern_->FullMatch(current_char)) { in TrimUnwantedEndChars()
1031 number->assign(UnicodeText::UTF8Substring(number_as_unicode.begin(), in TrimUnwantedEndChars()
1037 // A pattern that is used to determine if a numberFormat under in IsFormatEligibleForAsYouTypeFormatter()
1038 // availableFormats is eligible to be used by the AYTF. It is eligible when in IsFormatEligibleForAsYouTypeFormatter()
1040 // followed by a single digit, separated by valid phone number punctuation. in IsFormatEligibleForAsYouTypeFormatter()
1043 // group is present in the output pattern to ensure no data is lost while in IsFormatEligibleForAsYouTypeFormatter()
1045 const RegExp& eligible_format_pattern = reg_exps_->regexp_cache_->GetRegExp( in IsFormatEligibleForAsYouTypeFormatter()
1054 // A pattern that is used to determine if the national prefix formatting rule in FormattingRuleHasFirstGroupOnly()
1058 reg_exps_->regexp_cache_->GetRegExp("\\(?\\$1\\)?"); in FormattingRuleHasFirstGroupOnly()
1074 national_prefix->assign(metadata->national_prefix()); in GetNddPrefixForRegion()
1076 // Note: if any other non-numeric symbols are ever used in national in GetNddPrefixForRegion()
1083 return (region_to_metadata_map_->find(region_code) != in IsValidRegionCode()
1084 region_to_metadata_map_->end()); in IsValidRegionCode()
1093 return (std::binary_search(country_calling_code_to_region_code_map_->begin(), in HasValidCountryCallingCode()
1094 country_calling_code_to_region_code_map_->end(), in HasValidCountryCallingCode()
1099 // if the region code is invalid or unknown.
1103 region_to_metadata_map_->find(region_code); in GetMetadataForRegion()
1104 if (it != region_to_metadata_map_->end()) { in GetMetadataForRegion()
1105 return &it->second; in GetMetadataForRegion()
1113 country_code_to_non_geographical_metadata_map_->find( in GetMetadataForNonGeographicalRegion()
1115 if (it != country_code_to_non_geographical_metadata_map_->end()) { in GetMetadataForNonGeographicalRegion()
1116 return &it->second; in GetMetadataForNonGeographicalRegion()
1121 void PhoneNumberUtil::Format(const PhoneNumber& number, in Format() argument
1125 if (number.national_number() == 0) { in Format()
1126 const string& raw_input = number.raw_input(); in Format()
1129 // This is the only case where a number can be formatted as E164 without a in Format()
1130 // leading '+' symbol (but the original number wasn't parseable anyway). in Format()
1133 formatted_number->assign(raw_input); in Format()
1137 int country_calling_code = number.country_code(); in Format()
1139 GetNationalSignificantNumber(number, &national_significant_number); in Format()
1141 // Early exit for E164 case (even if the country calling code is invalid) in Format()
1142 // since no formatting of the national number needs to be applied. in Format()
1144 formatted_number->assign(national_significant_number); in Format()
1150 formatted_number->assign(national_significant_number); in Format()
1154 // that to format NANPA numbers. The same applies to Russian Fed regions - in Format()
1159 // Metadata cannot be NULL because the country calling code is valid (which in Format()
1166 MaybeAppendFormattedExtension(number, *metadata, number_format, in Format()
1173 const PhoneNumber& number, in FormatByPattern() argument
1178 int country_calling_code = number.country_code(); in FormatByPattern()
1179 // Note GetRegionCodeForCountryCode() is used because formatting information in FormatByPattern()
1180 // for regions which share a country calling code is contained by only one in FormatByPattern()
1184 GetNationalSignificantNumber(number, &national_significant_number); in FormatByPattern()
1186 formatted_number->assign(national_significant_number); in FormatByPattern()
1191 // Metadata cannot be NULL because the country calling code is valid. in FormatByPattern()
1198 // If no pattern above is matched, we format the number as a whole. in FormatByPattern()
1199 formatted_number->assign(national_significant_number); in FormatByPattern()
1207 formatting_pattern->national_prefix_formatting_rule()); in FormatByPattern()
1209 const string& national_prefix = metadata->national_prefix(); in FormatByPattern()
1226 MaybeAppendFormattedExtension(number, *metadata, NATIONAL, formatted_number); in FormatByPattern()
1232 const PhoneNumber& number, in FormatNationalNumberWithCarrierCode() argument
1235 int country_calling_code = number.country_code(); in FormatNationalNumberWithCarrierCode()
1237 GetNationalSignificantNumber(number, &national_significant_number); in FormatNationalNumberWithCarrierCode()
1239 formatted_number->assign(national_significant_number); in FormatNationalNumberWithCarrierCode()
1243 // Note GetRegionCodeForCountryCode() is used because formatting information in FormatNationalNumberWithCarrierCode()
1244 // for regions which share a country calling code is contained by only one in FormatNationalNumberWithCarrierCode()
1249 // Metadata cannot be NULL because the country calling code is valid. in FormatNationalNumberWithCarrierCode()
1254 MaybeAppendFormattedExtension(number, *metadata, NATIONAL, formatted_number); in FormatNationalNumberWithCarrierCode()
1267 const PhoneNumber& number, in FormatNationalNumberWithPreferredCarrierCode() argument
1271 number, in FormatNationalNumberWithPreferredCarrierCode()
1274 // result in a number we can dial. For this reason, we treat the empty in FormatNationalNumberWithPreferredCarrierCode()
1276 !number.preferred_domestic_carrier_code().empty() in FormatNationalNumberWithPreferredCarrierCode()
1277 ? number.preferred_domestic_carrier_code() in FormatNationalNumberWithPreferredCarrierCode()
1283 const PhoneNumber& number, in FormatNumberForMobileDialing() argument
1287 int country_calling_code = number.country_code(); in FormatNumberForMobileDialing()
1289 formatted_number->assign(number.has_raw_input() ? number.raw_input() : ""); in FormatNumberForMobileDialing()
1293 formatted_number->assign(""); in FormatNumberForMobileDialing()
1295 // the main number. in FormatNumberForMobileDialing()
1296 PhoneNumber number_no_extension(number); in FormatNumberForMobileDialing()
1310 // result in a number we can dial. For this reason, we treat the empty in FormatNumberForMobileDialing()
1320 formatted_number->assign(""); in FormatNumberForMobileDialing()
1337 // For non-geographical countries, and Mexican, Chilean and Uzbek fixed in FormatNumberForMobileDialing()
1345 // local area. It is trickier to get that to work correctly than in FormatNumberForMobileDialing()
1346 // using international format, which is tested to work fine on all in FormatNumberForMobileDialing()
1350 // reverse is true for mobile numbers. As a result, we output them in in FormatNumberForMobileDialing()
1352 // UZ mobile and fixed-line numbers have to be formatted in in FormatNumberForMobileDialing()
1354 // (for fixed-line) and 05 (for mobile) for dialling successfully in FormatNumberForMobileDialing()
1357 // phone types we return the number in international format here. in FormatNumberForMobileDialing()
1371 // region, so if a number is not a valid regular length phone number, we in FormatNumberForMobileDialing()
1384 const PhoneNumber& number, in FormatOutOfCountryCallingNumber() argument
1389 VLOG(1) << "Trying to format number from invalid region " << calling_from in FormatOutOfCountryCallingNumber()
1391 Format(number, INTERNATIONAL, formatted_number); in FormatOutOfCountryCallingNumber()
1394 int country_code = number.country_code(); in FormatOutOfCountryCallingNumber()
1396 GetNationalSignificantNumber(number, &national_significant_number); in FormatOutOfCountryCallingNumber()
1398 formatted_number->assign(national_significant_number); in FormatOutOfCountryCallingNumber()
1405 Format(number, NATIONAL, formatted_number); in FormatOutOfCountryCallingNumber()
1406 formatted_number->insert(0, StrCat(country_code, " ")); in FormatOutOfCountryCallingNumber()
1410 // If neither region is a NANPA region, then we check to see if the in FormatOutOfCountryCallingNumber()
1411 // country calling code of the number and the country calling code of the in FormatOutOfCountryCallingNumber()
1416 // Technically this is the case for dialling from la Réunion to other in FormatOutOfCountryCallingNumber()
1418 // but not vice versa - so we don't cover this edge case for now and for in FormatOutOfCountryCallingNumber()
1421 // http://www.petitfute.com/voyage/225-info-pratiques-reunion in FormatOutOfCountryCallingNumber()
1422 Format(number, NATIONAL, formatted_number); in FormatOutOfCountryCallingNumber()
1429 metadata_calling_from->international_prefix(); in FormatOutOfCountryCallingNumber()
1431 // In general, if there is a preferred international prefix, use that. in FormatOutOfCountryCallingNumber()
1433 // international format of the number is returned since we would not know in FormatOutOfCountryCallingNumber()
1436 if (metadata_calling_from->has_preferred_international_prefix()) { in FormatOutOfCountryCallingNumber()
1438 metadata_calling_from->preferred_international_prefix(); in FormatOutOfCountryCallingNumber()
1439 } else if (reg_exps_->single_international_prefix_->FullMatch( in FormatOutOfCountryCallingNumber()
1446 // Metadata cannot be NULL because the country_code is valid. in FormatOutOfCountryCallingNumber()
1451 MaybeAppendFormattedExtension(number, *metadata_for_region, INTERNATIONAL, in FormatOutOfCountryCallingNumber()
1454 formatted_number->insert( in FormatOutOfCountryCallingNumber()
1462 void PhoneNumberUtil::FormatInOriginalFormat(const PhoneNumber& number, in FormatInOriginalFormat() argument
1467 if (number.has_raw_input() && !HasFormattingPatternForNumber(number)) { in FormatInOriginalFormat()
1469 // format the number as a group without national prefix. in FormatInOriginalFormat()
1470 formatted_number->assign(number.raw_input()); in FormatInOriginalFormat()
1473 if (!number.has_country_code_source()) { in FormatInOriginalFormat()
1474 Format(number, NATIONAL, formatted_number); in FormatInOriginalFormat()
1477 switch (number.country_code_source()) { in FormatInOriginalFormat()
1479 Format(number, INTERNATIONAL, formatted_number); in FormatInOriginalFormat()
1482 FormatOutOfCountryCallingNumber(number, region_calling_from, in FormatInOriginalFormat()
1486 Format(number, INTERNATIONAL, formatted_number); in FormatInOriginalFormat()
1487 formatted_number->erase(formatted_number->begin()); in FormatInOriginalFormat()
1490 // Fall-through to default case. in FormatInOriginalFormat()
1493 GetRegionCodeForCountryCode(number.country_code(), ®ion_code); in FormatInOriginalFormat()
1494 // We strip non-digits from the NDD here, and from the raw input later, so in FormatInOriginalFormat()
1497 GetNddPrefixForRegion(region_code, true /* strip non-digits */, in FormatInOriginalFormat()
1503 Format(number, NATIONAL, formatted_number); in FormatInOriginalFormat()
1506 // Otherwise, we check if the original number was entered with a national in FormatInOriginalFormat()
1508 if (RawInputContainsNationalPrefix(number.raw_input(), national_prefix, in FormatInOriginalFormat()
1511 Format(number, NATIONAL, formatted_number); in FormatInOriginalFormat()
1515 // leaves the prefix empty if there is no metadata for the region. in FormatInOriginalFormat()
1518 GetNationalSignificantNumber(number, &national_number); in FormatInOriginalFormat()
1522 ChooseFormattingPatternForNumber(metadata->number_format(), in FormatInOriginalFormat()
1524 // The format rule could still be NULL here if the national number was 0 in FormatInOriginalFormat()
1529 Format(number, NATIONAL, formatted_number); in FormatInOriginalFormat()
1532 // When the format we apply to this number doesn't contain national in FormatInOriginalFormat()
1537 format_rule->national_prefix_formatting_rule()); in FormatInOriginalFormat()
1538 // We assume that the first-group symbol will never be _before_ the in FormatInOriginalFormat()
1545 Format(number, NATIONAL, formatted_number); in FormatInOriginalFormat()
1552 // National prefix not used when formatting this number. in FormatInOriginalFormat()
1553 Format(number, NATIONAL, formatted_number); in FormatInOriginalFormat()
1559 number_format->MergeFrom(*format_rule); in FormatInOriginalFormat()
1560 number_format->clear_national_prefix_formatting_rule(); in FormatInOriginalFormat()
1561 FormatByPattern(number, NATIONAL, number_formats, formatted_number); in FormatInOriginalFormat()
1564 // If no digit is inserted/removed/modified as a result of our formatting, we in FormatInOriginalFormat()
1565 // return the formatted phone number; otherwise we return the raw input the in FormatInOriginalFormat()
1567 if (!formatted_number->empty() && !number.raw_input().empty()) { in FormatInOriginalFormat()
1570 string normalized_raw_input(number.raw_input()); in FormatInOriginalFormat()
1573 formatted_number->assign(number.raw_input()); in FormatInOriginalFormat()
1578 // Check if raw_input, which is assumed to be in the national format, has a
1579 // national prefix. The national prefix is assumed to be in digits-only form.
1590 // number if the assumed national prefix is removed (777123 won't be in RawInputContainsNationalPrefix()
1603 const PhoneNumber& number) const { in HasFormattingPatternForNumber()
1604 int country_calling_code = number.country_code(); in HasFormattingPatternForNumber()
1613 GetNationalSignificantNumber(number, &national_number); in HasFormattingPatternForNumber()
1615 ChooseFormattingPatternForNumber(metadata->number_format(), in HasFormattingPatternForNumber()
1621 const PhoneNumber& number, in FormatOutOfCountryKeepingAlphaChars() argument
1624 // If there is no raw input, then we can't keep alpha characters because there in FormatOutOfCountryKeepingAlphaChars()
1626 if (number.raw_input().empty()) { in FormatOutOfCountryKeepingAlphaChars()
1627 FormatOutOfCountryCallingNumber(number, calling_from, formatted_number); in FormatOutOfCountryKeepingAlphaChars()
1630 int country_code = number.country_code(); in FormatOutOfCountryKeepingAlphaChars()
1632 formatted_number->assign(number.raw_input()); in FormatOutOfCountryKeepingAlphaChars()
1636 // this by comparing the number in raw_input with the parsed number. in FormatOutOfCountryKeepingAlphaChars()
1637 string raw_input_copy(number.raw_input()); in FormatOutOfCountryKeepingAlphaChars()
1638 // Normalize punctuation. We retain number grouping symbols such as " " only. in FormatOutOfCountryKeepingAlphaChars()
1639 NormalizeHelper(reg_exps_->all_plus_number_grouping_symbols_, true, in FormatOutOfCountryKeepingAlphaChars()
1641 // Now we trim everything before the first three digits in the parsed number. in FormatOutOfCountryKeepingAlphaChars()
1643 // - if it does not, then we don't trim anything at all. Similarly, if the in FormatOutOfCountryKeepingAlphaChars()
1644 // national number was less than three digits, we don't trim anything at all. in FormatOutOfCountryKeepingAlphaChars()
1646 GetNationalSignificantNumber(number, &national_number); in FormatOutOfCountryKeepingAlphaChars()
1663 ChooseFormattingPatternForNumber(metadata->number_format(), in FormatOutOfCountryKeepingAlphaChars()
1666 // If no pattern above is matched, we format the original input. in FormatOutOfCountryKeepingAlphaChars()
1667 formatted_number->assign(raw_input_copy); in FormatOutOfCountryKeepingAlphaChars()
1672 // The first group is the first group of digits that the user wrote in FormatOutOfCountryKeepingAlphaChars()
1682 // we have overridden the pattern to match anything, but that is not the in FormatOutOfCountryKeepingAlphaChars()
1690 // If an unsupported region-calling-from is entered, or a country with in FormatOutOfCountryKeepingAlphaChars()
1691 // multiple international prefixes, the international format of the number is in FormatOutOfCountryKeepingAlphaChars()
1692 // returned, unless there is a preferred international prefix. in FormatOutOfCountryKeepingAlphaChars()
1694 const string& international_prefix = metadata->international_prefix(); in FormatOutOfCountryKeepingAlphaChars()
1696 reg_exps_->single_international_prefix_->FullMatch(international_prefix) in FormatOutOfCountryKeepingAlphaChars()
1698 : metadata->preferred_international_prefix(); in FormatOutOfCountryKeepingAlphaChars()
1704 // Invalid region entered as country-calling-from (so no metadata was found in FormatOutOfCountryKeepingAlphaChars()
1708 VLOG(1) << "Trying to format number from invalid region " << calling_from in FormatOutOfCountryKeepingAlphaChars()
1711 formatted_number->assign(raw_input_copy); in FormatOutOfCountryKeepingAlphaChars()
1722 int size = it->leading_digits_pattern_size(); in ChooseFormattingPatternForNumber()
1725 reg_exps_->regexp_factory_->CreateInput(national_number)); in ChooseFormattingPatternForNumber()
1726 // We always use the last leading_digits_pattern, as it is the most in ChooseFormattingPatternForNumber()
1728 if (!reg_exps_->regexp_cache_->GetRegExp( in ChooseFormattingPatternForNumber()
1729 it->leading_digits_pattern(size - 1)).Consume( in ChooseFormattingPatternForNumber()
1735 reg_exps_->regexp_cache_->GetRegExp(it->pattern())); in ChooseFormattingPatternForNumber()
1743 // Note that carrier_code is optional - if an empty string, no carrier code
1759 reg_exps_->carrier_code_pattern_->Replace(&carrier_code_formatting_rule, in FormatNsnUsingPatternWithCarrier()
1761 reg_exps_->first_group_capturing_pattern_-> in FormatNsnUsingPatternWithCarrier()
1770 // contains only information on how the national significant number in FormatNsnUsingPatternWithCarrier()
1772 reg_exps_->first_group_capturing_pattern_->Replace( in FormatNsnUsingPatternWithCarrier()
1776 formatted_number->assign(national_number); in FormatNsnUsingPatternWithCarrier()
1779 reg_exps_->regexp_cache_->GetRegExp(formatting_pattern.pattern())); in FormatNsnUsingPatternWithCarrier()
1784 const scoped_ptr<RegExpInput> number( in FormatNsnUsingPatternWithCarrier() local
1785 reg_exps_->regexp_factory_->CreateInput(*formatted_number)); in FormatNsnUsingPatternWithCarrier()
1786 if (reg_exps_->separator_pattern_->Consume(number.get())) { in FormatNsnUsingPatternWithCarrier()
1787 formatted_number->assign(number->ToString()); in FormatNsnUsingPatternWithCarrier()
1789 // Then replace all separators with a "-". in FormatNsnUsingPatternWithCarrier()
1790 reg_exps_->separator_pattern_->GlobalReplace(formatted_number, "-"); in FormatNsnUsingPatternWithCarrier()
1806 void PhoneNumberUtil::FormatNsn(const string& number, in FormatNsn() argument
1811 FormatNsnWithCarrier(number, metadata, number_format, "", formatted_number); in FormatNsn()
1814 // Note in some regions, the national number can be written in two completely
1816 // INTERNATIONAL format. The number_format parameter here is used to specify
1817 // which format to use for those cases. If a carrier_code is specified, this
1819 void PhoneNumberUtil::FormatNsnWithCarrier(const string& number, in FormatNsnWithCarrier() argument
1825 // When the intl_number_formats exists, we use that to format national number in FormatNsnWithCarrier()
1832 ChooseFormattingPatternForNumber(available_formats, number); in FormatNsnWithCarrier()
1834 formatted_number->assign(number); in FormatNsnWithCarrier()
1836 FormatNsnUsingPatternWithCarrier(number, *formatting_pattern, number_format, in FormatNsnWithCarrier()
1841 // Appends the formatted extension of a phone number, if the phone number had an
1844 const PhoneNumber& number, in MaybeAppendFormattedExtension() argument
1849 if (number.has_extension() && number.extension().length() > 0) { in MaybeAppendFormattedExtension()
1851 StrAppend(formatted_number, kRfc3966ExtnPrefix, number.extension()); in MaybeAppendFormattedExtension()
1855 number.extension()); in MaybeAppendFormattedExtension()
1857 StrAppend(formatted_number, kDefaultExtnPrefix, number.extension()); in MaybeAppendFormattedExtension()
1864 return nanpa_regions_->find(region_code) != nanpa_regions_->end(); in IsNANPACountry()
1879 std::equal_range(country_calling_code_to_region_code_map_->begin(), in GetRegionCodesForCountryCallingCode()
1880 country_calling_code_to_region_code_map_->end(), in GetRegionCodesForCountryCallingCode()
1883 region_codes->insert(region_codes->begin(), in GetRegionCodesForCountryCallingCode()
1884 range.first->second->begin(), in GetRegionCodesForCountryCallingCode()
1885 range.first->second->end()); in GetRegionCodesForCountryCallingCode()
1903 void PhoneNumberUtil::GetRegionCodeForNumber(const PhoneNumber& number, in GetRegionCodeForNumber() argument
1906 int country_calling_code = number.country_code(); in GetRegionCodeForNumber()
1918 GetRegionCodeForNumberFromRegionList(number, region_codes, region_code); in GetRegionCodeForNumber()
1923 const PhoneNumber& number, const std::list<string>& region_codes, in GetRegionCodeForNumberFromRegionList() argument
1927 GetNationalSignificantNumber(number, &national_number); in GetRegionCodeForNumberFromRegionList()
1933 if (metadata->has_leading_digits()) { in GetRegionCodeForNumberFromRegionList()
1934 const scoped_ptr<RegExpInput> number( in GetRegionCodeForNumberFromRegionList() local
1935 reg_exps_->regexp_factory_->CreateInput(national_number)); in GetRegionCodeForNumberFromRegionList()
1936 if (reg_exps_->regexp_cache_-> in GetRegionCodeForNumberFromRegionList()
1937 GetRegExp(metadata->leading_digits()).Consume(number.get())) { in GetRegionCodeForNumberFromRegionList()
1961 return metadata->country_code(); in GetCountryCodeForValidRegion()
1964 // Gets a valid fixed-line number for the specified region_code. Returns false
1965 // if the region was unknown or 001 (representing non-geographical regions), or
1966 // if no number exists.
1968 PhoneNumber* number) const { in GetExampleNumber()
1969 DCHECK(number); in GetExampleNumber()
1970 return GetExampleNumberForType(region_code, FIXED_LINE, number); in GetExampleNumber()
1974 PhoneNumber* number) const { in GetInvalidExampleNumber()
1975 DCHECK(number); in GetInvalidExampleNumber()
1981 // We start off with a valid fixed-line number since every country supports in GetInvalidExampleNumber()
1982 // this. Alternatively we could start with a different number type, since in GetInvalidExampleNumber()
1983 // fixed-line numbers typically have a wide breadth of valid number lengths in GetInvalidExampleNumber()
1984 // and we may have to make it very short before we get an invalid number. in GetInvalidExampleNumber()
1988 if (!desc->has_example_number()) { in GetInvalidExampleNumber()
1989 // This shouldn't happen - we have a test for this. in GetInvalidExampleNumber()
1992 const string& example_number = desc->example_number(); in GetInvalidExampleNumber()
1993 // Try and make the number invalid. We do this by changing the length. We try in GetInvalidExampleNumber()
1994 // reducing the length of the number, since currently no region has a number in GetInvalidExampleNumber()
1995 // that is the same length as kMinLengthForNsn. This is probably quicker than in GetInvalidExampleNumber()
1996 // making the number longer, which is another alternative. We could also use in GetInvalidExampleNumber()
1997 // the possible number pattern to extract the possible lengths of the number in GetInvalidExampleNumber()
1998 // to make this faster, but this method is only for unit-testing so simplicity in GetInvalidExampleNumber()
1999 // is preferred to performance. in GetInvalidExampleNumber()
2000 // We don't want to return a number that can't be parsed, so we check the in GetInvalidExampleNumber()
2001 // number is long enough. We try all possible lengths because phone number in GetInvalidExampleNumber()
2002 // plans often have overlapping prefixes so the number 123456 might be valid in GetInvalidExampleNumber()
2003 // as a fixed-line number, and 12345 as a mobile number. It would be faster to in GetInvalidExampleNumber()
2006 // phone numbers - otherwise they would all be kMinLengthForNsn digits long.) in GetInvalidExampleNumber()
2007 for (size_t phone_number_length = example_number.length() - 1; in GetInvalidExampleNumber()
2009 phone_number_length--) { in GetInvalidExampleNumber()
2015 // region code is fine. in GetInvalidExampleNumber()
2017 number->MergeFrom(possibly_valid_number); in GetInvalidExampleNumber()
2026 // Gets a valid number for the specified region_code and type. Returns false if
2027 // the country was unknown or 001 (representing non-geographical regions), or if
2028 // no number exists.
2032 PhoneNumber* number) const { in GetExampleNumberForType()
2033 DCHECK(number); in GetExampleNumberForType()
2041 if (desc && desc->has_example_number()) { in GetExampleNumberForType()
2042 ErrorType success = Parse(desc->example_number(), region_code, number); in GetExampleNumberForType()
2046 LOG(ERROR) << "Error parsing example number (" in GetExampleNumberForType()
2055 PhoneNumber* number) const { in GetExampleNumberForType()
2056 DCHECK(number); in GetExampleNumberForType()
2060 if (GetExampleNumberForType(region_code, type, number)) { in GetExampleNumberForType()
2064 // If there wasn't an example number for a region, try the non-geographical in GetExampleNumberForType()
2074 if (desc->has_example_number()) { in GetExampleNumberForType()
2077 desc->example_number()), in GetExampleNumberForType()
2078 RegionCode::GetUnknown(), number); in GetExampleNumberForType()
2082 LOG(ERROR) << "Error parsing example number (" in GetExampleNumberForType()
2092 int country_calling_code, PhoneNumber* number) const { in GetExampleNumberForNonGeoEntity()
2093 DCHECK(number); in GetExampleNumberForNonGeoEntity()
2097 // For geographical entities, fixed-line data is always present. However, in GetExampleNumberForNonGeoEntity()
2098 // for non-geographical entities, this is not the case, so we have to go in GetExampleNumberForNonGeoEntity()
2099 // through different types to find the example number. We don't check in GetExampleNumberForNonGeoEntity()
2100 // fixed-line or personal number since they aren't used by non-geographical in GetExampleNumberForNonGeoEntity()
2101 // entities (if this changes, a unit-test will catch this.) in GetExampleNumberForNonGeoEntity()
2104 metadata->mobile(), metadata->toll_free(), metadata->shared_cost(), in GetExampleNumberForNonGeoEntity()
2105 metadata->voip(), metadata->voicemail(), metadata->uan(), in GetExampleNumberForNonGeoEntity()
2106 metadata->premium_rate()}; in GetExampleNumberForNonGeoEntity()
2112 RegionCode::GetUnknown(), number); in GetExampleNumberForNonGeoEntity()
2116 LOG(ERROR) << "Error parsing example number (" in GetExampleNumberForNonGeoEntity()
2130 PhoneNumber* number) const { in Parse()
2131 DCHECK(number); in Parse()
2132 return ParseHelper(number_to_parse, default_region, false, true, number); in Parse()
2138 PhoneNumber* number) const { in ParseAndKeepRawInput()
2139 DCHECK(number); in ParseAndKeepRawInput()
2140 return ParseHelper(number_to_parse, default_region, true, true, number); in ParseAndKeepRawInput()
2143 // Checks to see that the region code used is valid, or if it is not valid, that
2144 // the number to parse starts with a + symbol so that we can attempt to infer
2145 // the country from the number. Returns false if it cannot use the region
2151 const scoped_ptr<RegExpInput> number( in CheckRegionForParsing() local
2152 reg_exps_->regexp_factory_->CreateInput(number_to_parse)); in CheckRegionForParsing()
2153 if (!reg_exps_->plus_chars_pattern_->Consume(number.get())) { in CheckRegionForParsing()
2161 // national_number if it is written in RFC3966; otherwise extract a possible
2162 // number out of it and write to national_number.
2169 // If the phone context contains a phone number prefix, we need to capture in BuildNationalNumberForParsing()
2171 if (phone_context_start < (number_to_parse.length() - 1) && in BuildNationalNumberForParsing()
2175 // important for parsing the phone number. in BuildNationalNumberForParsing()
2180 phone_context_start, phone_context_end - phone_context_start)); in BuildNationalNumberForParsing()
2186 // Now append everything between the "tel:" prefix and the phone-context. in BuildNationalNumberForParsing()
2187 // This should include the national number, an optional extension or in BuildNationalNumberForParsing()
2188 // isdn-subaddress component. Note we also handle the case when "tel:" is in BuildNationalNumberForParsing()
2189 // missing, as we have seen in some of the phone number inputs. In that in BuildNationalNumberForParsing()
2198 index_of_phone_context - index_of_national_number)); in BuildNationalNumberForParsing()
2200 // Extract a possible number from the string passed in (this strips leading in BuildNationalNumberForParsing()
2201 // characters that could not be the start of a phone number.) in BuildNationalNumberForParsing()
2205 // Delete the isdn-subaddress and everything after it if it is present. Note in BuildNationalNumberForParsing()
2206 // extension won't appear at the same time with isdn-subaddress according to in BuildNationalNumberForParsing()
2208 size_t index_of_isdn = national_number->find(kRfc3966IsdnSubaddress); in BuildNationalNumberForParsing()
2210 national_number->erase(index_of_isdn); in BuildNationalNumberForParsing()
2212 // If both phone context and isdn-subaddress are absent but other parameters in BuildNationalNumberForParsing()
2213 // are present, the parameters are left in nationalNumber. This is because in BuildNationalNumberForParsing()
2214 // we are concerned about deleting content from a potential number string in BuildNationalNumberForParsing()
2215 // when there is no strong evidence that the number is actually written in in BuildNationalNumberForParsing()
2219 // Note if any new field is added to this method that should always be filled
2220 // in, even when keepRawInput is false, it should also be handled in the
2234 VLOG(2) << "The string supplied did not seem to be a phone number."; in ParseHelper()
2247 // Attempt to parse extension first, since it doesn't require country-specific in ParseHelper()
2248 // data and we want to have the non-normalised number here. in ParseHelper()
2255 // Check to see if the number is given in international format so we know in ParseHelper()
2256 // whether this number is from the default country or not. in ParseHelper()
2263 reg_exps_->regexp_factory_->CreateInput(national_number)); in ParseHelper()
2265 (reg_exps_->plus_chars_pattern_->Consume(number_string_piece.get()))) { in ParseHelper()
2266 normalized_national_number.assign(number_string_piece->ToString()); in ParseHelper()
2267 // Strip the plus-char, and try again. in ParseHelper()
2289 // Note that the national number was already normalized by in ParseHelper()
2291 country_code = country_metadata->country_code(); in ParseHelper()
2294 VLOG(2) << "The string supplied is too short to be a phone number."; in ParseHelper()
2305 // Otherwise, we don't do the stripping, since the original number could be in ParseHelper()
2306 // a valid short number. in ParseHelper()
2321 VLOG(2) << "The string supplied is too short to be a phone number."; in ParseHelper()
2325 VLOG(2) << "The string supplied is too long to be a phone number."; in ParseHelper()
2334 phone_number->Swap(&temp_number); in ParseHelper()
2338 // Attempts to extract a possible number from the string passed in. This
2340 // phone number. Characters that can be used to start a phone number are
2342 // found in the number passed in, an empty string is returned. This function
2344 // more are present, such as in the case of: (530) 583-6985 x302/x2303. The
2345 // second extension here makes this actually two phone numbers, (530) 583-6985
2346 // x302 and (530) 583-6985 x2303. We remove the second extension so that the
2347 // first number is parsed correctly.
2348 void PhoneNumberUtil::ExtractPossibleNumber(const string& number, in ExtractPossibleNumber() argument
2353 number_as_unicode.PointToUTF8(number.data(), static_cast<int>(number.size())); in ExtractPossibleNumber()
2355 // The input wasn't valid UTF-8. Produce an empty string to indicate an error. in ExtractPossibleNumber()
2356 extracted_number->clear(); in ExtractPossibleNumber()
2365 if (reg_exps_->valid_start_char_pattern_->FullMatch(current_char)) { in ExtractPossibleNumber()
2373 extracted_number->clear(); in ExtractPossibleNumber()
2377 extracted_number->assign( in ExtractPossibleNumber()
2380 if (extracted_number->length() == 0) { in ExtractPossibleNumber()
2385 reg_exps_->capture_up_to_second_number_start_pattern_-> in ExtractPossibleNumber()
2389 bool PhoneNumberUtil::IsPossibleNumber(const PhoneNumber& number) const { in IsPossibleNumber()
2390 ValidationResult result = IsPossibleNumberWithReason(number); in IsPossibleNumber()
2395 const PhoneNumber& number, const PhoneNumberType type) const { in IsPossibleNumberForType() argument
2396 ValidationResult result = IsPossibleNumberForTypeWithReason(number, type); in IsPossibleNumberForType()
2401 const string& number, in IsPossibleNumberForString() argument
2404 if (Parse(number, region_dialing_from, &number_proto) == NO_PARSING_ERROR) { in IsPossibleNumberForString()
2412 const PhoneNumber& number) const { in IsPossibleNumberWithReason()
2413 return IsPossibleNumberForTypeWithReason(number, PhoneNumberUtil::UNKNOWN); in IsPossibleNumberWithReason()
2417 PhoneNumberUtil::IsPossibleNumberForTypeWithReason(const PhoneNumber& number, in IsPossibleNumberForTypeWithReason() argument
2420 GetNationalSignificantNumber(number, &national_number); in IsPossibleNumberForTypeWithReason()
2421 int country_code = number.country_code(); in IsPossibleNumberForTypeWithReason()
2424 // GetRegionCodeForNumber will not work if the number is possible but not in IsPossibleNumberForTypeWithReason()
2425 // valid. There is in fact one country calling code (290) where the possible in IsPossibleNumberForTypeWithReason()
2426 // number pattern differs between various regions (Saint Helena and Tristan da in IsPossibleNumberForTypeWithReason()
2427 // Cuñha), but this is handled by putting all possible lengths for any country in IsPossibleNumberForTypeWithReason()
2435 // Metadata cannot be NULL because the country calling code is valid. in IsPossibleNumberForTypeWithReason()
2441 bool PhoneNumberUtil::TruncateTooLongNumber(PhoneNumber* number) const { in TruncateTooLongNumber()
2442 if (IsValidNumber(*number)) { in TruncateTooLongNumber()
2445 PhoneNumber number_copy(*number); in TruncateTooLongNumber()
2446 uint64 national_number = number->national_number(); in TruncateTooLongNumber()
2455 number->set_national_number(national_number); in TruncateTooLongNumber()
2460 const PhoneNumber& number) const { in GetNumberType()
2462 GetRegionCodeForNumber(number, ®ion_code); in GetNumberType()
2464 GetMetadataForRegionOrCallingCode(number.country_code(), region_code); in GetNumberType()
2469 GetNationalSignificantNumber(number, &national_significant_number); in GetNumberType()
2473 bool PhoneNumberUtil::IsValidNumber(const PhoneNumber& number) const { in IsValidNumber()
2475 GetRegionCodeForNumber(number, ®ion_code); in IsValidNumber()
2476 return IsValidNumberForRegion(number, region_code); in IsValidNumber()
2479 bool PhoneNumberUtil::IsValidNumberForRegion(const PhoneNumber& number, in IsValidNumberForRegion() argument
2481 int country_code = number.country_code(); in IsValidNumberForRegion()
2488 // number does not match that of the region code. in IsValidNumberForRegion()
2492 GetNationalSignificantNumber(number, &national_number); in IsValidNumberForRegion()
2507 (reg_exps_->geo_mobile_countries_.find(country_calling_code) in IsNumberGeographical()
2508 != reg_exps_->geo_mobile_countries_.end() && in IsNumberGeographical()
2517 phone_number->set_italian_leading_zero(true); in SetItalianLeadingZerosForPhoneNumber()
2519 // Note that if the national number is all "0"s, the last "0" is not in SetItalianLeadingZerosForPhoneNumber()
2521 while (number_of_leading_zeros < national_number.length() - 1 && in SetItalianLeadingZerosForPhoneNumber()
2526 phone_number->set_number_of_leading_zeros(static_cast<int32_t>(number_of_leading_zeros)); in SetItalianLeadingZerosForPhoneNumber()
2533 // Check if any possible number lengths are present; if so, we use them to in IsNumberMatchingDesc()
2536 // already checked before checking a specific number type. in IsNumberMatchingDesc()
2550 VLOG(4) << "Number type unknown - doesn't match general national number" in GetNumberTypeHelper()
2555 VLOG(4) << "Number is a premium number."; in GetNumberTypeHelper()
2559 VLOG(4) << "Number is a toll-free number."; in GetNumberTypeHelper()
2563 VLOG(4) << "Number is a shared cost number."; in GetNumberTypeHelper()
2567 VLOG(4) << "Number is a VOIP (Voice over IP) number."; in GetNumberTypeHelper()
2571 VLOG(4) << "Number is a personal number."; in GetNumberTypeHelper()
2575 VLOG(4) << "Number is a pager number."; in GetNumberTypeHelper()
2579 VLOG(4) << "Number is a UAN."; in GetNumberTypeHelper()
2583 VLOG(4) << "Number is a voicemail number."; in GetNumberTypeHelper()
2591 VLOG(4) << "Fixed-line and mobile patterns equal, number is fixed-line" in GetNumberTypeHelper()
2595 VLOG(4) << "Fixed-line and mobile patterns differ, but number is " in GetNumberTypeHelper()
2596 << "still fixed-line or mobile"; in GetNumberTypeHelper()
2599 VLOG(4) << "Number is a fixed line number."; in GetNumberTypeHelper()
2602 // Otherwise, test to see if the number is mobile. Only do this if certain in GetNumberTypeHelper()
2606 VLOG(4) << "Number is a mobile number."; in GetNumberTypeHelper()
2609 VLOG(4) << "Number type unknown - doesn\'t match any specific number type" in GetNumberTypeHelper()
2615 const PhoneNumber& number, in GetNationalSignificantNumber() argument
2618 // If leading zero(s) have been set, we prefix this now. Note this is not a in GetNationalSignificantNumber()
2619 // national prefix. Ensure the number of leading zeros is at least 0 so we in GetNationalSignificantNumber()
2621 StrAppend(national_number, number.italian_leading_zero() ? in GetNationalSignificantNumber()
2622 string(std::max(number.number_of_leading_zeros(), 0), '0') : ""); in GetNationalSignificantNumber()
2623 StrAppend(national_number, number.national_number()); in GetNationalSignificantNumber()
2627 const PhoneNumber& number) const { in GetLengthOfGeographicalAreaCode()
2629 GetRegionCodeForNumber(number, ®ion_code); in GetLengthOfGeographicalAreaCode()
2634 // If a country doesn't use a national prefix, and this number doesn't have an in GetLengthOfGeographicalAreaCode()
2635 // Italian leading zero, we assume it is a closed dialling plan with no area in GetLengthOfGeographicalAreaCode()
2637 if (!metadata->has_national_prefix() && !number.italian_leading_zero()) { in GetLengthOfGeographicalAreaCode()
2641 PhoneNumberType type = GetNumberType(number); in GetLengthOfGeographicalAreaCode()
2642 int country_calling_code = number.country_code(); in GetLengthOfGeographicalAreaCode()
2644 reg_exps_->geo_mobile_countries_without_mobile_area_codes_.find( in GetLengthOfGeographicalAreaCode()
2646 reg_exps_->geo_mobile_countries_without_mobile_area_codes_.end()) { in GetLengthOfGeographicalAreaCode()
2654 return GetLengthOfNationalDestinationCode(number); in GetLengthOfGeographicalAreaCode()
2658 const PhoneNumber& number) const { in GetLengthOfNationalDestinationCode()
2659 PhoneNumber copied_proto(number); in GetLengthOfNationalDestinationCode()
2660 if (number.has_extension()) { in GetLengthOfNationalDestinationCode()
2668 reg_exps_->regexp_factory_->CreateInput(formatted_number)); in GetLengthOfNationalDestinationCode()
2673 if (!reg_exps_->capturing_ascii_digits_pattern_->FindAndConsume( in GetLengthOfNationalDestinationCode()
2685 if (GetNumberType(number) == MOBILE) { in GetLengthOfNationalDestinationCode()
2689 // mobile token, which also forms part of the national significant number. in GetLengthOfNationalDestinationCode()
2690 // This assumes that the mobile token is always formatted separately from in GetLengthOfNationalDestinationCode()
2691 // the rest of the phone number. in GetLengthOfNationalDestinationCode()
2693 GetCountryMobileToken(number.country_code(), &mobile_token); in GetLengthOfNationalDestinationCode()
2704 std::map<int, char>::iterator it = reg_exps_->mobile_token_mappings_.find( in GetCountryMobileToken()
2706 if (it != reg_exps_->mobile_token_mappings_.end()) { in GetCountryMobileToken()
2707 *mobile_token = it->second; in GetCountryMobileToken()
2709 mobile_token->assign(""); in GetCountryMobileToken()
2713 void PhoneNumberUtil::NormalizeDigitsOnly(string* number) const { in NormalizeDigitsOnly()
2714 DCHECK(number); in NormalizeDigitsOnly()
2715 const RegExp& non_digits_pattern = reg_exps_->regexp_cache_->GetRegExp( in NormalizeDigitsOnly()
2718 non_digits_pattern.GlobalReplace(number, ""); in NormalizeDigitsOnly()
2720 number->assign(NormalizeUTF8::NormalizeDecimalDigits(*number)); in NormalizeDigitsOnly()
2723 void PhoneNumberUtil::NormalizeDiallableCharsOnly(string* number) const { in NormalizeDiallableCharsOnly()
2724 DCHECK(number); in NormalizeDiallableCharsOnly()
2725 NormalizeHelper(reg_exps_->diallable_char_mappings_, in NormalizeDiallableCharsOnly()
2726 true /* remove non matches */, number); in NormalizeDiallableCharsOnly()
2729 bool PhoneNumberUtil::IsAlphaNumber(const string& number) const { in IsAlphaNumber()
2730 if (!IsViablePhoneNumber(number)) { in IsAlphaNumber()
2731 // Number is too short, or doesn't match the basic phone number pattern. in IsAlphaNumber()
2734 // Copy the number, since we are going to try and strip the extension from it. in IsAlphaNumber()
2735 string number_copy(number); in IsAlphaNumber()
2738 return reg_exps_->valid_alpha_phone_pattern_->FullMatch(number_copy); in IsAlphaNumber()
2741 void PhoneNumberUtil::ConvertAlphaCharactersInNumber(string* number) const { in ConvertAlphaCharactersInNumber()
2742 DCHECK(number); in ConvertAlphaCharactersInNumber()
2743 NormalizeHelper(reg_exps_->alpha_phone_mappings_, false, number); in ConvertAlphaCharactersInNumber()
2746 // Normalizes a string of characters representing a phone number. This performs
2748 // - Punctuation is stripped.
2750 // - Letters are converted to their numeric representation on a telephone
2751 // keypad. The keypad used here is the one defined in ITU Recommendation
2752 // E.161. This is only done if there are 3 or more letters in the number, to
2755 // - Wide-ascii digits are converted to normal ASCII (European) digits.
2756 // - Arabic-Indic numerals are converted to European numerals.
2757 // - Spurious alpha characters are stripped.
2758 void PhoneNumberUtil::Normalize(string* number) const { in Normalize()
2759 DCHECK(number); in Normalize()
2760 if (reg_exps_->valid_alpha_phone_pattern_->PartialMatch(*number)) { in Normalize()
2761 NormalizeHelper(reg_exps_->alpha_phone_mappings_, true, number); in Normalize()
2763 NormalizeDigitsOnly(number); in Normalize()
2766 // Checks to see if the string of characters could possibly be a phone number at
2769 // method does not require the number to be normalized in advance - but does
2770 // assume that leading non-number symbols have been removed, such as by the
2772 bool PhoneNumberUtil::IsViablePhoneNumber(const string& number) const { in IsViablePhoneNumber()
2773 if (number.length() < kMinLengthForNsn) { in IsViablePhoneNumber()
2776 return reg_exps_->valid_phone_number_pattern_->FullMatch(number); in IsViablePhoneNumber()
2779 // Strips the IDD from the start of the number if present. Helper function used
2782 string* number) const { in ParsePrefixAsIdd()
2783 DCHECK(number); in ParsePrefixAsIdd()
2785 reg_exps_->regexp_factory_->CreateInput(*number)); in ParsePrefixAsIdd()
2789 // Only strip this if the first digit after the match is not a 0, since in ParsePrefixAsIdd()
2792 if (reg_exps_->capturing_digit_pattern_->PartialMatch( in ParsePrefixAsIdd()
2793 number_copy->ToString(), &extracted_digit)) { in ParsePrefixAsIdd()
2799 number->assign(number_copy->ToString()); in ParsePrefixAsIdd()
2805 // Strips any international prefix (such as +, 00, 011) present in the number
2806 // provided, normalizes the resulting number, and indicates if an international
2810 // the region we think this number may be dialed in.
2812 // number, otherwise false if the number did not seem to be in international
2817 string* number) const { in MaybeStripInternationalPrefixAndNormalize()
2818 DCHECK(number); in MaybeStripInternationalPrefixAndNormalize()
2819 if (number->empty()) { in MaybeStripInternationalPrefixAndNormalize()
2823 reg_exps_->regexp_factory_->CreateInput(*number)); in MaybeStripInternationalPrefixAndNormalize()
2824 if (reg_exps_->plus_chars_pattern_->Consume(number_string_piece.get())) { in MaybeStripInternationalPrefixAndNormalize()
2825 number->assign(number_string_piece->ToString()); in MaybeStripInternationalPrefixAndNormalize()
2826 // Can now normalize the rest of the number since we've consumed the "+" in MaybeStripInternationalPrefixAndNormalize()
2828 Normalize(number); in MaybeStripInternationalPrefixAndNormalize()
2833 reg_exps_->regexp_cache_->GetRegExp(possible_idd_prefix); in MaybeStripInternationalPrefixAndNormalize()
2834 Normalize(number); in MaybeStripInternationalPrefixAndNormalize()
2835 return ParsePrefixAsIdd(idd_pattern, number) in MaybeStripInternationalPrefixAndNormalize()
2840 // Strips any national prefix (such as 0, 1) present in the number provided.
2841 // The number passed in should be the normalized telephone number that we wish
2843 // region that we think this number is from. Returns true if a national prefix
2847 string* number, in MaybeStripNationalPrefixAndCarrierCode() argument
2849 DCHECK(number); in MaybeStripNationalPrefixAndCarrierCode()
2853 if (number->empty() || possible_national_prefix.empty()) { in MaybeStripNationalPrefixAndCarrierCode()
2858 // We use two copies here since Consume modifies the phone number, and if the in MaybeStripNationalPrefixAndCarrierCode()
2859 // first if-clause fails the number will already be changed. in MaybeStripNationalPrefixAndCarrierCode()
2861 reg_exps_->regexp_factory_->CreateInput(*number)); in MaybeStripNationalPrefixAndCarrierCode()
2863 reg_exps_->regexp_factory_->CreateInput(*number)); in MaybeStripNationalPrefixAndCarrierCode()
2864 string number_string_copy(*number); in MaybeStripNationalPrefixAndCarrierCode()
2867 // Check if the original number is viable. in MaybeStripNationalPrefixAndCarrierCode()
2869 IsMatch(*matcher_api_, *number, general_desc); in MaybeStripNationalPrefixAndCarrierCode()
2874 reg_exps_->regexp_cache_->GetRegExp(possible_national_prefix); in MaybeStripNationalPrefixAndCarrierCode()
2883 // We make the transformation and check that the resultant number is still in MaybeStripNationalPrefixAndCarrierCode()
2884 // viable. If so, replace the number and return. in MaybeStripNationalPrefixAndCarrierCode()
2891 number->assign(number_string_copy); in MaybeStripNationalPrefixAndCarrierCode()
2893 carrier_code->assign(carrier_code_temp); in MaybeStripNationalPrefixAndCarrierCode()
2900 // If captured_part_of_prefix is empty, this implies nothing was captured by in MaybeStripNationalPrefixAndCarrierCode()
2902 // transformation is necessary, and we just remove the national prefix. in MaybeStripNationalPrefixAndCarrierCode()
2904 number_copy_without_transform->ToString(); in MaybeStripNationalPrefixAndCarrierCode()
2909 number->assign(number_copy_as_string); in MaybeStripNationalPrefixAndCarrierCode()
2911 carrier_code->assign(carrier_code_temp); in MaybeStripNationalPrefixAndCarrierCode()
2920 // Strips any extension (as in, the part of the number dialled after the call is
2922 // the number, and returns it. The number passed in should be non-normalized.
2923 bool PhoneNumberUtil::MaybeStripExtension(string* number, std::string* extension) in MaybeStripExtension() argument
2925 DCHECK(number); in MaybeStripExtension()
2934 string number_copy(*number); in MaybeStripExtension()
2936 reg_exps_->regexp_factory_->CreateInput(number_copy)); in MaybeStripExtension()
2937 if (reg_exps_->extn_pattern_->Consume( in MaybeStripExtension()
2943 reg_exps_->extn_pattern_->Replace(&number_copy, ""); in MaybeStripExtension()
2944 // If we find a potential extension, and the number preceding this is a in MaybeStripExtension()
2945 // viable number, we assume it is an extension. in MaybeStripExtension()
2951 number->assign(number_copy); in MaybeStripExtension()
2953 extension->assign(possible_extension_one); in MaybeStripExtension()
2955 extension->assign(possible_extension_two); in MaybeStripExtension()
2957 extension->assign(possible_extension_three); in MaybeStripExtension()
2959 extension->assign(possible_extension_four); in MaybeStripExtension()
2961 extension->assign(possible_extension_five); in MaybeStripExtension()
2963 extension->assign(possible_extension_six); in MaybeStripExtension()
2974 // leaves national_number unmodified. Assumes the national_number is at least 3
2978 if (national_number->empty() || (national_number->at(0) == '0')) { in ExtractCountryCode()
2983 safe_strto32(national_number->substr(0, i), &potential_country_code); in ExtractCountryCode()
2987 national_number->erase(0, i); in ExtractCountryCode()
2994 // Tries to extract a country calling code from a number. Country calling codes
2996 // - by stripping the international dialing prefix of the region the person
2997 // is dialing from, if this is present in the number, and looking at the next
2999 // - by stripping the '+' sign if present and then looking at the next digits
3000 // - by comparing the start of the number and the country calling code of the
3001 // default region. If the number is not considered possible for the numbering
3004 // country calling code. If this number is considered a possible number, then
3011 // If NO_PARSING_ERROR is returned, the national_number without the country
3012 // calling code is populated, and the country_code of the phone_number passed
3013 // in is set to the country calling code if found, otherwise to 0.
3021 // Set the default prefix to be something that will never match if there is no in MaybeExtractCountryCode()
3024 ? default_region_metadata->international_prefix() in MaybeExtractCountryCode()
3030 phone_number->set_country_code_source(country_code_source); in MaybeExtractCountryCode()
3033 if (national_number->length() <= kMinLengthForNsn) { in MaybeExtractCountryCode()
3034 VLOG(2) << "Phone number had an IDD, but after this was not " in MaybeExtractCountryCode()
3035 << "long enough to be a viable phone number."; in MaybeExtractCountryCode()
3040 phone_number->set_country_code(potential_country_code); in MaybeExtractCountryCode()
3047 // Check to see if the number starts with the country calling code for the in MaybeExtractCountryCode()
3049 // checks on the validity of the number before and after. in MaybeExtractCountryCode()
3050 int default_country_code = default_region_metadata->country_code(); in MaybeExtractCountryCode()
3058 default_region_metadata->general_desc(); in MaybeExtractCountryCode()
3062 VLOG(4) << "Number without country calling code prefix"; in MaybeExtractCountryCode()
3063 // If the number was not valid before but is valid now, or if it was too in MaybeExtractCountryCode()
3064 // long before, we consider the number with the country code stripped to in MaybeExtractCountryCode()
3071 national_number->assign(potential_national_number); in MaybeExtractCountryCode()
3073 phone_number->set_country_code_source( in MaybeExtractCountryCode()
3076 phone_number->set_country_code(default_country_code); in MaybeExtractCountryCode()
3082 phone_number->set_country_code(0); in MaybeExtractCountryCode()
3089 // We only are about the fields that uniquely define a number, so we copy in IsNumberMatch()
3108 // A SHORT_NSN_MATCH occurs if there is a difference because of the in IsNumberMatch()
3114 // This is not a match. in IsNumberMatch()
3121 // If all else was the same, then this is an NSN_MATCH. in IsNumberMatch()
3159 // One or more of the phone numbers we are trying to match is not a viable in IsNumberMatchWithTwoStrings()
3160 // phone number. in IsNumberMatchWithTwoStrings()
3167 // First see if the second number has an implicit country calling code, by in IsNumberMatchWithOneString()
3176 // The second number has no country calling code. EXACT_MATCH is no longer in IsNumberMatchWithOneString()
3178 // first number, and if EXACT_MATCH is returned, we replace this with in IsNumberMatchWithOneString()
3194 // If the first number didn't have a valid country calling code, then we in IsNumberMatchWithOneString()
3195 // parse the second number without one as well. in IsNumberMatchWithOneString()
3203 // One or more of the phone numbers we are trying to match is not a viable in IsNumberMatchWithOneString()
3204 // phone number. in IsNumberMatchWithOneString()
3214 const PhoneNumber& number) const { in CanBeInternationallyDialled()
3216 GetRegionCodeForNumber(number, ®ion_code); in CanBeInternationallyDialled()
3219 // Note numbers belonging to non-geographical entities (e.g. +800 numbers) in CanBeInternationallyDialled()
3224 GetNationalSignificantNumber(number, &national_significant_number); in CanBeInternationallyDialled()
3226 national_significant_number, metadata->no_international_dialling()); in CanBeInternationallyDialled()