@@ -134,7 +134,7 @@ def _maybe_create_new_template(self):
134134 self ._able_to_format = False
135135 return False
136136
137- def _get_available_formats (self , leading_three_digits ):
137+ def _get_available_formats (self , leading_digits ):
138138 if (self ._is_complete_number and
139139 len (self ._current_metadata .intl_number_format ) > 0 ):
140140 format_list = self ._current_metadata .intl_number_format
@@ -147,7 +147,7 @@ def _get_available_formats(self, leading_three_digits):
147147 _formatting_rule_has_first_group_only (this_format .national_prefix_formatting_rule )):
148148 if self ._is_format_eligible (this_format .format ):
149149 self ._possible_formats .append (this_format )
150- self ._narrow_down_possible_formats (leading_three_digits )
150+ self ._narrow_down_possible_formats (leading_digits )
151151
152152 def _is_format_eligible (self , format ):
153153 return fullmatch (_ELIGIBLE_FORMAT_PATTERN , format )
@@ -156,19 +156,19 @@ def _narrow_down_possible_formats(self, leading_digits):
156156 index_of_leading_digits_pattern = len (leading_digits ) - _MIN_LEADING_DIGITS_LENGTH
157157 ii = 0
158158 while ii < len (self ._possible_formats ):
159- format = self ._possible_formats [ii ]
159+ num_format = self ._possible_formats [ii ]
160160 ii += 1
161- if len (format .leading_digits_pattern ) > index_of_leading_digits_pattern :
162- leading_digits_pattern = re . compile ( format . leading_digits_pattern [ index_of_leading_digits_pattern ])
163- m = leading_digits_pattern . match ( leading_digits )
164- if not m :
165- # remove the element we've just examined, now at (ii- 1)
166- ii -= 1
167- self . _possible_formats . pop ( ii )
168- else :
169- # The particular format has no more specific
170- # leading_digits_pattern, and it should be retained.
171- pass
161+ if len (num_format .leading_digits_pattern ) == 0 :
162+ # Keep everything that isn't restricted by leading digits.
163+ continue
164+ last_leading_digits_pattern = min ( index_of_leading_digits_pattern ,
165+ len ( num_format . leading_digits_pattern ) - 1 )
166+ leading_digits_pattern = re . compile ( num_format . leading_digits_pattern [ last_leading_digits_pattern ])
167+ m = leading_digits_pattern . match ( leading_digits )
168+ if not m :
169+ # remove the element we've just examined, now at (ii-1)
170+ ii -= 1
171+ self . _possible_formats . pop ( ii )
172172
173173 def _create_formatting_template (self , num_format ):
174174 number_pattern = num_format .pattern
@@ -337,7 +337,7 @@ def input_digit(self, next_char, remember_position=False):
337337 self ._current_output = self ._prefix_before_national_number + self ._national_number
338338 return self ._current_output
339339
340- if len (self ._possible_formats ) > 0 : # The formatting pattern is already chosen.
340+ if len (self ._possible_formats ) > 0 : # The formatting patterns are already chosen.
341341 temp_national_number = self ._input_digit_helper (next_char )
342342 # See if the accrued digits can be formatted properly already. If
343343 # not, use the results from input_digit_helper, which does
@@ -441,7 +441,7 @@ def _attempt_to_choose_formatting_pattern(self):
441441 # We start to attempt to format only when at least MIN_LEADING_DIGITS_LENGTH digits of national
442442 # number (excluding national prefix) have been entered.
443443 if len (self ._national_number ) >= _MIN_LEADING_DIGITS_LENGTH :
444- self ._get_available_formats (self ._national_number [: _MIN_LEADING_DIGITS_LENGTH ] )
444+ self ._get_available_formats (self ._national_number )
445445 # See if the accrued digits can be formatted properly already.
446446 formatted_number = self ._attempt_to_format_accrued_digits ()
447447 if len (formatted_number ) > 0 :
0 commit comments