|
30 | 30 | from .phonenumberutil import MatchType, NumberParseException, PhoneNumberFormat |
31 | 31 | from .phonenumberutil import is_possible_number, is_valid_number, parse |
32 | 32 | from .phonenumberutil import normalize_digits_only, national_significant_number |
33 | | -from .phonenumberutil import format_nsn_using_pattern |
| 33 | +from .phonenumberutil import format_nsn_using_pattern, ndd_prefix_for_region |
34 | 34 | from .phonenumberutil import format_number, is_number_match, region_code_for_country_code |
35 | 35 | from .phonenumberutil import _maybe_strip_national_prefix_carrier_code |
36 | 36 | from .phonenumberutil import choose_formatting_pattern_for_number |
| 37 | +from .phonenumberutil import _formatting_rule_has_first_group_only |
37 | 38 | from .phonenumber import CountryCodeSource |
38 | 39 | from .phonemetadata import PhoneMetadata |
39 | 40 |
|
@@ -236,12 +237,19 @@ def _all_number_groups_remain_grouped(numobj, normalized_candidate, formatted_nu |
236 | 237 | # Moves from_index forward. |
237 | 238 | from_index += len(formatted_number_group) |
238 | 239 | if (ii == 0 and from_index < len(normalized_candidate)): |
239 | | - # We are at the position right after the NDC. |
240 | | - if normalized_candidate[from_index].isdigit(): |
| 240 | + # We are at the position right after the NDC. We get the region |
| 241 | + # used for formatting information based on the country code in the |
| 242 | + # phone number, rather than the number itself, as we do not need |
| 243 | + # to distinguish between different countries with the same country |
| 244 | + # calling code and this is faster. |
| 245 | + region = region_code_for_country_code(numobj.country_code) |
| 246 | + if (ndd_prefix_for_region(region, True) is not None and |
| 247 | + normalized_candidate[from_index].isdigit()): |
241 | 248 | # This means there is no formatting symbol after the NDC. In |
242 | 249 | # this case, we only accept the number if there is no |
243 | 250 | # formatting symbol at all in the number, except for |
244 | | - # extensions. |
| 251 | + # extensions. This is only important for countries with |
| 252 | + # national prefixes. |
245 | 253 | nsn = national_significant_number(numobj) |
246 | 254 | return normalized_candidate[(from_index - len(formatted_number_group)):].startswith(nsn) |
247 | 255 | # The check here makes sure that we haven't mistakenly already used the extension to |
@@ -409,13 +417,7 @@ def _is_national_prefix_present_if_required(numobj): |
409 | 417 | # The national-prefix is optional in these cases, so we don't need |
410 | 418 | # to check if it was present. |
411 | 419 | return True |
412 | | - # Remove the first-group symbol. |
413 | | - candidate_national_prefix_rule = format_rule.national_prefix_formatting_rule |
414 | | - # We assume that the first-group symbol will never be _before_ the |
415 | | - # national prefix. |
416 | | - candidate_national_prefix_rule = candidate_national_prefix_rule[:candidate_national_prefix_rule.find("\\1")] |
417 | | - candidate_national_prefix_rule = normalize_digits_only(candidate_national_prefix_rule) |
418 | | - if len(candidate_national_prefix_rule) == 0: |
| 420 | + if _formatting_rule_has_first_group_only(format_rule.national_prefix_formatting_rule): |
419 | 421 | # National Prefix not needed for this number. |
420 | 422 | return True |
421 | 423 | # Normalize the remainder. |
|
0 commit comments