2828from re_util import fullmatch
2929
3030from phonemetadata import PhoneMetadata
31- from phonenumberutil import _VALID_START_CHAR_PATTERN , _DIGIT_MAPPINGS , _PLUS_SIGN
31+ from phonenumberutil import _VALID_START_CHAR_PATTERN , _VALID_PUNCTUATION
32+ from phonenumberutil import _DIGIT_MAPPINGS , _PLUS_SIGN
3233from phonenumberutil import _extract_country_code , region_code_for_country_code
3334
34- _EMPTY_METADATA = PhoneMetadata (id = "" , international_prefix = "NA" , register = False )
35+ _EMPTY_METADATA = PhoneMetadata (id = u "" , international_prefix = u "NA" , register = False )
3536
3637# A pattern that is used to match character classes in regular expressions. An
3738# example of a character class is [1-4].
38- _CHARACTER_CLASS_PATTERN = re .compile ("\\ [([^\\ [\\ ]])*\\ ]" )
39+ _CHARACTER_CLASS_PATTERN = re .compile (u "\\ [([^\\ [\\ ]])*\\ ]" )
3940# Any digit in a regular expression that actually denotes a digit. For
4041# example, in the regular expression 80[0-2]\d{6,10}, the first 2 digits (8
4142# and 0) are standalone digits, but the rest are not.
4243# Two look-aheads are needed because the number following \\d could be a
4344# two-digit number, since the phone number can be as long as 15 digits.
44- _STANDALONE_DIGIT_PATTERN = re .compile ("\\ d(?=[^,}][^,}])" )
45+ _STANDALONE_DIGIT_PATTERN = re .compile (u"\\ d(?=[^,}][^,}])" )
46+
47+ # A pattern that is used to determine if a number_format under
48+ # available_formats is eligible to be used by the AYTF. It is eligible when
49+ # the format element under number_format contains groups of the dollar sign
50+ # followed by a single digit, separated by valid phone number
51+ # punctuation. This prevents invalid punctuation (such as the star sign in
52+ # Israeli star numbers) getting into the output of the AYTF.
53+ _ELIGIBLE_FORMAT_PATTERN = re .compile (u"[" + _VALID_PUNCTUATION + u"]*" +
54+ u"(\\ \\ \\ d" + u"[" + _VALID_PUNCTUATION + u"]*)+" )
4555
4656# This is the minimum length of national number accrued that is required to
4757# trigger the formatter. The first element of the leadingDigitsPattern of each
@@ -96,9 +106,14 @@ def _get_available_formats(self, leading_three_digits):
96106 format_list = self ._current_metadata .intl_number_format
97107 else :
98108 format_list = self ._current_metadata .number_format
99- self ._possible_formats .extend (format_list )
109+ for this_format in format_list :
110+ if self ._is_format_eligible (this_format .format ):
111+ self ._possible_formats .append (this_format )
100112 self ._narrow_down_possible_formats (leading_three_digits )
101113
114+ def _is_format_eligible (self , format ):
115+ return fullmatch (_ELIGIBLE_FORMAT_PATTERN , format )
116+
102117 def _narrow_down_possible_formats (self , leading_digits ):
103118 index_of_leading_digits_pattern = len (leading_digits ) - _MIN_LEADING_DIGITS_LENGTH
104119 ii = 0
@@ -138,7 +153,7 @@ def _create_formatting_template(self, num_format):
138153 return False
139154
140155 def _get_formatting_template (self , number_pattern , number_format ):
141- """Gets a formatting template which could be used to efficiently
156+ """Gets a formatting template which can be used to efficiently
142157 format a partial number where digits are added one by one."""
143158 # Create a phone number consisting only of the digit 9 that matches the
144159 # number_pattern by applying the pattern to the longest_phone_number string.
@@ -178,6 +193,7 @@ def _clear(self):
178193 self ._possible_formats = []
179194
180195 def clear (self ):
196+ """Clears the internal state of the formatter, so it can be reused."""
181197 self ._clear ()
182198 if self ._current_metadata != self ._default_metadata :
183199 self ._current_metadata = PhoneMetadata .region_metadata .get (self ._default_country , _EMPTY_METADATA )
@@ -186,7 +202,7 @@ def input_digit(self, next_char, remember_position=False):
186202 """Formats a phone number on-the-fly as each digit is entered.
187203
188204 If remember_position is set, remembers the position where next_char is
189- inserted, so that it could be retrieved later by using
205+ inserted, so that it can be retrieved later by using
190206 get_remembered_position. The remembered position will be automatically
191207 adjusted if additional formatting characters are later
192208 inserted/removed in front of next_char.
0 commit comments