summaryrefslogtreecommitdiffstats
path: root/Lib/_pyrepl/utils.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-05-23 04:23:40 (GMT)
committerGitHub <noreply@github.com>2024-05-23 04:23:40 (GMT)
commit58dbb4a4b053800bb802aaf9662e3977ad114a52 (patch)
treef6be64041fb52f2669e5e8916ef7941cbf3ff77d /Lib/_pyrepl/utils.py
parent9fa1b4fc466e9857210fd6e87f1cbf0c234886ee (diff)
downloadcpython-58dbb4a4b053800bb802aaf9662e3977ad114a52.zip
cpython-58dbb4a4b053800bb802aaf9662e3977ad114a52.tar.gz
cpython-58dbb4a4b053800bb802aaf9662e3977ad114a52.tar.bz2
[3.13] gh-111201: Speed up paste mode in the REPL (#119341) (GH-119432) (#119439)
(cherry picked from commit e6572e8f98d33994d2d0dd3afa92a2a72ee642a9) Also includes: * gh-111201: Use calc_complete_screen after bracketed paste in PyREPL (GH-119432) (cherry picked from commit 14b063cbf1bb11a489d04a31f277edba0fc8893c) Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com> Co-authored-by: Ɓukasz Langa <lukasz@langa.pl> Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
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)