Repository navigation
feat(ts): translate str slicing and &str patterns - #106
Conversation
Quest-Claim: 5b26b64b13a140f1
`&s[a..b]` on a `str` or `String` slices by UTF-8 byte offsets and panics with std's messages off a char boundary. `split`, `match_indices`, `contains`, `starts_with`, `trim_start_matches`, `trim_end_matches`, and `rfind` take a `&str` pattern, an empty one included, and `[&str]::join` joins by one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 48ce3ed
No actionable bugs found in the changed areas. The direction is sound: one byte-offset slicing helper preserves the existing JS-string representation, and extending the shared searcher avoids duplicating matching logic. The linear scan is a reasonable simplicity trade-off here rather than adding indexing/cache machinery. I checked UTF-8 boundaries and panic precedence, empty/overlapping string-pattern behavior, iterator consumers, lowering guards, and the differential fixtures; the empty-pattern progress and trim guards address the important termination cases.
Verification limits: source review only; I did not run the compiler, differential suite, or benchmarks. There were no earlier published review findings to reconcile.
From #87's and #106's reports. Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn Co-authored-by: Luke Curley <432854+kixelated@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Problem
#96 made
rfindandmatch_indicesreturn UTF-8 byte offsets, but nothing could use them:&s[a..b]on astrwas refused, and so was everystrmethod given a&strpattern. moq-net'spath/mod.rs,discovery.rs, andlite/compress.rsslice by those offsets and search by&str.Approach
&s[a..b],&s[a..],&s[..b], and&s[..]on astrorStringlower tort.strSlice, which reads the string up to the byte offsets to find their code units. Off a char boundary, past the end, or backwards, it panics with std's message, in std's order of checks,{ch:?}included.split,match_indices,contains(new, for every pattern),starts_with,trim_start_matches,trim_end_matches, andrfindtake a&str,&String, or&&strpattern as its text. A well-formed pattern matches in UTF-16 exactly where its UTF-8 would.&strpattern follows std'sEmptyNeedle: it matches before each char and after the last insplitandmatch_indices, andtrim_*_matchesleave the string as it is. Before this, an empty text pattern looped forever intrim_*. std'sStrSearcheris not double-ended, so a&strsearch only ever runs from one end.[&str]::join(&str)(and[String]) lowers toArray.prototype.join. path/mod.rs calls it 5 times.Differential tests: a new lang op,
strs::slices(op 43, the harness modulus goes to 45). Its patterns are 0 to 3 chars taken from the text itself, so they match, and they are empty a quarter of the time. The text uses the existing alphabet of every UTF-8 width and the chars next to the surrogates. It covers split, match_indices, slices around each match, trims, contains, rfind, the discovery parent walk, compress's cut, join, and slices at arbitrary or near-end offsets that hit every panic. Six hand-made mutants ofrt/str.tseach fail the 50,000-case run: the empty pattern's first match, the panic order, surrogate width, the panic's char span,containsby predicate, and trimming by empty.Census (moq-net)
path/mod.rsdiscovery.rslite/compress.rsAll 12
strslice,&strpattern, and[&str]::joinrefusals are gone: path/mod.rs:124, 130, 156, 193, 218, 263, 380, 414, 488, 739, discovery.rs:310, compress.rs:86. kio is unchanged. What still refuses in those files belongs to other quests or has none:Cow<str>(ts-cow-str),moq_clockandmoq_patterntypes,Path::ne(ts-cmp-defaults), iterators that own a value that drops (ts-drop), andbool::then,iter::successors,usize::from(bool),Hop::to_owned, and anOption<Option<&Entry>>(no quest).Cost of byte offsets
A JS string has no byte index, so a slice costs a scan to its end offset: about 1.5 to 2.5 ns per code unit. I timed moq-shaped paths in headless Chromium (start 2 bytes in, end 3 bytes from the end, both offsets found per call):
No variant wins across moq's path lengths, so this ships the simplest.
just ts benchhas no string workload, so I measured this outside it.Impact
rt.strSliceandrt.strContains. Nothing that translated before changes.Choices made without asking
[&str]::join, which takes a&strseparator rather than a pattern, because path/mod.rs needs it and it costs one match arm.strssub-cases, becausestrsalready uses all 8 of its kinds.[char; 2].&mut s[a..b]on astris still refused.Alternatives
stras UTF-8 bytes would make slices O(1), but every other string operation would then need conversions. Out of scope.Follow-ups
bool::thenanditer::successors, which discovery.rs's parent walk needs next to the slice.just ts bench, if moq's model puts paths on a hot path.Checks
just check,just test(lang differential at 50,000 cases), andjust ts census, all after merging main.(written by Opus 5.5)
🤖 Generated with Claude Code
https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn