Skip to content

bugfix clip() painter's algo and charset designator - #259

Merged
jquast merged 7 commits into
masterfrom
jq/clip-fixes
Sep 17, 2026
Merged

jquast merged 7 commits into
masterfrom
jq/clip-fixes

Conversation

@jquast

@jquast jquast commented Sep 17, 2026 •

Copy link
Copy Markdown
Owner
  • Bugfix width(), ljust(), rjust(), center(), clip()_ and strip_sequences()_
    treated ESC ( LF as text instead of a character set designation (rare)

  • Bugfix clip()_ painter's algorithm, and, "tab expansion" should use " " instead of fillchar.

text = 'hello\x1b[2Dxy ' * 20
master : '...helxy helxy hell'    # wrong
branch : '...helxy helxy helx'    # correct, 'x' that overwrote the 'l'

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

codecov Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Codecov Report

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

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.
📢 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 17, 2026 •

Copy link
Copy Markdown

Merging this PR will regress 2 benchmarks

⚡ 1 improved benchmark
❌ 2 regressed benchmarks
✅ 116 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_clip_cursor_csi_backward 829.2 µs 2,063 µs -59.81%
❌ test_clip_dense_ansi_overtype 662.8 µs 897.3 µs -26.13%
⚡ test_clip_long_cjk_overtype 11,585.2 µs 612.5 µs ×19

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing jq/clip-fixes (fa17585) with master (69d5c1e)

Open in CodSpeed

_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.
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.
@jquast jquast changed the title bugfix clip() painter's algo and charset designator (rare) bugfix clip() painter's algo and charset designator Sep 17, 2026
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
jquast marked this pull request as ready for review September 17, 2026 20:36
@jquast

jquast commented Sep 17, 2026

Copy link
Copy Markdown
Owner Author

The performance regression is worth it, for correcting (see before and after in description)

@jquast
jquast merged commit b088450 into master Sep 17, 2026
33 checks passed
@jquast
jquast deleted the jq/clip-fixes branch September 17, 2026 20:40
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.

1 participant