Commit 49c5e74
committed
fix: clamp n to len(pwcs) in wcswidth/wcstwidth to avoid IndexError
Passing n > len(pwcs) to wcswidth() or wcstwidth() causes an
IndexError when the loop index reaches the end of the string, because
`end` was set to n directly.
Fix: `end = min(n, len(pwcs))` so that n larger than the string length
is treated the same as measuring the whole string, which is the natural
Python semantics for sized strings.
Adds a regression test covering ASCII, wide characters, and ZWJ clusters.1 parent d1c99fe commit 49c5e74
2 files changed
Lines changed: 18 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
497 | 497 | | |
498 | 498 | | |
499 | 499 | | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
500 | 516 | | |
501 | 517 | | |
502 | 518 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
101 | | - | |
| 101 | + | |
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
| |||
262 | 262 | | |
263 | 263 | | |
264 | 264 | | |
265 | | - | |
| 265 | + | |
266 | 266 | | |
267 | 267 | | |
268 | 268 | | |
| |||
0 commit comments