Skip to content

Fix VS15 applying more than once to the same base character - #241

Closed
afonsojanu wants to merge 2 commits into
jquast:masterfrom
afonsojanu:fix-vs15-repeated-narrowing
Closed

afonsojanu wants to merge 2 commits into
jquast:masterfrom
afonsojanu:fix-vs15-repeated-narrowing

Conversation

@afonsojanu

Copy link
Copy Markdown
Contributor

VS16 handling clears last_measured_idx right after it adjusts the pending
cluster width, and the comment there says why: it stops a second VS16 from
reapplying to the same base character. The VS15 branch just below it does
the same kind of adjustment (narrowing a wide base character by one column)
but never got that same reset.

So a base character followed by two VS15 selectors in a row gets narrowed
twice, and three in a row narrows it three times, even though VS15 requests
are supposed to be idempotent, applying "text presentation" once, the same
way applying it twice shouldn't ask for anything different than applying it
once.

>>> from wcwidth import width
>>> width('⌚')                    # WATCH, default emoji presentation
2
>>> width('⌚︎')               # one VS15, narrowed as expected
1
>>> width('⌚︎︎')         # two VS15, still narrow width 1... but:
0
>>> width('⌚︎︎' + 'x')   # and it costs the next character a column
1

A wide character can only lose one column going from wide to narrow, so
every VS15 past the first is quietly stealing a column that belongs to
whatever follows it in the string, which is how this shows up in practice:
not as a standalone measurement being off, but as everything after a
malformed run of variation selectors drifting out of alignment.

This same handling is duplicated across width(), wcswidth(), and
wcstwidth(), and all three had the identical gap, so all three needed the
one-line fix. I added a test exercising all three, including the case
where a trailing character follows the repeated selectors, since that's
where the lost column actually becomes visible.

Ran the full test suite locally (pytest tests/) and it's green, 1263
passed.

VS16 clears last_measured_idx once it has adjusted the pending cluster
width, specifically to stop a second VS16 from reapplying to the same
base character. The VS15 branch right below it does the same kind of
adjustment but never got that reset, so a base character followed by
two VS15 selectors in a row gets narrowed twice instead of once, and
three in a row narrows it three times. Since a wide character can only
lose one column going from wide to narrow, every VS15 past the first
one is quietly stealing a column that belongs to whatever comes next
in the string.

This affected width(), wcswidth(), and wcstwidth(), each of which
carries its own copy of the VS15 handling. Added a test covering all
three plus the case where a trailing character follows the repeated
selectors, since that's where the corruption actually becomes visible
downstream.
@jquast

jquast commented Sep 14, 2026 •

Copy link
Copy Markdown
Owner

Thanks, I consider these "glitch unicode", they do not occur naturally in the wild except by a kind of /dev/urandom (or fuzzing) of codepoints. This library generally takes a stance against trying to determine accurate measurements of "non-recommended sequences", especially if it hurts performance.

In this case, the index is easy to update (its a one line change), and we're already in VS-15 branch conditional so it is not harmful for general performance. Note that VS-15 behavior is also contested, #234

@codecov

codecov Bot commented Sep 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (17986f5) to head (38d138e).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #241   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           27        27           
  Lines         1976      1979    +3     
  Branches       463       463           
=========================================
+ Hits          1976      1979    +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed

codspeed Bot commented Sep 14, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 108 untouched benchmarks


Comparing afonsojanu:fix-vs15-repeated-narrowing (38d138e) with master (17986f5)

Open in CodSpeed

@jquast

jquast commented Sep 15, 2026

Copy link
Copy Markdown
Owner

Looking more, this change is not conforming to any known unicode standard, an errant/extra vs15 or other combining character is already split or determined by grapheme clustering.

This is only accepted because it is so small and otherwise harmless.

@jquast jquast closed this in #244 Sep 15, 2026
hugovk pushed a commit to hugovk/wcwidth that referenced this pull request Sep 30, 2026
Do not re-apply "narrowing" of a base character with repeated VS-15. Closes jquast#241.

This change is not conforming to any known unicode standard, an errant/extra vs15 or other combining
character is already split or determined by grapheme clustering.  This is only accepted because it
is so small and otherwise harmless.

I consider these "glitch unicode", they do not occur naturally in the wild except by a kind of
``/dev/urandom`` (or fuzzing) of codepoints. This library generally takes a stance against trying to
determine accurate measurements of "non-recommended sequences", especially if it hurts performance.

In this case, the index is easy to update (its a one line change), and we're already in VS-15 branch
conditional so it is not harmful for general performance. Note that VS-15 behavior is also
contested, jquast#234

---------

Co-authored-by: Afonso Januário <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants