@@ -1485,14 +1485,15 @@ def format_in_original_format(numobj, region_calling_from):
14851485
14861486 Returns the formatted phone number in its original number format.
14871487 """
1488- if (numobj .raw_input is not None and not _has_formatting_pattern_for_number (numobj )):
1488+ format_rule = _choose_formatting_pattern_for_numobj (numobj )
1489+ if (numobj .raw_input is not None and format_rule is None ):
14891490 # We check if we have the formatting pattern because without that, we
14901491 # might format the number as a group without national prefix.
14911492 return numobj .raw_input
14921493 if numobj .country_code_source is CountryCodeSource .UNSPECIFIED :
14931494 return format_number (numobj , PhoneNumberFormat .NATIONAL )
14941495
1495- formatted_number = _format_original_allow_mods (numobj , region_calling_from )
1496+ formatted_number = _format_original_allow_mods (numobj , region_calling_from , format_rule )
14961497 num_raw_input = numobj .raw_input
14971498 # If no digit is inserted/removed/modified as a result of our formatting,
14981499 # we return the formatted phone number; otherwise we return the raw input
@@ -1505,7 +1506,7 @@ def format_in_original_format(numobj, region_calling_from):
15051506 return formatted_number
15061507
15071508
1508- def _format_original_allow_mods (numobj , region_calling_from ):
1509+ def _format_original_allow_mods (numobj , region_calling_from , format_rule ):
15091510 if (numobj .country_code_source == CountryCodeSource .FROM_NUMBER_WITH_PLUS_SIGN ):
15101511 return format_number (numobj , PhoneNumberFormat .INTERNATIONAL )
15111512 elif numobj .country_code_source == CountryCodeSource .FROM_NUMBER_WITH_IDD :
@@ -1527,12 +1528,6 @@ def _format_original_allow_mods(numobj, region_calling_from):
15271528 if (_raw_input_contains_national_prefix (numobj .raw_input , national_prefix , region_code )):
15281529 # If so, we can safely return the national format.
15291530 return national_format
1530- # Metadata cannot be None here because ndd_prefix_for_region() (above) returns None if
1531- # there is no metadata for the region.
1532- metadata = PhoneMetadata .metadata_for_region (region_code )
1533- assert metadata is not None
1534- national_number = national_significant_number (numobj )
1535- format_rule = _choose_formatting_pattern_for_number (metadata .number_format , national_number )
15361531 # The format rule could still be null here if the national number was
15371532 # 0 and there was no raw input (this should not be possible for
15381533 # numbers generated by the phonenumber library as they would also not
@@ -1578,15 +1573,28 @@ def _raw_input_contains_national_prefix(raw_input, national_prefix, region_code)
15781573 return False
15791574
15801575
1581- def _has_formatting_pattern_for_number (numobj ):
1576+ def _choose_formatting_pattern_for_numobj (numobj ):
15821577 country_code = numobj .country_code
15831578 phone_number_region = region_code_for_country_code (country_code )
15841579 metadata = PhoneMetadata .metadata_for_region_or_calling_code (country_code , phone_number_region )
15851580 if metadata is None :
1586- return False
1581+ return None
15871582 national_number = national_significant_number (numobj )
1588- format_rule = _choose_formatting_pattern_for_number (metadata .number_format , national_number )
1589- return format_rule is not None
1583+ return _choose_formatting_pattern_for_number (metadata .number_format , national_number )
1584+
1585+
1586+ def _choose_formatting_pattern_for_number (available_formats , national_number ):
1587+ for num_format in available_formats :
1588+ size = len (num_format .leading_digits_pattern )
1589+ # We always use the last leading_digits_pattern, as it is the most detailed.
1590+ if size > 0 :
1591+ ld_pattern = re .compile (num_format .leading_digits_pattern [- 1 ])
1592+ ld_match = ld_pattern .match (national_number )
1593+ if size == 0 or ld_match :
1594+ format_pattern = re .compile (num_format .pattern )
1595+ if fullmatch (format_pattern , national_number ):
1596+ return num_format
1597+ return None
15901598
15911599
15921600def format_out_of_country_keeping_alpha_chars (numobj , region_calling_from ):
@@ -1769,20 +1777,6 @@ def _format_nsn(number, metadata, num_format, carrier_code=None):
17691777 return _format_nsn_using_pattern (number , formatting_pattern , num_format , carrier_code )
17701778
17711779
1772- def _choose_formatting_pattern_for_number (available_formats , national_number ):
1773- for num_format in available_formats :
1774- size = len (num_format .leading_digits_pattern )
1775- # We always use the last leading_digits_pattern, as it is the most detailed.
1776- if size > 0 :
1777- ld_pattern = re .compile (num_format .leading_digits_pattern [- 1 ])
1778- ld_match = ld_pattern .match (national_number )
1779- if size == 0 or ld_match :
1780- format_pattern = re .compile (num_format .pattern )
1781- if fullmatch (format_pattern , national_number ):
1782- return num_format
1783- return None
1784-
1785-
17861780def _format_nsn_using_pattern (national_number , formatting_pattern , number_format ,
17871781 carrier_code = None ):
17881782 # Note that carrier_code is optional - if None or an empty string, no
0 commit comments