Skip to content

feat(ts): translate str slicing and &str patterns - #106

Merged
kixelated merged 4 commits into
mainfrom
quest/m2/ts-str-slice
Oct 27, 2026
Merged

kixelated merged 4 commits into
mainfrom
quest/m2/ts-str-slice

Conversation

@kixelated

@kixelated kixelated commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

#96 made rfind and match_indices return UTF-8 byte offsets, but nothing could use them: &s[a..b] on a str was refused, and so was every str method given a &str pattern. moq-net's path/mod.rs, discovery.rs, and lite/compress.rs slice by those offsets and search by &str.

Approach

  • &s[a..b], &s[a..], &s[..b], and &s[..] on a str or String lower to rt.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, and rfind take a &str, &String, or &&str pattern as its text. A well-formed pattern matches in UTF-16 exactly where its UTF-8 would.
  • An empty &str pattern follows std's EmptyNeedle: it matches before each char and after the last in split and match_indices, and trim_*_matches leave the string as it is. Before this, an empty text pattern looped forever in trim_*. std's StrSearcher is not double-ended, so a &str search only ever runs from one end.
  • [&str]::join(&str) (and [String]) lowers to Array.prototype.join. path/mod.rs calls it 5 times.
  • The lowering maps a search's name to its runtime function by camel case, instead of a table.

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 of rt/str.ts each fail the 50,000-case run: the empty pattern's first match, the panic order, surrogate width, the panic's char span, contains by predicate, and trimming by empty.

Census (moq-net)

Before After
Refusals 11499 11487
path/mod.rs 40 30
discovery.rs 58 57
lite/compress.rs 3 2

All 12 str slice, &str pattern, and [&str]::join refusals 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_clock and moq_pattern types, Path::ne (ts-cmp-defaults), iterators that own a value that drops (ts-drop), and bool::then, iter::successors, usize::from(bool), Hop::to_owned, and an Option<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):

Path units scan (shipped) ASCII runs in a tight loop ASCII regex first
10 20 ns 32 ns 46 ns
17 48 ns 43 ns 62 ns
69 187 ns 122 ns 163 ns
552 1367 ns 884 ns 1175 ns

No variant wins across moq's path lengths, so this ships the simplest. just ts bench has no string workload, so I measured this outside it.

Impact

  • The translator's output: these constructs now translate instead of refusing. New runtime exports rt.strSlice and rt.strContains. Nothing that translated before changes.

Choices made without asking

  • Included [&str]::join, which takes a &str separator rather than a pattern, because path/mod.rs needs it and it costs one match arm.
  • Gave the tests their own harness op rather than more strs sub-cases, because strs already uses all 8 of its kinds.
  • The reject fixture's unsupported pattern is now [char; 2].
  • &mut s[a..b] on a str is still refused.

Alternatives

  • Keeping a str as UTF-8 bytes would make slices O(1), but every other string operation would then need conversions. Out of scope.

Follow-ups

  • bool::then and iter::successors, which discovery.rs's parent walk needs next to the slice.
  • A string workload in just ts bench, if moq's model puts paths on a hot path.

Checks

just check, just test (lang differential at 50,000 cases), and just ts census, all after merging main.

(written by Opus 5.5)

🤖 Generated with Claude Code

https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

kixelated and others added 4 commits October 26, 2026 20:46
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
@kixelated kixelated changed the title str slicing and &str patterns feat(ts): translate str slicing and &str patterns Oct 27, 2026
@kixelated
kixelated marked this pull request as ready for review October 27, 2026 04:23
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T04:25:51.582449Z 48ce3ed Draft marked ready
🔒 Security Review ✅ Completed 2026-10-27T04:27:39.115453Z 48ce3ed Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@kixelated
kixelated merged commit f39c8af into main Oct 27, 2026
1 check passed
kixelated added a commit that referenced this pull request Oct 27, 2026
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>
@kixelated
kixelated deleted the quest/m2/ts-str-slice branch October 27, 2026 20:32
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