mirror of
https://github.com/python/cpython.git
synced 2025-07-09 20:35:26 +00:00
gh-111201: Speed up paste mode in the REPL (#119341)
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
This commit is contained in:
parent
cd516cd1f5
commit
e6572e8f98
6 changed files with 21 additions and 16 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue