Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/_pytest/_io/wcwidth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
from functools import lru_cache
import unicodedata

_Cf_Zp_Zl_SET = {
0x0000,
0x2028, 0x2029, 0x202A, 0x202B, 0x202C, 0x202D, 0x202E,
0x2060, 0x2061, 0x2062, 0x2063,
0x200B, 0x200C, 0x200D, 0x200E, 0x200F,
}

_COMBINING_CATEGORIES = {"Me", "Mn"}

_EAWIDE = {"F", "W"}


@lru_cache(100)
def wcwidth(c: str) -> int:
Expand All @@ -16,12 +27,7 @@ def wcwidth(c: str) -> int:
return 1

# Some Cf/Zp/Zl characters which should be zero-width.
if (
o == 0x0000
or 0x200B <= o <= 0x200F
or 0x2028 <= o <= 0x202E
or 0x2060 <= o <= 0x2063
):
if o in _Cf_Zp_Zl_SET:
return 0

category = unicodedata.category(c)
Expand All @@ -31,11 +37,11 @@ def wcwidth(c: str) -> int:
return -1

# Combining characters with zero width.
if category in ("Me", "Mn"):
if category in _COMBINING_CATEGORIES:
return 0

# Full/Wide east asian characters.
if unicodedata.east_asian_width(c) in ("F", "W"):
if unicodedata.east_asian_width(c) in _EAWIDE:
return 2

return 1
Expand Down