summaryrefslogtreecommitdiffstats
path: root/Lib/_pyrepl/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/_pyrepl/utils.py')
-rw-r--r--Lib/_pyrepl/utils.py8
1 files changed, 6 insertions, 2 deletions
diff --git a/Lib/_pyrepl/utils.py b/Lib/_pyrepl/utils.py
index cd1df7c..96e917e 100644
--- a/Lib/_pyrepl/utils.py
+++ b/Lib/_pyrepl/utils.py
@@ -1,10 +1,14 @@
import re
import unicodedata
+import functools
ANSI_ESCAPE_SEQUENCE = re.compile(r"\x1b\[[ -@]*[A-~]")
+@functools.cache
def str_width(c: str) -> int:
+ if ord(c) < 128:
+ return 1
w = unicodedata.east_asian_width(c)
if w in ('N', 'Na', 'H', 'A'):
return 1
@@ -13,6 +17,6 @@ def str_width(c: str) -> int:
def wlen(s: str) -> int:
length = sum(str_width(i) for i in s)
-
# remove lengths of any escape sequences
- return length - sum(len(i) for i in ANSI_ESCAPE_SEQUENCE.findall(s))
+ sequence = ANSI_ESCAPE_SEQUENCE.findall(s)
+ return length - sum(len(i) for i in sequence)