Skip to content

Commit 2e4dca5

Browse files
committed
Merge code changes from upstream r656
1 parent 59e72d8 commit 2e4dca5

2 files changed

Lines changed: 36 additions & 16 deletions

File tree

python/phonenumbers/asyoutypeformatter.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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:

python/tests/asyoutypetest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,26 @@ def testAYTFClearNDDAfterIDDExtraction(self):
11381138
self.assertEqual("007001234567890123456", formatter.input_digit('6'))
11391139
self.assertEqual("0070012345678901234567", formatter.input_digit('7'))
11401140

1141+
def testAYTFNumberPatternsBecomingInvalidShouldNotResultInDigitLoss(self):
1142+
formatter = AsYouTypeFormatter("CN")
1143+
self.assertEqual("+", formatter.input_digit('+'))
1144+
self.assertEqual("+8", formatter.input_digit('8'))
1145+
self.assertEqual("+86 ", formatter.input_digit('6'))
1146+
self.assertEqual("+86 9", formatter.input_digit('9'))
1147+
self.assertEqual("+86 98", formatter.input_digit('8'))
1148+
self.assertEqual("+86 988", formatter.input_digit('8'))
1149+
self.assertEqual("+86 988 1", formatter.input_digit('1'))
1150+
# Now the number pattern is no longer valid because there are multiple
1151+
# leading digit patterns; when we try again to extract a country code
1152+
# we should ensure we use the last leading digit pattern, rather than
1153+
# the first one such that it *thinks* it's found a valid formatting
1154+
# rule again.
1155+
# https://code.google.com/p/libphonenumber/issues/detail?id=437
1156+
self.assertEqual("+8698812", formatter.input_digit('2'))
1157+
self.assertEqual("+86988123", formatter.input_digit('3'))
1158+
self.assertEqual("+869881234", formatter.input_digit('4'))
1159+
self.assertEqual("+8698812345", formatter.input_digit('5'))
1160+
11411161
def testAYTFShortNumberFormatting_AR(self):
11421162
# Python version extra test: use real metadata
11431163
formatter = AsYouTypeFormatter("AR")

0 commit comments

Comments
 (0)