From 41dc0cfd20e35e82ba181a2c7087df315560e980 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 26 Oct 2026 20:46:30 -0700 Subject: [PATCH 1/3] chore(quest): claim quest/m2/ts-str-slice.md Quest-Claim: 5b26b64b13a140f1 From aab46e0daa469c835c3378658d69877290f1d98e Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 26 Oct 2026 21:12:22 -0700 Subject: [PATCH 2/3] feat(ts): translate str slicing and &str patterns `&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 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- quest/m2/README.md | 1 - quest/m2/ts-str-slice.md | 20 ---- ts/README.md | 21 +++-- ts/fixtures/lang/expected/harness.ts | 6 +- ts/fixtures/lang/expected/strs.ts | 132 +++++++++++++++++++++++++++ ts/fixtures/lang/src/harness.rs | 3 +- ts/fixtures/lang/src/strs.rs | 104 ++++++++++++++++++++- ts/fixtures/reject/census.md | 6 +- ts/fixtures/reject/expected.txt | 2 +- ts/fixtures/reject/src/lib.rs | 7 +- ts/fixtures/reject/ts.toml | 2 +- ts/runtime/rt/str.ts | 95 +++++++++++++++++-- ts/src/lower/library.rs | 30 +++--- ts/src/lower/mir/call.rs | 11 +++ 14 files changed, 374 insertions(+), 66 deletions(-) delete mode 100644 quest/m2/ts-str-slice.md diff --git a/quest/m2/README.md b/quest/m2/README.md index 85b9233b7..a24caf238 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -38,7 +38,6 @@ The fork's feature quests folded into the [exporter](/quest/m1/ffi-export.md); - [Output tsc accepts for writes through Result and Poll compares](/quest/m2/ts-tsc-shapes.md) - two shapes whose output fails `tsc` type-check - [Default comparison methods](/quest/m2/ts-cmp-defaults.md) - inherited `ne`, `lt`, `le`, `gt`, `ge` and `[u8]` equality translate - [Copy-on-write strings](/quest/m2/ts-cow-str.md) - `Cow` translates, finishing `ietf/namespace.rs` -- [str slicing and &str patterns](/quest/m2/ts-str-slice.md) - `&s[a..b]` and `&str` patterns translate, so `rfind`'s offsets are usable - [Translate moq-net's model to TypeScript](/quest/m2/ts-model.md) - the driver grows from moq-net's codec to its model, for moq to compare against WASM - [Waiting: Swift 6.4 in nixpkgs-unstable](/quest/m2/uniffi-swift-arm.md) - the nightly UniFFI fixtures move back to arm - [Waiting: ubrn reads UniFFI 0.32](/quest/m2/ubrn-release.md) - the binding benchmark's browser runner pins a ubrn release, not an open PR diff --git a/quest/m2/ts-str-slice.md b/quest/m2/ts-str-slice.md deleted file mode 100644 index e2f7d7a2d..000000000 --- a/quest/m2/ts-str-slice.md +++ /dev/null @@ -1,20 +0,0 @@ -# [S] str slicing and &str patterns - -## Goal - -rust2-ts translates `&s[a..b]` on a `str` and the `str` methods that take a `&str` -pattern, so moq-net's `path/mod.rs`, `discovery.rs`, and `lite/compress.rs` translate, -proven by differential tests against native Rust. - -## Plan - -Found by [#96](https://github.com/kixelated/rust2/pull/96) on 2026-10-27, which -translated `char` patterns and returns UTF-8 byte offsets from `rfind` and -`match_indices`. Slicing is what lets those offsets be used. - -- Slice by UTF-8 byte offsets and panic, as std does, on a boundary inside a char. -- An empty `&str` pattern matches between every char in `split`, `trim_*`, and - `match_indices`; cover it and multi-byte patterns in the differential tests. -- Report moq-net's census before and after. - -Public API: the translator's output. diff --git a/ts/README.md b/ts/README.md index d01719fd7..ea3b12543 100644 --- a/ts/README.md +++ b/ts/README.md @@ -129,11 +129,12 @@ std's adaptors through `rt.iterFrom`. A caller calls `next()` for one item, or i an `rt.Iter` with `for ... of`. A `str` is a JS string, which is UTF-16: `chars` steps by code point, and a search by a -`char` or a predicate, as in `rt.strSplit(s, "/")`, takes the char's text or the closure, -which it calls on each char it reads, in the order std reads them. An offset it returns, -from `rfind` or `match_indices`, counts UTF-8 bytes, as Rust's does. A JS string can't -change in place, so `String::push` assigns a longer one to the local or field the `&mut` -borrows. +`char`, a `&str`, or a predicate, as in `rt.strSplit(s, "/")`, takes the text or the +closure, which it calls on each char it reads, in the order std reads them. An offset it +returns, from `rfind` or `match_indices`, counts UTF-8 bytes, as Rust's does, and so does +one `&s[a..b]` takes, which reads the string up to it and panics as std does inside a +char. A JS string can't change in place, so `String::push` assigns a longer one to the +local or field the `&mut` borrows. Functions and members are camelCase. Functions are monomorphized: each instance is its own function, suffixed after a `$` by its trait, then by its type's type arguments and @@ -323,11 +324,11 @@ without a mapping in `src/lower/iter.rs` (such as `cycle`, `unzip`, and `partiti own `impl Iterator` with `next_back`, `len`, or another provided method of its own, other format specs (a precision, `+`, `{:b}`, `{:x?}`, and widths from arguments), writing to a `String` with `write!` or pushing to one through a parameter, a `str` pattern other than a -`char` or a predicate, a copy of a closure that changes what it captured, `mem::swap`, -`replace`, or `take` of a reference's whole pointee, assigning an `Arc`, a waker, a -`MutexGuard`, a pinned reference, or a future through a reference, a switch on or a -field of another crate's type without a mapping, and std functions without a mapping in -`src/lower/library.rs`. +`char`, a `&str`, or a predicate, a mutable `str` slice, a copy of a closure that changes +what it captured, `mem::swap`, `replace`, or `take` of a reference's whole pointee, +assigning an `Arc`, a waker, a `MutexGuard`, a pinned reference, or a future through a +reference, a switch on or a field of another crate's type without a mapping, and std +functions without a mapping in `src/lower/library.rs`. ## Tests diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index 3d9d0d683..645417dc4 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -9,7 +9,7 @@ import { u8Op, u16Op, u32Op, u64Op, u128Op, usizeOp, i8Op, i16Op, i32Op, i64Op, import { adaptors, consumers, ranges, options, own, values } from "./iters.ts"; import { keys } from "./keys.ts"; import { copies, order, discriminants, loops, patterns, closures, library, flows } from "./shapes.ts"; -import { strs } from "./strs.ts"; +import { strs, slices } from "./strs.ts"; /** Run one case. */ export function run(input: rt.ByteSlice): rt.ByteVec { @@ -20,7 +20,7 @@ export function run(input: rt.ByteSlice): rt.ByteVec { const _10: [rt.u8, rt.u8] = [input.data[input.start], input.data[input.start + 1]]; const op: rt.u8 = _10[0]; const kind: rt.u8 = _10[1]; - const _14: rt.u8 = rt.u8Rem(op, 44); + const _14: rt.u8 = rt.u8Rem(op, 45); if (_14 === 0) { u8Op(kind, rt.byteSliceRange(input, 2), out); } else if (_14 === 1) { @@ -125,6 +125,8 @@ export function run(input: rt.ByteSlice): rt.ByteVec { strs(kind, rt.byteSliceRange(input, 2), out); } else if (_14 === 42) { vecs(rt.byteSliceRange(input, 2), out); + } else if (_14 === 43) { + slices(kind, rt.byteSliceRange(input, 2), out); } else { stores(rt.byteSliceRange(input, 2), out); } diff --git a/ts/fixtures/lang/expected/strs.ts b/ts/fixtures/lang/expected/strs.ts index 06d8d4442..7769e63b7 100644 --- a/ts/fixtures/lang/expected/strs.ts +++ b/ts/fixtures/lang/expected/strs.ts @@ -274,6 +274,138 @@ export function strs(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { } } +/** + * Up to three chars of `s`, from the char the kind's high bits pick: empty a quarter of + * the time, and otherwise found at least once unless the text is shorter. + */ +export function pattern(kind: rt.u8, s: string): string { + let p: string = ""; + const iter: rt.Iter = rt.iterTake(rt.iterSkip(rt.strChars(s), rt.u8Div(kind, 32) as rt.usize), rt.u8Rem(rt.u8Div(kind, 8), 4) as rt.usize); + while (true) { + const _17: rt.char | rt.Done = iter.next(); + if (_17 === rt.DONE) { + return p; + } + p = p + String.fromCodePoint(_17); + } +} + +/** + * `&s[a..b]` by byte offsets, and the `str` methods by a `&str` pattern, which an empty + * one matches before each char and after the last. + */ +export function slices(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + let _198!: [rt.usize, rt.usize]; + let sliced!: string; + const s: string = text(data); + const _6: string = s; + const p: string = pattern(kind, _6); + b0: { + const _8: rt.u8 = rt.u8Rem(kind, 8); + if (_8 === 0) { + let n: rt.i32 = 0; + const iter: rt.Iter = rt.strSplit(s, p); + while (true) { + const _18: string | rt.Done = iter.next(); + if (_18 === rt.DONE) { + break; + } + put(out, _18); + n = rt.i32Add(n, 1); + } + rt.byteVecPush(out, n & 0xff); + put(out, rt.optionUnwrapOr(rt.iterLast(rt.strSplit(s, p)), "none")); + } else { + if (_8 === 1) { + const iter2: rt.Iter<[rt.usize, string]> = rt.strMatchIndices(s, p); + while (true) { + const _42: [rt.usize, string] | rt.Done = iter2.next(); + if (_42 === rt.DONE) { + break b0; + } + const at: rt.usize = _42[0]; + putOffset(out, at); + put(out, _42[1]); + put(out, rt.strSlice(s, 0, at)); + const part5 = rt.usizeAdd(at, rt.stringLen(_42[1])); + put(out, rt.strSlice(s, part5)); + const part6 = rt.usizeAdd(at, rt.stringLen(_42[1])); + put(out, rt.strSlice(s, at, part6)); + } + } + if (_8 === 2) { + put(out, rt.strTrimStartMatches(s, p)); + put(out, rt.strTrimEndMatches(s, p)); + put(out, rt.strTrimEndMatches(rt.strTrimStartMatches(s, p), p)); + rt.byteVecPush(out, rt.strStartsWith(s, p) ? 1 : 0); + rt.byteVecPush(out, rt.strContains(s, p) ? 1 : 0); + rt.byteVecPush(out, rt.strContains(s, p) ? 1 : 0); + rt.byteVecPush(out, rt.strContains(s, "/") ? 1 : 0); + rt.byteVecPush(out, rt.strContains(s, (c: rt.char): boolean => { + return rt.charIsAsciiDigit(c); + }) ? 1 : 0); + putOffset(out, rt.strRfind(s, p)); + } else if (_8 === 3) { + let path: string = s; + while (true) { + const _125: rt.usize | undefined = rt.strRfind(path, "/"); + if (_125 !== undefined) { + const _130: string = path; + path = rt.strSlice(_130, 0, _125); + put(out, path); + } else { + break; + } + } + put(out, path); + } else if (_8 === 4) { + const keep: rt.usize = rt.usizeAdd(rt.u8Rem(rt.u8Div(kind, 8), 4) as rt.usize, 1); + const _146: rt.Iter<[rt.usize, string]> = rt.strMatchIndices(s, "/"); + const _149: rt.usize = rt.usizeSub(keep, 1); + put(out, rt.optionUnwrapOr(rt.optionMap(rt.iterNth(_146, _149), (_2: [rt.usize, string]): string => { + return rt.strSlice(s, 0, _2[0]); + }), "none")); + const _159: rt.Iter<[rt.usize, string]> = rt.strMatchIndices(s, "/"); + const _162: rt.usize = rt.usizeSub(keep, 1); + const part4 = rt.optionMapOr(rt.iterNth(_159, _162), rt.stringLen(s), (_22: [rt.usize, string]): rt.usize => { + return rt.usizeAdd(_22[0], 1); + }); + put(out, rt.strSlice(s, part4)); + } else if (_8 === 5) { + put(out, rt.iterToArray(rt.strSplit(s, "/")).join(p)); + put(out, ["a", "", s].join("/")); + } else { + const at2: (a0: rt.u8) => rt.usize = (b2: rt.u8): rt.usize => { + if ((kind & 32) === 0) { + return rt.usizeRem(b2 as rt.usize, rt.usizeAdd(rt.stringLen(s), 2)); + } + return rt.usizeSaturatingSub(rt.usizeAdd(rt.stringLen(s), 1), rt.usizeRem(b2 as rt.usize, 4)); + }; + if (!(data.length >= 2)) { + _198 = [0, 0] as [rt.usize, rt.usize]; + } else { + _198 = [at2(data.data[data.start]), at2(data.data[data.start + 1])] as [rt.usize, rt.usize]; + } + const a: rt.usize = _198[0]; + const b: rt.usize = _198[1]; + const _212: rt.u8 = rt.u8Rem(rt.u8Div(kind, 8), 4); + if (_212 === 0) { + sliced = rt.strSlice(s, a, b); + } else if (_212 === 1) { + sliced = rt.strSlice(s, a); + } else if (_212 === 2) { + const _223: string = s; + sliced = rt.strSlice(_223, 0, b); + } else { + const _227: string = s; + sliced = rt.strSlice(_227, 0); + } + put(out, sliced); + } + } + } +} + /** A predicate that is a function, not a closure. */ export function digit(c: rt.char): boolean { return rt.charIsAsciiDigit(c); diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index 5430d8c80..bbc991429 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -19,7 +19,7 @@ pub fn run(input: &[u8]) -> Vec { return out; }; let (op, kind) = (*op, *kind); - match op % 44 { + match op % 45 { 0 => ints::u8_op(kind, data, &mut out), 1 => ints::u16_op(kind, data, &mut out), 2 => ints::u32_op(kind, data, &mut out), @@ -73,6 +73,7 @@ pub fn run(input: &[u8]) -> Vec { 40 => chars::chars(kind, data, &mut out), 41 => strs::strs(kind, data, &mut out), 42 => collections::vecs(data, &mut out), + 43 => strs::slices(kind, data, &mut out), _ => collections::stores(data, &mut out), } out diff --git a/ts/fixtures/lang/src/strs.rs b/ts/fixtures/lang/src/strs.rs index 48a5c1c34..35268f046 100644 --- a/ts/fixtures/lang/src/strs.rs +++ b/ts/fixtures/lang/src/strs.rs @@ -1,7 +1,8 @@ -//! `str` and `String` over `char`: iterated from both ends, searched by a `char` or a -//! predicate, trimmed, split, and built a char at a time. Text is made of chars of every -//! UTF-8 width and those next to UTF-16's surrogates, so byte offsets and code units part. -//! Predicates log each char they are called with, so where a search stops is output too. +//! `str` and `String` over `char`: iterated from both ends, searched by a `char`, a +//! predicate, or a `&str`, trimmed, split, sliced by byte offsets, and built a char at a +//! time. Text is made of chars of every UTF-8 width and those next to UTF-16's surrogates, +//! so byte offsets and code units part. Predicates log each char they are called with, so +//! where a search stops is output too. use crate::fmt::put; @@ -225,6 +226,101 @@ pub fn strs(kind: u8, data: &[u8], out: &mut Vec) { } } +/// Up to three chars of `s`, from the char the kind's high bits pick: empty a quarter of +/// the time, and otherwise found at least once unless the text is shorter. +fn pattern(kind: u8, s: &str) -> String { + let mut p = String::new(); + for c in s.chars().skip((kind / 32) as usize).take((kind / 8 % 4) as usize) { + p.push(c); + } + p +} + +/// `&s[a..b]` by byte offsets, and the `str` methods by a `&str` pattern, which an empty +/// one matches before each char and after the last. +pub fn slices(kind: u8, data: &[u8], out: &mut Vec) { + let s = text(data); + let p = pattern(kind, &s); + match kind % 8 { + 0 => { + // `split`, by a `&str` and by a `&String`. + let mut n = 0; + for part in s.split(p.as_str()) { + put(out, part); + n += 1; + } + out.push(n as u8); + put(out, s.split(&p).last().unwrap_or("none")); + } + 1 => { + // `match_indices`: byte offsets, then the text before and after each match. + for (at, matched) in s.match_indices(p.as_str()) { + put_offset(out, Some(at)); + put(out, matched); + put(out, &s[..at]); + put(out, &s[at + matched.len()..]); + put(out, &s[at..at + matched.len()]); + } + } + 2 => { + // Trimmed, tested, and found. + put(out, s.trim_start_matches(p.as_str())); + put(out, s.trim_end_matches(p.as_str())); + put(out, s.trim_start_matches(&p).trim_end_matches(&p)); + out.push(s.starts_with(p.as_str()) as u8); + out.push(s.contains(p.as_str()) as u8); + out.push(s.contains(&p) as u8); + out.push(s.contains('/') as u8); + out.push(s.contains(|c: char| c.is_ascii_digit()) as u8); + put_offset(out, s.rfind(p.as_str())); + } + 3 => { + // Each parent of a path, as moq-net's discovery walks them. + let mut path = s.as_str(); + while let Some(slash) = path.rfind('/') { + path = &path[..slash]; + put(out, path); + } + put(out, path); + } + 4 => { + // A path cut after its `keep`th part, and the rest, as moq-net's compression + // cuts one. + let keep = (kind / 8 % 4) as usize + 1; + let head = s.match_indices('/').nth(keep - 1).map(|(end, _)| &s[..end]); + put(out, head.unwrap_or("none")); + let start = s.match_indices('/').nth(keep - 1).map_or(s.len(), |(i, _)| i + 1); + put(out, &s[start..]); + } + 5 => { + // Parts joined again, by the pattern. + let parts: Vec<&str> = s.split('/').collect(); + put(out, &parts.join(p.as_str())); + put(out, &["a", "", &s].join("/")); + } + _ => { + // A slice at any offsets, or at ones about the end by the kind's bit 5, which + // panics as std does past the end, backwards, or inside a char, whichever comes + // first. + let at = |b: u8| match kind & 0x20 { + 0 => b as usize % (s.len() + 2), + _ => (s.len() + 1).saturating_sub(b as usize % 4), + }; + let (a, b) = match data { + [a, b, ..] => (at(*a), at(*b)), + _ => (0, 0), + }; + let sliced = match kind / 8 % 4 { + 0 => &s[a..b], + 1 => &s[a..], + 2 => &s.as_str()[..b], + _ => &s.as_str()[..], + }; + put(out, sliced); + } + } +} + /// A predicate that is a function, not a closure. fn digit(c: char) -> bool { c.is_ascii_digit() diff --git a/ts/fixtures/reject/census.md b/ts/fixtures/reject/census.md index 006ab4684..24c84fff1 100644 --- a/ts/fixtures/reject/census.md +++ b/ts/fixtures/reject/census.md @@ -84,7 +84,7 @@ The type or module each refused type or path belongs to. | 1 | `std::ops::RangeTo` | fixtures/reject/src/lib.rs:289:7 | | 1 | `std::ops::Range` | fixtures/reject/src/lib.rs:207:8 | | 1 | `std::slice::Iter` | fixtures/reject/src/lib.rs:153:11 | -| 1 | `std::str` | fixtures/reject/src/lib.rs:382:4 | +| 1 | `std::str` | fixtures/reject/src/lib.rs:383:4 | | 1 | `std::sync::MutexGuard` | fixtures/reject/src/lib.rs:148:2 | | 1 | `u64` | fixtures/reject/src/lib.rs:118:2 | | 1 | `{async closure}` | fixtures/reject/src/lib.rs:74:2 | @@ -138,7 +138,7 @@ The type or module each refused type or path belongs to. | 1 | unsupported call to `_` | `std::ops::Range::contains` | fixtures/reject/src/lib.rs:207:8 | | 1 | unsupported call to `_` | `std::slice::copy_within` | fixtures/reject/src/lib.rs:367:4 | | 1 | unsupported call to `_` | `std::slice::sort` | fixtures/reject/src/lib.rs:32:4 | -| 1 | unsupported call to `_` | `std::str::split` | fixtures/reject/src/lib.rs:382:4 | +| 1 | unsupported call to `_` | `std::str::split` | fixtures/reject/src/lib.rs:383:4 | | 1 | unsupported type `_` | `dyn std::ops::Fn(u8) -> u8` | fixtures/reject/src/lib.rs:10:16 | ## Spans @@ -163,7 +163,7 @@ The type or module each refused type or path belongs to. - fixtures/reject/src/lib.rs:153:11: unsupported call to ` as std::iter::Iterator>::cycle` - fixtures/reject/src/lib.rs:207:8: unsupported call to `std::ops::Range::::contains::` - fixtures/reject/src/lib.rs:367:4: unsupported call to `core::slice::::copy_within::>` -- fixtures/reject/src/lib.rs:382:4: unsupported call to `core::str::::split::<&str>` +- fixtures/reject/src/lib.rs:383:4: unsupported call to `core::str::::split::<[char; 2]>` diff --git a/ts/fixtures/reject/expected.txt b/ts/fixtures/reject/expected.txt index 18d649881..582e45745 100644 --- a/ts/fixtures/reject/expected.txt +++ b/ts/fixtures/reject/expected.txt @@ -63,7 +63,7 @@ unsupported call to `core::slice::::copy_within::` at fixtures/reject/src/lib.rs:372:2 `&mut std::string::String`: a JS value of `std::string::String` is a copy, so a reference can't change it at fixtures/reject/src/lib.rs:376:15 replacing the value behind a reference to `std::string::String` at fixtures/reject/src/lib.rs:377:4 -unsupported call to `core::str::::split::<&str>` at fixtures/reject/src/lib.rs:382:4 +unsupported call to `core::str::::split::<[char; 2]>` at fixtures/reject/src/lib.rs:383:4 overriding `Iterator::fold`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:166:2 an iterator of the crate implementing `DoubleEndedIterator`, which is not supported yet at fixtures/reject/src/lib.rs:186:1 overriding `Iterator::size_hint`, which std's adaptors call in place of `next` or beside it at fixtures/reject/src/lib.rs:220:2 diff --git a/ts/fixtures/reject/src/lib.rs b/ts/fixtures/reject/src/lib.rs index c08ac00f4..a239af3d4 100644 --- a/ts/fixtures/reject/src/lib.rs +++ b/ts/fixtures/reject/src/lib.rs @@ -377,7 +377,8 @@ pub fn pushed(s: &mut String) { s.push('a'); } -/// A `str` pattern, where only a `char` and a predicate are supported. -pub fn split_str(s: &str) -> usize { - s.split("ab").count() +/// A `str` pattern of several chars, where only a `char`, a `&str`, and a predicate are +/// supported. +pub fn split_chars(s: &str) -> usize { + s.split(['a', 'b']).count() } diff --git a/ts/fixtures/reject/ts.toml b/ts/fixtures/reject/ts.toml index 28b23a24f..d055212eb 100644 --- a/ts/fixtures/reject/ts.toml +++ b/ts/fixtures/reject/ts.toml @@ -45,7 +45,7 @@ roots = [ "shifted", "shared", "pushed", - "split_str", + "split_chars", "missing", ] printable = ["Meters"] diff --git a/ts/runtime/rt/str.ts b/ts/runtime/rt/str.ts index d5f78ec10..eb7f40544 100644 --- a/ts/runtime/rt/str.ts +++ b/ts/runtime/rt/str.ts @@ -1,14 +1,16 @@ -// `str`'s iterators and searches over `char`, for rust2-ts output. `rt.ts` re-exports all -// of it. +// `str`'s slices, iterators, and searches, for rust2-ts output. `rt.ts` re-exports all of +// it. // // A JS string is UTF-16 where a `str` is UTF-8: these step by code point, and an offset -// they return counts UTF-8 bytes, as Rust's does. A string from translated code is well -// formed, so a char's text never matches across a surrogate pair. +// they take or return counts UTF-8 bytes, as Rust's does. A string from translated code is +// well formed, so a pattern's text matches where its UTF-8 would, never across a +// surrogate pair. +import { fmtCharDebug, panic, panicFmt, stringLen } from "../rt.ts"; import type { char, usize } from "./int.ts"; import { DONE, type Done, Iter } from "./iter.ts"; -/** A pattern: a `char`, as its text, or a `FnMut(char) -> bool`. */ +/** A pattern: a `char` or a `&str`, as its text, or a `FnMut(char) -> bool`. */ export type Pattern = string | ((c: char) => boolean); /** The UTF-8 length of the code units of `s` from `from` to `to`. */ @@ -28,6 +30,64 @@ function before(s: string, end: number): number { return u >= 0xdc00 && u <= 0xdfff ? end - 2 : end - 1; } +/** A char's UTF-8 length. */ +function charLen(c: number): number { + return c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4; +} + +/** + * The code unit of `s` where byte `to` starts a char, reading on from code unit `i`, which + * is byte `from`; -1 if `to` is inside a char, past the end, or before `from`. + */ +function unitAt(s: string, i: number, from: number, to: number): number { + while (from < to && i < s.length) { + const u = s.charCodeAt(i); + if (u >= 0xd800 && u <= 0xdbff) { + // A surrogate pair is one char of four bytes. + from += 4; + i += 2; + } else { + from += u < 0x80 ? 1 : u < 0x800 ? 2 : 3; + i++; + } + } + return from === to ? i : -1; +} + +/** `&s[start..end]` by UTF-8 byte offsets, with `end` defaulting to the length. */ +export function strSlice(s: string, start: number, end?: number): string { + const from = unitAt(s, 0, 0, start); + if (end === undefined) { + if (from < 0) sliceFailed(s, start, stringLen(s)); + return s.slice(from); + } + const to = from < 0 ? -1 : unitAt(s, from, start, end); + if (to < 0) sliceFailed(s, start, end); + return s.slice(from, to); +} + +/** std's panic for a slice of `s` that is out of bounds, backwards, or inside a char. */ +function sliceFailed(s: string, start: number, end: number): never { + const len = stringLen(s); + if (start > len) panic(`start byte index ${start} is out of bounds for string of length ${len}`); + if (end > len) panic(`end byte index ${end} is out of bounds for string of length ${len}`); + if (start > end) panic(`byte range starts at ${start} but ends at ${end}`); + const [which, at] = unitAt(s, 0, 0, start) < 0 ? ["start", start] : ["end", end]; + // The char `at` is inside, and where its bytes start. + let floor = 0; + let c = 0; + for (const ch of s) { + c = ch.codePointAt(0) as number; + if (floor + charLen(c) > at) break; + floor += charLen(c); + } + panicFmt([ + `${which} byte index ${at} is not a char boundary; it is inside `, + (f) => fmtCharDebug(c as char, f), + ` (bytes ${floor}..${floor + charLen(c)} of string)`, + ]); +} + /** `str::chars`. */ export function strChars(s: string): Iter { return new Chars(s); @@ -60,13 +120,16 @@ class Chars extends Iter { /** * std's searcher of a pattern in `s` between `front` and `back`, in code units. Each step - * finds the next match from one end, `start` to `end`, and moves that end past it. + * finds the next match from one end, `start` to `end`, and moves that end past it. A + * `&str` pattern searches from one end only, as std's can. */ class Searcher { readonly s: string; readonly #p: Pattern; #front = 0; #back: number; + // An empty pattern matches before each char and after the last, as std's does. + #matched = false; start = 0; end = 0; @@ -79,6 +142,15 @@ class Searcher { nextMatch(): boolean { const { s } = this; const p = this.#p; + if (p === "") { + if (this.#matched) { + if (this.#front >= this.#back) return false; + this.#front += (s.codePointAt(this.#front) as number) > 0xffff ? 2 : 1; + } + this.#matched = true; + this.start = this.end = this.#front; + return true; + } if (typeof p === "string") { const i = s.indexOf(p, this.#front); if (i < 0 || i + p.length > this.#back) { @@ -211,16 +283,22 @@ class MatchIndices extends Iter<[usize, string]> { } } +/** `str::contains`. */ +export function strContains(s: string, p: Pattern): boolean { + return typeof p === "string" ? s.includes(p) : new Searcher(s, p).nextMatch(); +} + /** `str::starts_with`. */ export function strStartsWith(s: string, p: Pattern): boolean { if (typeof p === "string") return s.startsWith(p); return s.length > 0 && p(s.codePointAt(0) as char); } -/** `str::trim_start_matches`. */ +/** `str::trim_start_matches`, which an empty pattern leaves as it is. */ export function strTrimStartMatches(s: string, p: Pattern): string { let i = 0; if (typeof p === "string") { + if (p === "") return s; while (s.startsWith(p, i)) i += p.length; } else { while (i < s.length) { @@ -232,10 +310,11 @@ export function strTrimStartMatches(s: string, p: Pattern): string { return s.slice(i); } -/** `str::trim_end_matches`. */ +/** `str::trim_end_matches`, which an empty pattern leaves as it is. */ export function strTrimEndMatches(s: string, p: Pattern): string { let end = s.length; if (typeof p === "string") { + if (p === "") return s; while (end >= p.length && s.startsWith(p, end - p.length)) end -= p.length; } else { while (end > 0) { diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 8bacb3fde..fb31fcca6 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -9,6 +9,7 @@ use rustc_hir::def_id::DefId; use rustc_middle::ty::{self, GenericArgsRef, Instance, Ty, TyCtxt}; use rustc_span::Span; +use super::Translator; use super::body::Body; use super::fmt; use super::num::{self, Int, Operand}; @@ -753,17 +754,15 @@ pub fn call<'tcx>(b: &mut Body<'_, '_, 'tcx>, call: &Call<'tcx>, args: Vec ("String" | "str", "is_empty") => args.pop()?.member("length").binary("===", Expr::lit("0")), ("str", "chars") => Expr::rt("strChars", args), // Searches by a pattern. - ("str", "split" | "match_indices" | "starts_with" | "trim_start_matches" | "trim_end_matches" | "rfind") => { - let f = match name.as_str() { - "split" => "strSplit", - "match_indices" => "strMatchIndices", - "starts_with" => "strStartsWith", - "trim_start_matches" => "strTrimStartMatches", - "trim_end_matches" => "strTrimEndMatches", - _ => "strRfind", - }; + ("str", "split" | "match_indices" | "contains" | "starts_with" | "rfind") + | ("str", "trim_start_matches" | "trim_end_matches") => { let [s, p] = <[Expr; 2]>::try_from(args).ok()?; - Expr::rt(f, vec![s, pattern(targ(gargs, 0), p)?]) + Expr::rt_of("str", &super::camel(&name)).call(vec![s, pattern(b.t, targ(gargs, 0), p)?]) + } + // A slice of strings joined by a `&str`. + ("slice", "join") if is_text(b.t, targ(gargs, 0)) && targ(gargs, 1).peel_refs().is_str() => { + let [parts, sep] = <[Expr; 2]>::try_from(args).ok()?; + parts.method("join", vec![sep]) } ("char", "is_ascii_digit") => Expr::rt("charIsAsciiDigit", args), ("ToString", "to_string") | ("ToOwned", "to_owned") if matches!(self_ty()?.peel_refs().kind(), ty::Str) => { @@ -943,15 +942,22 @@ fn owned<'tcx>(b: &mut Body<'_, '_, 'tcx>, value: Expr, ty: Ty<'tcx>, span: Span } /// A `str` method's pattern as the runtime takes it, an `rt.Pattern`: a `char`'s text, -/// or a predicate as it is. `None` for any other pattern. -fn pattern(ty: Ty<'_>, p: Expr) -> Option { +/// or a string or a predicate as it is. `None` for any other pattern. +fn pattern<'tcx>(t: &Translator<'_, 'tcx>, ty: Ty<'tcx>, p: Expr) -> Option { match ty.kind() { ty::Char => Some(char_text(p)), ty::Closure(..) | ty::FnDef(..) => Some(p), + ty::Ref(..) if is_text(t, ty) => Some(p), _ => None, } } +/// Whether `ty` is a `str` or a `String`, behind any references: a JS string. +fn is_text<'tcx>(t: &Translator<'_, 'tcx>, ty: Ty<'tcx>) -> bool { + let ty = ty.peel_refs(); + ty.is_str() || t.std_of(ty) == Some(Std::String) +} + /// A `char`'s text: a literal for a constant, or `String.fromCodePoint(c)`. pub fn char_text(c: Expr) -> Expr { if let Expr::Lit(n) = &c diff --git a/ts/src/lower/mir/call.rs b/ts/src/lower/mir/call.rs index ae837fc84..eb469d788 100644 --- a/ts/src/lower/mir/call.rs +++ b/ts/src/lower/mir/call.rs @@ -653,6 +653,17 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { ) -> Option> { let container = gargs.type_at(0); let index_ty = gargs.type_at(1); + // A string slices by UTF-8 byte offsets, which a JS string doesn't index by. + if container.is_str() || self.b.t.std_of(container) == Some(Std::String) { + if name == "index_mut" { + return None; + } + let base = self.operand(&args[0].node, span, out); + let (start, end) = self.range_bounds(&args[1].node, index_ty, span, out)?; + let mut values = vec![self.path(base, "s", out), start]; + values.extend(end); + return Some(Some((Expr::rt("strSlice", values), true))); + } let elem_ty = elem(self.tcx, container)?; let bytes = matches!(elem_ty.kind(), ty::Uint(ty::UintTy::U8)); let base = self.operand(&args[0].node, span, out); From 48ce3edf84baa3cb5a4e5094240f55e1ebd1fd56 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 26 Oct 2026 21:23:48 -0700 Subject: [PATCH 3/3] test(ts): join a Vec by a &str Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ts/fixtures/lang/expected/strs.ts | 42 +++++++++++++++++-------------- ts/fixtures/lang/src/strs.rs | 2 ++ 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/ts/fixtures/lang/expected/strs.ts b/ts/fixtures/lang/expected/strs.ts index 7769e63b7..3126af058 100644 --- a/ts/fixtures/lang/expected/strs.ts +++ b/ts/fixtures/lang/expected/strs.ts @@ -295,7 +295,7 @@ export function pattern(kind: rt.u8, s: string): string { * one matches before each char and after the last. */ export function slices(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { - let _198!: [rt.usize, rt.usize]; + let _211!: [rt.usize, rt.usize]; let sliced!: string; const s: string = text(data); const _6: string = s; @@ -327,10 +327,10 @@ export function slices(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { putOffset(out, at); put(out, _42[1]); put(out, rt.strSlice(s, 0, at)); - const part5 = rt.usizeAdd(at, rt.stringLen(_42[1])); - put(out, rt.strSlice(s, part5)); const part6 = rt.usizeAdd(at, rt.stringLen(_42[1])); - put(out, rt.strSlice(s, at, part6)); + put(out, rt.strSlice(s, part6)); + const part7 = rt.usizeAdd(at, rt.stringLen(_42[1])); + put(out, rt.strSlice(s, at, part7)); } } if (_8 === 2) { @@ -367,12 +367,16 @@ export function slices(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { }), "none")); const _159: rt.Iter<[rt.usize, string]> = rt.strMatchIndices(s, "/"); const _162: rt.usize = rt.usizeSub(keep, 1); - const part4 = rt.optionMapOr(rt.iterNth(_159, _162), rt.stringLen(s), (_22: [rt.usize, string]): rt.usize => { + const part5 = rt.optionMapOr(rt.iterNth(_159, _162), rt.stringLen(s), (_22: [rt.usize, string]): rt.usize => { return rt.usizeAdd(_22[0], 1); }); - put(out, rt.strSlice(s, part4)); + put(out, rt.strSlice(s, part5)); } else if (_8 === 5) { - put(out, rt.iterToArray(rt.strSplit(s, "/")).join(p)); + const parts: string[] = rt.iterToArray(rt.strSplit(s, "/")); + put(out, parts.join(p)); + put(out, rt.iterToArray(rt.iterMap(rt.iterSlice(parts), (part3: string): string => { + return part3; + })).join(p)); put(out, ["a", "", s].join("/")); } else { const at2: (a0: rt.u8) => rt.usize = (b2: rt.u8): rt.usize => { @@ -382,23 +386,23 @@ export function slices(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { return rt.usizeSaturatingSub(rt.usizeAdd(rt.stringLen(s), 1), rt.usizeRem(b2 as rt.usize, 4)); }; if (!(data.length >= 2)) { - _198 = [0, 0] as [rt.usize, rt.usize]; + _211 = [0, 0] as [rt.usize, rt.usize]; } else { - _198 = [at2(data.data[data.start]), at2(data.data[data.start + 1])] as [rt.usize, rt.usize]; + _211 = [at2(data.data[data.start]), at2(data.data[data.start + 1])] as [rt.usize, rt.usize]; } - const a: rt.usize = _198[0]; - const b: rt.usize = _198[1]; - const _212: rt.u8 = rt.u8Rem(rt.u8Div(kind, 8), 4); - if (_212 === 0) { + const a: rt.usize = _211[0]; + const b: rt.usize = _211[1]; + const _225: rt.u8 = rt.u8Rem(rt.u8Div(kind, 8), 4); + if (_225 === 0) { sliced = rt.strSlice(s, a, b); - } else if (_212 === 1) { + } else if (_225 === 1) { sliced = rt.strSlice(s, a); - } else if (_212 === 2) { - const _223: string = s; - sliced = rt.strSlice(_223, 0, b); + } else if (_225 === 2) { + const _236: string = s; + sliced = rt.strSlice(_236, 0, b); } else { - const _227: string = s; - sliced = rt.strSlice(_227, 0); + const _240: string = s; + sliced = rt.strSlice(_240, 0); } put(out, sliced); } diff --git a/ts/fixtures/lang/src/strs.rs b/ts/fixtures/lang/src/strs.rs index 35268f046..becee85b3 100644 --- a/ts/fixtures/lang/src/strs.rs +++ b/ts/fixtures/lang/src/strs.rs @@ -296,6 +296,8 @@ pub fn slices(kind: u8, data: &[u8], out: &mut Vec) { // Parts joined again, by the pattern. let parts: Vec<&str> = s.split('/').collect(); put(out, &parts.join(p.as_str())); + let owned: Vec = parts.iter().map(|part| part.to_string()).collect(); + put(out, &owned.join(p.as_str())); put(out, &["a", "", &s].join("/")); } _ => {