Skip to content

Commit 4ddf024

Browse files
committed
Move str_width and wlen to the place in the file where they were before
1 parent 7b3fa87 commit 4ddf024

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

Lib/_pyrepl/utils.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,6 @@
2525
BUILTINS = frozenset({str(name) for name in dir(builtins) if not name.startswith('_')})
2626

2727

28-
@functools.cache
29-
def str_width(c: str) -> int:
30-
if ord(c) < 128:
31-
return 1
32-
# gh-139246 for zero-width joiner and combining characters
33-
if unicodedata.combining(c):
34-
return 0
35-
category = unicodedata.category(c)
36-
if category == "Cf" and c != "\u00ad":
37-
return 0
38-
w = unicodedata.east_asian_width(c)
39-
if w in ("N", "Na", "H", "A"):
40-
return 1
41-
return 2
42-
43-
44-
def wlen(s: str) -> int:
45-
if len(s) == 1 and s != "\x1a":
46-
return str_width(s)
47-
length = sum(str_width(i) for i in s)
48-
# remove lengths of any escape sequences
49-
sequence = ANSI_ESCAPE_SEQUENCE.findall(s)
50-
ctrl_z_cnt = s.count("\x1a")
51-
return length - sum(len(i) for i in sequence) + ctrl_z_cnt
52-
53-
5428
def THEME(**kwargs):
5529
# Not cached: the user can modify the theme inside the interactive session.
5630
return _colorize.get_theme(**kwargs).syntax
@@ -86,6 +60,34 @@ class ColorSpan(NamedTuple):
8660
tag: str
8761

8862

63+
@functools.cache
64+
def str_width(c: str) -> int:
65+
if ord(c) < 128:
66+
return 1
67+
# gh-139246 for zero-width joiner and combining characters
68+
if unicodedata.combining(c):
69+
return 0
70+
category = unicodedata.category(c)
71+
if category == "Cf" and c != "\u00ad":
72+
return 0
73+
w = unicodedata.east_asian_width(c)
74+
if w in ("N", "Na", "H", "A"):
75+
return 1
76+
return 2
77+
78+
79+
def wlen(s: str) -> int:
80+
if len(s) == 1 and s != "\x1a":
81+
return str_width(s)
82+
length = sum(str_width(i) for i in s)
83+
# remove lengths of any escape sequences
84+
sequence = ANSI_ESCAPE_SEQUENCE.findall(s)
85+
ctrl_z_cnt = s.count("\x1a")
86+
return length - sum(len(i) for i in sequence) + ctrl_z_cnt
87+
88+
89+
90+
8991
def unbracket(s: str, including_content: bool = False) -> str:
9092
r"""Return `s` with \001 and \002 characters removed.
9193

0 commit comments

Comments
 (0)