Repository navigation
bugfix clip() painter's algo and charset designator - #259
Merged
Merged
Conversation
wcwidth_clip_u8() answered differently from Python's clip() for three kinds of sequence it does not support, rather than saying so. It now returns NULL with WCWIDTH_ERROR_UNSUPPORTED for: - horizontal cursor movement: BS, CR, CUF, CUB and HPA - OSC 8 hyperlinks, which it measures but never rewrites - OSC 66 text sizing, which it never clips as a unit Movement is found by a scan at entry, because a CUB or HPA to the right of the window still rewinds into it and clip_run() stops at the first ordinary character past v_end. The two OSC forms are reported from clip_run() itself, where reaching one is the only way it can affect the result. Also fixes a NULL dereference: clip_run() writes through *error unconditionally, but error is documented as optional.
ZERO_WIDTH_PATTERN read a character set designation as ESC, '(' or ')',
and '.', which does not match a newline outside DOTALL. ESC ( LF was
left as text and measured one cell wide, where libwcwidth consumes the
three bytes as a sequence. Every final byte ECMA-48 permits there
already agreed; only the C0 controls did not.
strip_sequences() substituted OSC 66 in a pass of its own before the
other sequences, so removing one joined the text on either side: an ESC
to the left of an OSC 66 then introduced whatever followed it, and the
second pass stripped a sequence that was never in the input. Both now
match in one alternation, whose OSC 66 fragment _SEQUENCE_CLASSIFY
shares rather than repeating.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #259 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 27 27
Lines 1982 2003 +21
Branches 463 469 +6
=========================================
+ Hits 1982 2003 +21 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Merging this PR will regress 2 benchmarks
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing |
_clip_painter() left the loop as soon as the write column passed the
window, which was wrong three ways:
- Cursor movement to the right of the window still rewinds into it, so
with propagate_sgr=True the exit ran before the movement was seen and
clip('abcdef\x1b[6DZZZ', 0, 3) answered 'abc' instead of 'ZZZ'.
- A tab or CR past the window ended the scan, dropping the sequences
that followed it. These are exempt alongside ESC now, as they are in
_clip_simple().
A tab also expanded with fillchar, which is for a split wide grapheme,
rather than a space; and a zero-width write was stored as a cell, where
the next write overwrote it -- an OSC 66 unit could vanish.
libwcwidth has not been released, so its changes have no audience for a history entry. Drops everything that came in with #253, and the link targets left unreferenced by their removal.
jquast
force-pushed
the
jq/clip-fixes
branch
from
September 17, 2026 19:35
395c1f5 to
2278cc1
Compare
The early exit resolved "is there movement ahead" once and kept the
answer, so any text containing movement was scanned to the end even
where every movement was already behind the scan position:
clip('ab\x1b[1D' + 'x' * 5000, 0, 3) went from 11us to 54us for an
unchanged result.
Keeping the position of the next movement rather than a flag lets the
scan stop as soon as it is passed, and an rfind for the three bytes any
movement must start with settles the common case ~600x cheaper than
scanning the remainder for a movement that is not there.
Text whose movement really is ahead still scans to reach it; that is
what makes the answer right.
Cut the explanatory comments added by this branch down to what they need to say, in C and Python alike, and shorten the paragraph under wcwidth_clip_u8() to one line. Regenerates api_c.rst from clip.h.
jquast
marked this pull request as ready for review
September 17, 2026 20:36
Owner
Author
|
The performance regression is worth it, for correcting (see before and after in description) |
jquast
force-pushed
the
jq/clip-fixes
branch
from
September 17, 2026 20:37
04bf8b0 to
fa17585
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bugfix
width(),ljust(),rjust(),center(),clip()_ andstrip_sequences()_treated
ESC ( LFas text instead of a character set designation (rare)Bugfix
clip()_ painter's algorithm, and, "tab expansion" should use " " instead of fillchar.