Skip to content
Closed
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,23 @@ def test_zwj_at_end_of_string():
assert wcwidth.wcswidth('a\u200D') == 1


def test_wcswidth_n_exceeds_length():
"""
Wcswidth() with n > len(string) returns same as n=None.

Passing n larger than the string length should not raise IndexError; it should behave
identically to measuring the whole string.
"""
# ASCII string
assert wcwidth.wcswidth('hello', 10) == 5
# Wide characters
assert wcwidth.wcswidth('\u30B3\u30F3', 5) == 4
# ZWJ cluster
family = '\U0001F468\u200D\U0001F469\u200D\U0001F467'
assert wcwidth.wcswidth(family, len(family) + 1) == wcwidth.wcswidth(family)
assert wcwidth.wcstwidth(family, len(family) + 1) == wcwidth.wcstwidth(family)


def test_soft_hyphen():
# Test SOFT HYPHEN, category 'Cf' usually are zero-width, but most
# implementations agree to draw it was '1' cell, visually
Expand Down
7 changes: 7 additions & 0 deletions tests/test_term_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,13 @@ def test_get_term_overrides_narrow_wider_still_empty():
assert overrides.narrow_wider == ()


@pytest.mark.parametrize('func', [wcwidth.wcstwidth, wcwidth.width])
def test_narrow_wider_width(func):
"""Width() matches wcstwidth() for narrow_wider overrides."""
assert wcwidth.wcswidth('\u261d') == 1
assert func('\u261d', term_program='kitty') == 2


@pytest.mark.parametrize('codepoint', [
'\u00ad',
'\u0600',
Expand Down
4 changes: 2 additions & 2 deletions wcwidth/_wcswidth.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def wcswidth(

_wcwidth = wcwidth if ambiguous_width == 1 else lambda c: wcwidth(c, 'auto', ambiguous_width)

end = len(pwcs) if n is None else n
end = len(pwcs) if n is None else min(n, len(pwcs))
total_width = 0
idx = 0

Expand Down Expand Up @@ -262,7 +262,7 @@ def wcstwidth(
# Select wcwidth call pattern for best lru_cache performance
_wcwidth = wcwidth if ambiguous_width == 1 else lambda c: wcwidth(c, 'auto', ambiguous_width)

end = len(pwcs) if n is None else n
end = len(pwcs) if n is None else min(n, len(pwcs))
total_width = 0
idx = 0

Expand Down