Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion python/phonenumbers/phonenumberutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,10 @@
# which nets down to: >=1 non-Number, non-Letter, non-# characters at string end
# In Python Unicode regexp mode '(?u)', the class '[^#\w]' will match anything
# that is not # and is not alphanumeric and is not underscore.
_UNWANTED_END_CHARS = u(r"(?u)(?:_|[^#\w])+$")
# The negative lookbehind only lets a match start at the beginning of a run
# of these characters; without it, search() retries from every position in a
# run that is not at the end of the string, which is quadratic in its length.
_UNWANTED_END_CHARS = u(r"(?u)(?<!_|[^#\w])(?:_|[^#\w])+$")
_UNWANTED_END_CHAR_PATTERN = re.compile(_UNWANTED_END_CHARS)

# We use this pattern to check if the phone number has at least three letters
Expand Down
10 changes: 10 additions & 0 deletions python/tests/shortnumberinfotest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# limitations under the License.

import sys
import time

import phonenumbers
from phonenumbers import connects_to_emergency_number, is_emergency_number, ShortNumberCost
Expand Down Expand Up @@ -269,6 +270,15 @@ def testIsEmergencyNumberLongNumber_BR(self):
self.assertFalse(is_emergency_number("1900", "BR"))
self.assertFalse(is_emergency_number("9996", "BR"))

def testEmergencyNumberLongSeparatorRun(self):
# Python version extra test: a long run of separators that isn't at
# the end of the input used to take quadratic time to strip.
number = "911" + " " * 20000 + "1"
start = time.time()
self.assertFalse(is_emergency_number(number, "US"))
self.assertTrue(connects_to_emergency_number(number, "US"))
self.assertTrue(time.time() - start < 1)

def testIsEmergencyNumber_AO(self):
# Angola doesn't have any metadata for emergency numbers in the test metadata.
self.assertFalse(is_emergency_number("911", "AO"))
Expand Down
Loading