Browse Source

Try to optimize for the common ASCII case.

This is still a slowdown (about 45ns to 300ns) but it's less severe
than the original implementation (about 900 to 1100ns).
pull/1887/head
Rapptz 6 years ago
parent
commit
e429763dea
  1. 13
      discord/utils.py

13
discord/utils.py

@ -40,7 +40,6 @@ import warnings
from .errors import InvalidArgument
DISCORD_EPOCH = 1420070400000
UNICODE_WIDE_CHAR_TYPE = u"WFA"
class cached_property:
def __init__(self, function):
@ -327,9 +326,17 @@ class SnowflakeList(array.array):
i = bisect_left(self, element)
return i != len(self) and self[i] == element
def _string_width(string):
_IS_ASCII = re.compile(r'^[\x00-\x7f]+$')
def _string_width(string, *, _IS_ASCII=_IS_ASCII):
"""Returns string's width."""
match = _IS_ASCII.match(string)
if match:
return match.endpos
UNICODE_WIDE_CHAR_TYPE = 'WFA'
width = 0
func = unicodedata.east_asian_width
for char in string:
width += 2 if unicodedata.east_asian_width(char) in UNICODE_WIDE_CHAR_TYPE else 1
width += 2 if func(char) in UNICODE_WIDE_CHAR_TYPE else 1
return width

Loading…
Cancel
Save