Fix #5537: Fix capCodePoints returning nearly the full string when maxCodePoints is zero or negative - #5538
Conversation
…hen maxCodePoints is zero or negative
|
Two small things, one of them semantic. The
It would also be great to pin the boundary with a regression test (cap |
…files, add regression tests - capCodePoints now returns '' when maxCodePoints <= 0, matching the contract 'at most maxCodePoints code points' and sibling helpers truncateUtf16Safe/sanitizeUnicodeText (apache#5537) - Remove empty fork-error.txt and fork-view-error.txt (accidental commits) - Add regression tests for cap 0, -2, and empty string input
|
Thanks for the review, @ggbdpq! Here's what I addressed in the follow-up commit (
|
|
Confirmed on |
…eep capCodePoints fix
|
Resolved merge conflict with
|
Fixes #5537
PR Summary — Fix
capCodePointsfor zero/negative caps (#5537)What changed
packages/core/src/thread-search.ts—capCodePointsnow handlesmaxCodePoints <= 0explicitly:slice(0, maxCodePoints - 1)truncation. Non-empty input with a zero or negative cap now returns just"…", and empty input still returns""(previously""with a negative cap fell through to the slice and incorrectly returned"…").Why it addresses the issue
codePoints.slice(0, maxCodePoints - 1)used-1as the end index for a 0 cap, and a negative end counts back from the array end — socapCodePoints("hello", 0)returned"hell…"(5 code points) instead of"…", completely bypassing the documented "at mostmaxCodePointscode points" bound. Negative caps behaved the same way.sanitizeUnicodeText. Positive-cap behavior is untouched (stillslice(0, max - 1) + '…'), and current production callers passingSNIPPET_MAX_CODE_POINTS = 240are unaffected.Verification
node_modulesabsent), so the@maka/corebuild fails witherror TS2688: Cannot find type definition file for 'node', and thethread-searchtests (which run from built output vianode --test dist/main/**/*.test.js) cannot execute.capCodePointsfunction was extracted verbatim frompackages/core/src/thread-search.tsand evaluated directly withnode -e, covering the issue's repro plus edge cases:"hello" @0 => "…" (1pt)(was"hell…" (5pts)before the fix)"0123456789…"(40 chars) @0 => "…" (1pt)(was 40pts before the fix)"hello" @-2 => "…" (1pt)(was"he…" (3pts)before the fix)"" @0 => "","" @-2 => ""(empty input stays empty)"hello" @1 => "…","hello" @5 => "hello","hello" @10 => "hello"(positive/no-truncation paths unchanged)