diff --git a/quest/m3/README.md b/quest/m3/README.md index ea66a11f5..5674761fd 100644 --- a/quest/m3/README.md +++ b/quest/m3/README.md @@ -22,7 +22,6 @@ generic std gap is planned here even when moq never calls it. - [The capacity of a BytesMut](/quest/m3/ts-bytes-capacity.md) - `BytesMut::capacity` and what reads it translate with `bytes`' values - [BufMut's provided writes through a &mut](/quest/m3/ts-buf-mut-forward.md) - `put`, `put_bytes`, `put_uint`, and `put_int` of a `&mut Vec` or `&mut BytesMut` translate - [Trait objects](/quest/m3/ts-dyn.md) - `dyn Error`, `dyn Iterator`, `dyn Buf`, and `Arc` translate, so a consumer neither rewrites nor binds them -- [Strings from and into iterators](/quest/m3/ts-string-iters.md) - `collect::`, `String::extend`, and `drain` translate - [A widening try_from typed by its Infallible](/quest/m3/ts-try-from-infallible.md) - an integer `try_from` that can't fail type-checks as `Result` - [Placeholders written to a crate's own fmt::Write](/quest/m3/ts-fmt-calls.md) - `write!` with placeholders into a crate's own writer translates, its `write_str` seeing each call std makes - [Waiting: type-check in process with tsrs](/quest/m3/ts-tsrs.md) - rust2-ts checks bindings through tsrs instead of `tsc` once it is a stable library @@ -32,6 +31,7 @@ generic std gap is planned here even when moq never calls it. - [Drop order in std's folds and iterators](/quest/m3/ts-drop-order.md) - the values that drop still refused in std's folds, zips, flattens, and collects drop as std's do - [References to byte-array keys a collection yields](/quest/m3/ts-byte-key-refs.md) - `keys`, `iter`, `get_key_value`, and `HashSet::get` of a `[u8; N]` key translate - [An array as a slice](/quest/m3/ts-array-as-slice.md) - `<[T; N]>::as_slice` and `as_mut_slice` translate +- [Size hints of std's iterators](/quest/m3/ts-size-hints.md) - `size_hint` translates, and `Vec`'s collect and extend panic past `isize::MAX` as std's do - [Closures that return an Option whose None is rt.None](/quest/m3/ts-option-closures.md) - `find_map`, `filter_map`, and `map_while` to `Option>` keep `None` apart from `Some(None)` - [Peekable::next_if_eq against another type](/quest/m3/ts-next-if-eq-other.md) - `next_if_eq` with an expected value of another type than the item, such as a `&str` of `String` items, translates - [Waiting: a slice object for a mutable part of other values](/quest/m3/ts-slice-object.md) - waits on a moq profile showing the `Proxy` view's traps matter, then decides on an `rt.ByteSlice`-like form diff --git a/quest/m3/ts-size-hints.md b/quest/m3/ts-size-hints.md new file mode 100644 index 000000000..e421f7a4a --- /dev/null +++ b/quest/m3/ts-size-hints.md @@ -0,0 +1,20 @@ +# [M] Size hints of std's iterators + +## Goal + +[rust2-ts](/ts/README.md) translates `Iterator::size_hint` of std's iterators, and each +collect and extend of a collection that reserves by one, such as `Vec`'s `collect` and +`extend`, panics with std's `capacity overflow` where std's does, proven by differential +tests against native Rust. + +## Plan + +Found by [#284](https://github.com/moq-dev/rust2/pull/284) on 2026-10-30, which gives +`rt.Iter` a `lower` bound, exact for ranges and what adapts them, and reserves it in +`String`'s `Extend`. `Vec`'s `collect` of a `TrustedLen` iterator reserves its upper +bound and panics without one, and its `extend` reserves the lower bound; `rt.iterToArray` +and `rt.iterExtend` reserve neither, so `(0..u32::MAX).map(f).collect::>()` reads +on until memory runs out where std's panics before calling `f`. A call of `size_hint` +itself needs the upper bound too, and the crate's own `size_hint` is refused. + +Public API: the translator's output. diff --git a/quest/m3/ts-string-iters.md b/quest/m3/ts-string-iters.md deleted file mode 100644 index 7cd8deb12..000000000 --- a/quest/m3/ts-string-iters.md +++ /dev/null @@ -1,20 +0,0 @@ -# [S] Strings from and into iterators - -## Goal - -[rust2-ts](/ts/README.md) translates `collect::`, `String`'s `Extend` of chars and -strings, and `String::drain`, proven by differential tests against native Rust. - -## Plan - -Found by [#271](https://github.com/moq-dev/rust2/pull/271) on 2026-10-30, which translates -`String`'s edits that take and give plain values. Collecting chars into a `String` is -refused too. moq-net and kio call none of these, so this is m3 under the all-of-std -decision. - -- Each reads its iterator as std does: `Extend` asks `size_hint` first, which an own - iterator may observe. -- A `Drain` is an iterator of the drained chars; the `&mut String` it holds keeps anything - from seeing the string before it drops, so it can be edited at once. - -Public API: the translator's output. diff --git a/ts/README.md b/ts/README.md index fcb524515..dd0b42cd3 100644 --- a/ts/README.md +++ b/ts/README.md @@ -215,9 +215,12 @@ returns, from `rfind` or `match_indices`, counts UTF-8 bytes, as Rust's does, an 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`'s edits, `push_str`, `truncate`, `insert`, `retain`, `replace_range`, and the rest, assign the edited one to the local or -field the `&mut` borrows, taking and checking byte offsets as std does. A `String` keeps no -capacity: `with_capacity`, `reserve`, and `shrink_to_fit` do nothing, and `capacity` is -refused. A `Cow` is its text, borrowed or owned, so +field the `&mut` borrows, taking and checking byte offsets as std does. `extend` and +`collect` read their iterator by `for_each`, as std's do, and of chars first reserve its +size hint's lower bound, which panics past `isize::MAX`. A `drain` holds the `&mut`'s +place and writes the string without the drained text as it drops, so one that +`mem::forget` leaks leaves it as it was, as std's does. A `String` keeps no capacity: +`with_capacity`, `reserve`, and `shrink_to_fit` do nothing, and `capacity` is refused. A `Cow` is its text, borrowed or owned, so `to_mut` borrows the `Cow`'s own place, and code that tells its variants apart, a `match` on them or `Cow::is_borrowed`, is refused. @@ -622,7 +625,7 @@ copies that std makes, other than a map's, or hands a closure, as `iter_mut` and without a mapping in `src/lower/iter.rs` (such as `cycle`, `unzip`, and `partition`), an own `impl Iterator` with `next_back`, `len`, or another provided method of its own, a pointer's address (`{:p}`), `write!` with a placeholder into -the crate's own `fmt::Write`, `String::extend` and `drain`, a `str` pattern other than a +the crate's own `fmt::Write`, a `str` pattern other than a `char`, a `&str`, a predicate, or chars, 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 diff --git a/ts/fixtures/lang/caller.ts b/ts/fixtures/lang/caller.ts index 2b9853f9b..4ce2d88ea 100644 --- a/ts/fixtures/lang/caller.ts +++ b/ts/fixtures/lang/caller.ts @@ -378,4 +378,22 @@ const sliceEdits = [0, 1, 2, 3, 4].flatMap((n) => }), ); -export const cases: Uint8Array[] = [...loops, ...durations, ...ints(), ...utf8, ...floats, ...formats, ...parsed, ...dyns, ...cells, ...shared, ...locks, ...onces, ...bounds, ...unsafeCells, ...edits, ...subslices, ...consumers, ...ranges, ...reducers, ...sliceEdits]; +/** + * `strs::drains` of each range and way to end the drain on `aé\u{800}😀`, at every byte + * offset into it and up to two past its end, reading none, two from either end, or three + * from the back. + */ +const drainsOp = op("strs::drains"); +const drains = Array.from({ length: 32 * 13 * 13 }, (_, i) => { + const [form, a, b] = [i % 32, Math.floor(i / 32) % 13, Math.floor(i / 416)]; + return Uint8Array.of(drainsOp, 4, 2, 7, 9, 14, form, a, b, [0, 0x0a, 0x1f][(form + a + b) % 3]); +}); + +/** `strs::iters` of each kind, on no text, text of each UTF-8 width, and parts between `/`s, empty ones too. */ +const iters = Array.from({ length: 12 }, (_, kind) => [[], [2, 7, 9, 14], [0, 2, 0, 0, 7, 9], [14, 0]].map((data) => Uint8Array.of(op("strs::iters"), kind, ...data))).flat(); + +/** `strs::hinted` of each adaptor and each pair of range lengths, each way, on `aé` alone. */ +const hintedOp = op("strs::hinted"); +const hinted = Array.from({ length: 10 * 7 * 21 }, (_, i) => Uint8Array.of(hintedOp, i % 10, Math.floor(i / 10) % 7, Math.floor(i / 70))); + +export const cases: Uint8Array[] = [...loops, ...durations, ...ints(), ...utf8, ...floats, ...formats, ...parsed, ...dyns, ...cells, ...shared, ...locks, ...onces, ...bounds, ...unsafeCells, ...edits, ...subslices, ...consumers, ...ranges, ...reducers, ...sliceEdits, ...drains, ...iters, ...hinted]; diff --git a/ts/fixtures/lang/expected/collections.ts b/ts/fixtures/lang/expected/collections.ts index c8c28a42a..a71883d6b 100644 --- a/ts/fixtures/lang/expected/collections.ts +++ b/ts/fixtures/lang/expected/collections.ts @@ -1116,7 +1116,7 @@ export function stores(data: rt.ByteSlice, out: rt.ByteVec): void { v[at8][1] = v[at8][1] ^ 16; rt.iterExtendBytes(out, rt.iterFlatMap(rt.iterSlice(v), (p: [rt.u8, rt.u8]): Uint8Array => { return Uint8Array.of(p[0], p[1]); - }, (value: Uint8Array) => rt.iterSlice(value))); + }, (value: Uint8Array) => rt.iterSlice(value), false, 2)); rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(changed[0], changed[1], old[0], old[1]))); const tally: Tally.Tally = { n: a as rt.u16 }; const q: rt.VecDeque = rt.dequeNew() as rt.VecDeque; @@ -1148,7 +1148,7 @@ export function stores(data: rt.ByteSlice, out: rt.ByteVec): void { return mapOr$1nlkf9b(rt.hashGet(hash, k), rt.bytes(2).fill(255), (p2: [rt.u8, rt.u8]): Uint8Array => { return Uint8Array.of(p2[0], p2[1]); }); - }, (value: Uint8Array) => rt.iterSlice(value))); + }, (value: Uint8Array) => rt.iterSlice(value), false, 2)); const tree: rt.BTreeMap = rt.btreeNew(rt.cmp) as rt.BTreeMap; rt.btreeInsert(tree, 0, copy$u8_u8(pair)); rt.btreeInsert(tree, 1, copy$u8_u8(pair)); @@ -1158,7 +1158,7 @@ export function stores(data: rt.ByteSlice, out: rt.ByteVec): void { } rt.iterExtendBytes(out, rt.iterFlatMap(rt.btreeValues(tree), (p3: [rt.u8, rt.u8]): Uint8Array => { return Uint8Array.of(p3[0], p3[1]); - }, (value: Uint8Array) => rt.iterSlice(value))); + }, (value: Uint8Array) => rt.iterSlice(value), false, 2)); const boxed: [rt.u8, rt.u8] = copy$u8_u8(pair); boxed[0] = boxed[0] ^ 1; const holders: Holder.Holder[] = [{ pair: copy$u8_u8(pair) }]; diff --git a/ts/fixtures/lang/expected/core/option.ts b/ts/fixtures/lang/expected/core/option.ts index 4db5e2072..794fc93a2 100644 --- a/ts/fixtures/lang/expected/core/option.ts +++ b/ts/fixtures/lang/expected/core/option.ts @@ -127,6 +127,13 @@ export function unwrapOr$0x6fy3l(self: [rt.u8, rt.u8] | undefined, default_: [rt return self; } +export function unwrapOr$1rbuegs(self: rt.ByteSlice | undefined, default_: rt.ByteSlice): rt.ByteSlice { + if (self === undefined) { + return default_; + } + return self; +} + export function unwrapOr$14m5xjm(self: [rt.usize, rt.u8] | undefined, default_: [rt.usize, rt.u8]): [rt.usize, rt.u8] { if (self === undefined) { return default_; diff --git a/ts/fixtures/lang/expected/harness.ts b/ts/fixtures/lang/expected/harness.ts index abf735a5d..16676cccb 100644 --- a/ts/fixtures/lang/expected/harness.ts +++ b/ts/fixtures/lang/expected/harness.ts @@ -37,7 +37,7 @@ import { copies as shapes_copies, order, discriminants, loops, patterns, closure import { parts, chunked, subslices, drains, ranges as slices_ranges, edits as slices_edits } from "./slices.ts"; import { splits } from "./splits.ts"; import { statics, cells as statics_cells, onces } from "./statics.ts"; -import { strs, slices, edits as strs_edits, parses as strs_parses } from "./strs.ts"; +import { strs, slices, edits as strs_edits, parses as strs_parses, iters, drains as strs_drains, hinted } from "./strs.ts"; import { decodes } from "./utf8.ts"; import { wrappers } from "./wrappers.ts"; @@ -90,7 +90,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) refs(data21, out21); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::counts", (_2121: rt.u8, data22: rt.ByteSlice, out22: rt.ByteVec): void => { counts(data22, out22); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_2122: rt.u8, _378: rt.ByteSlice, out23: rt.ByteVec): void => { + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["fmt::rounding", (_2122: rt.u8, _387: rt.ByteSlice, out23: rt.ByteVec): void => { rounding(out23); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["iters::skips", skips] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["wrappers::wrappers", wrappers] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["edits::edits", edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["utf8::decodes", decodes] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["floats::floats", floats] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["atomics::atomics", atomics] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["keys::overrides", overrides] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["dyns::dyns", (_2123: rt.u8, data23: rt.ByteSlice, out24: rt.ByteVec): void => { dyns(data23, out24); @@ -176,7 +176,7 @@ export function cases(): [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) reducers(data63, out64); }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["pointers::writers", (_2164: rt.u8, data64: rt.ByteSlice, out65: rt.ByteVec): void => { writers(data64, out65); - }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["errors::eqs", eqs] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["slices::edits", slices_edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]; + }] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["errors::eqs", eqs] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["slices::edits", slices_edits] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::iters", iters] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::drains", strs_drains] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void], ["strs::hinted", hinted] as [string, (a0: rt.u8, a1: rt.ByteSlice, a2: rt.ByteVec) => void]]; } /** Run one case. */ diff --git a/ts/fixtures/lang/expected/holds.ts b/ts/fixtures/lang/expected/holds.ts index dfdf7bbac..5efa4d95a 100644 --- a/ts/fixtures/lang/expected/holds.ts +++ b/ts/fixtures/lang/expected/holds.ts @@ -69,7 +69,7 @@ export function places(a: rt.u8, b: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): set(rt.place(() => grid[_30][_32], (value) => grid[_30][_32] = value), k); set(rt.place(() => rows[_35].cells[_39], (value2) => rows[_35].cells[_39] = value2), k ^ 1); } - rt.iterExtendBytes(out, rt.iterFlatten(rt.iterMap(rt.iterSlice(grid), rt.byteSliceFrom), (value: rt.ByteSlice) => rt.iterSlice(rt.byteSliceView(value)))); + rt.iterExtendBytes(out, rt.iterFlatten(rt.iterMap(rt.iterSlice(grid), rt.byteSliceFrom), (value: rt.ByteSlice) => rt.iterSlice(rt.byteSliceView(value)), false, 4)); for (const _61 of rows) { rt.iterExtendBytes(out, rt.iterSlice(_61.cells)); } diff --git a/ts/fixtures/lang/expected/orders.ts b/ts/fixtures/lang/expected/orders.ts index 9ad55b661..5b947534d 100644 --- a/ts/fixtures/lang/expected/orders.ts +++ b/ts/fixtures/lang/expected/orders.ts @@ -436,7 +436,7 @@ export function values(data: rt.ByteSlice, out: rt.ByteVec): void { } rt.iterExtendBytes(out, rt.iterFlatMap(rt.iterSlice(rt.heapIntoSortedVec(pairs, rt.cmpLe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpGe(rt.cmpTuple([rt.cmp, rt.cmp])), rt.cmpLt(rt.cmpTuple([rt.cmp, rt.cmp])))), (_25: [rt.u8, rt.u8]): Uint8Array => { return Uint8Array.of(_25[0], _25[1]); - }, (value: Uint8Array) => rt.iterSlice(value))); + }, (value: Uint8Array) => rt.iterSlice(value), false, 2)); } /** @@ -531,9 +531,9 @@ export function bytes(data: rt.ByteSlice, out: rt.ByteVec): void { rt.iterExtendBytes(out, rt.iterFlatMap(rt.btreeIter(rt.btreeExtend(rt.btreeNew(rt.cmpBytes) as rt.BTreeMap, _185, true)), (_22: [Uint8Array, rt.usize]): Uint8Array => { const k: Uint8Array = _22[0]; return Uint8Array.of(k[0], k[1], _22[1] & 0xff); - }, (value: Uint8Array) => rt.iterSlice(value))); + }, (value: Uint8Array) => rt.iterSlice(value), false, 3)); rt.sliceSort(arrays, rt.cmpLt(rt.cmpBytes)); - rt.iterExtendBytes(out, rt.iterFlatten(rt.iterMap(rt.iterSlice(arrays), rt.byteSliceFrom), (value: rt.ByteSlice) => rt.iterSlice(rt.byteSliceView(value)))); + rt.iterExtendBytes(out, rt.iterFlatten(rt.iterMap(rt.iterSlice(arrays), rt.byteSliceFrom), (value: rt.ByteSlice) => rt.iterSlice(rt.byteSliceView(value)), false, 2)); const slices: rt.ByteSlice[] = rt.iterToArray(rt.byteSliceChunks(data, 3)); rt.sliceSortBy(slices, (a7: rt.ByteSlice, b3: rt.ByteSlice): rt.Ordering => { return rt.cmpBytes(b3, a7); diff --git a/ts/fixtures/lang/expected/refs.ts b/ts/fixtures/lang/expected/refs.ts index 3645810d0..aed6f6b84 100644 --- a/ts/fixtures/lang/expected/refs.ts +++ b/ts/fixtures/lang/expected/refs.ts @@ -741,7 +741,7 @@ export function elements(data: rt.ByteSlice, out: rt.ByteVec): void { for (const _168 of pairs) { rt.byteVecExtendFromSlice(out, rt.byteSliceFrom(Uint8Array.of(_168[0], unwrapOr$0r2bmpr(_168[1], 238)))); } - const iter10: rt.Iter = rt.iterFlatten(rt.iterSlice(grid), (value: rt.u32[]) => rt.iterSlice(value)); + const iter10: rt.Iter = rt.iterFlatten(rt.iterSlice(grid), (value: rt.u32[]) => rt.iterSlice(value), false, 2); while (true) { const _186: rt.u32 | rt.Done = iter10.next(); if (_186 === rt.DONE) { diff --git a/ts/fixtures/lang/expected/strs.Counted.ts b/ts/fixtures/lang/expected/strs.Counted.ts new file mode 100644 index 000000000..0ef22bf88 --- /dev/null +++ b/ts/fixtures/lang/expected/strs.Counted.ts @@ -0,0 +1,44 @@ +// Generated by rust2-ts. Do not edit. + +import * as rt from "./rt.ts"; + +/** A crate's own iterator over `items`, which counts each `next` it is asked. */ +export interface Counted { + items: T[]; + nexts: rt.Cell; +} + +export function next$Iterator_String(self: Counted): string | rt.Done { + rt.cellSet(self.nexts, rt.u8Add(self.nexts.v, 1)); + const _8: [string, string[]] | undefined = rt.sliceSplitFirst(self.items); + if (_8 !== undefined) { + const val: [string, string[]] = _8; + self.items = val[1]; + return val[0]; + } + return rt.DONE; +} + +export function next$Iterator_char(self: Counted): rt.char | rt.Done { + rt.cellSet(self.nexts, rt.u8Add(self.nexts.v, 1)); + const _8: [rt.char, rt.char[]] | undefined = rt.sliceSplitFirst(self.items); + if (_8 !== undefined) { + const val: [rt.char, rt.char[]] = _8; + self.items = val[1]; + return val[0]; + } + return rt.DONE; +} + +export function next$Iterator_str(self: Counted): string | rt.Done { + rt.cellSet(self.nexts, rt.u8Add(self.nexts.v, 1)); + const _8: [string, string[]] | undefined = rt.sliceSplitFirst(self.items); + if (_8 !== undefined) { + const val: [string, string[]] = _8; + self.items = val[1]; + return val[0]; + } + return rt.DONE; +} + +//# sourceMappingURL=strs.Counted.ts.map diff --git a/ts/fixtures/lang/expected/strs.ts b/ts/fixtures/lang/expected/strs.ts index eb8cf20a8..a51a77d1b 100644 --- a/ts/fixtures/lang/expected/strs.ts +++ b/ts/fixtures/lang/expected/strs.ts @@ -3,10 +3,11 @@ import * as rt from "./rt.ts"; import { then$0havnzo } from "./core/bool.ts"; import { fromStr$1d7pnbk, fromStrRadix$1ae2pnm, fromStr$15isx69, fromStrRadix$1mr00o6, fromStr$04uijf8, fromStrRadix$1wqmb68, fromStr$0l2zu9x, fromStrRadix$0cd0jvv, fromStr$0h0ptos, fromStrRadix$10vntu7, fromStr$10svit2, fromStrRadix$0wc95fw, fromStr$19kbj04, fromStrRadix$1o9c7gd, fromStr$0br814e, fromStrRadix$188cokx, fromStr$1471qz1, fromStrRadix$0l5ymb0, fromStr$0k8sfhf, fromStrRadix$0fpnm5c, fromStr$0b2xa80, fromStrRadix$1095n8m, fromStr$17dd66m, fromStrRadix$1yrk04t } from "./core/num/nonzero.ts"; -import { unwrapOr$1iemzn8, unwrapOr$0r2bmpr, map$0gl6wke, mapOr$147z9gi, mapOr$16umieg } from "./core/option.ts"; +import { unwrapOr$1iemzn8, unwrapOr$0r2bmpr, unwrapOr$1rbuegs, map$0gl6wke, mapOr$147z9gi, mapOr$16umieg } from "./core/option.ts"; import { parse$0ub96so, parse$1ogkfqt, parse$1n7pf37, parse$1xm5283, parse$0xeilg3, parse$0eefxsx, parse$0bwhuel, parse$0zy5dnc, parse$1o8vfr6, parse$0l0cs1u, parse$0zrdgp4, parse$0s8xu45, parse$0vh4cry } from "./core/str.ts"; import { fromStr$05d44vn } from "./core/str/traits.ts"; import { put } from "./fmt.ts"; +import * as Counted from "./strs.Counted.ts"; import * as Named from "./strs.Named.ts"; /** A char of the alphabet for each byte, by its low bits. */ @@ -1177,4 +1178,344 @@ export function parses(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { } } +/** + * `String` made from and extended by iterators of chars and of strings, each the kind + * picks, from text of chars of every UTF-8 width and its parts between `/`s. + */ +export function iters(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + const s: string = text(data); + const chars: rt.char[] = rt.iterToArray(rt.strChars(s)); + const parts: string[] = rt.iterToArray(rt.strSplit(s, "/")); + const nexts: rt.Cell = new rt.Cell(0); + const named: Named.Named = { name: text(rt.byteSliceRange(data, 0, rt.usizeDiv(data.length, 2))) }; + const _21: rt.u8 = rt.u8Rem(kind, 4); + if (_21 === 0) { + put(out, rt.stringExtendChars("", rt.strChars(s))); + put(out, rt.stringExtendChars("", rt.iterFilter(rt.iterRev(rt.strChars(s)), (c: rt.char): boolean => { + return !rt.charIsAsciiDigit(c); + }))); + put(out, rt.stringExtendChars("", rt.iterSlice(chars))); + put(out, rt.stringExtendChars("", rt.iterSkip(rt.iterSlice(chars), 1))); + rt.iterEmpty(); + put(out, rt.stringExtendChars("", rt.iterEmpty())); + put(out, rt.stringExtendChars("", rt.iterOption(rt.charFromU32(kind as rt.u32)))); + put(out, rt.stringExtendChars("", rt.iterFrom({ items: chars, nexts }, (it: Counted.Counted) => Counted.next$Iterator_char(it)))); + } else if (_21 === 1) { + put(out, rt.stringExtend("", rt.iterSlice(parts))); + put(out, rt.stringExtend("", rt.iterRev(rt.strSplit(s, "/")))); + put(out, rt.stringFromStrings(rt.iterMap(rt.iterSlice(parts), (p3: string): string => { + return p3; + }))); + put(out, rt.stringFromStrings(rt.iterFilter(rt.iterMap(rt.strSplit(s, "/"), (p4: string): string => { + const args4: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtPad(f, p4)]; + return rt.fmtFormat([args4[0], args4[0]]); + }), (p5: string): boolean => { + return !((p5.length === 0)); + }))); + rt.iterEmpty(); + put(out, rt.stringFromStrings(rt.iterEmpty())); + put(out, rt.stringExtendChars("", rt.iterRev(rt.strChars(s)))); + put(out, rt.stringExtend("", rt.iterSlice(parts))); + const owned: string[] = rt.iterToArray(rt.iterMap(rt.iterSlice(parts), (p6: string): string => { + return p6; + })); + put(out, rt.stringFromStrings(rt.iterMap(rt.iterSlice(owned), (p7: string): string => { + return p7; + }))); + put(out, rt.stringFromStrings(rt.iterFrom({ items: owned, nexts }, (it: Counted.Counted) => Counted.next$Iterator_String(it)))); + } else if (_21 === 2) { + let t: string = "é"; + const _153: rt.char[] = chars; + extendChars(rt.place(() => t, (value) => t = value), _153, kind); + const _157: rt.Iter = rt.iterRev(rt.strChars(s)); + t = rt.stringExtendChars(t, _157); + named.name = rt.stringExtendChars(named.name, rt.iterSlice(chars)); + rt.iterEmpty(); + named.name = rt.stringExtendChars(named.name, rt.iterEmpty()); + const counted3: Counted.Counted = { items: chars, nexts }; + t = rt.stringExtendChars(t, rt.iterFrom(counted3, (it: Counted.Counted) => Counted.next$Iterator_char(it))); + put(out, t); + } else { + let t2: string = ""; + const _179: rt.Iter = rt.strSplit(s, "/"); + t2 = rt.stringExtend(t2, _179); + const _184: rt.Iter = rt.iterMap(rt.iterSlice(parts), (p: string): string => { + const args2: rt.Fmt[] = [(f: rt.Formatter) => rt.fmtPad(f, p)]; + return rt.fmtFormat([args2[0], "é"]); + }); + t2 = rt.stringExtend(t2, _184); + const _190: rt.Iter = rt.iterRev(rt.iterSlice(parts)); + named.name = rt.stringExtend(named.name, _190); + const _197: rt.Iter = rt.iterMap(rt.iterSlice(parts), (p2: string): string => { + return p2; + }); + named.name = rt.stringExtend(named.name, _197); + rt.iterEmpty(); + named.name = rt.stringExtend(named.name, rt.iterEmpty()); + const counted4: Counted.Counted = { items: parts, nexts }; + t2 = rt.stringExtend(t2, rt.iterFrom(counted4, (it: Counted.Counted) => Counted.next$Iterator_str(it))); + put(out, t2); + } + put(out, named.name); + rt.byteVecPush(out, nexts.v); +} + +export function extendChars(t: rt.Place, chars: rt.char[], kind: rt.u8): void { + const _4: rt.u8 = rt.u8Rem(rt.u8Div(kind, 4), 3); + if (_4 === 0) { + const _8: rt.Iter = rt.iterSlice(chars); + t.v = rt.stringExtendChars(t.v, _8); + } else if (_4 === 1) { + const _9: rt.Iter = rt.iterFilter(rt.iterSlice(chars), (c: rt.char): boolean => { + return rt.charIsAsciiDigit(c); + }); + t.v = rt.stringExtendChars(t.v, _9); + } else { + t.v = rt.stringExtendChars(t.v, rt.iterOption(128512)); + } +} + +/** + * `String::drain` of `s` by the range `form` picks, between offsets from `a` and `b`, + * read from either end by `steps`' bits, then dropped, forgotten, or read to its end. + */ +export function drainBy(s: rt.Place, form: rt.u8, a: rt.u8, b: rt.u8, steps: rt.u8, out: rt.ByteVec): void { + let drain!: rt.Iter; + let _35!: rt.u8; + let c!: rt.char | undefined; + let _71: boolean = false; + const _9: [rt.usize, rt.usize] = [offset(s.v, a), offset(s.v, b)]; + const i: rt.usize = _9[0]; + const j: rt.usize = _9[1]; + const _17: rt.u8 = rt.u8Rem(form, 8); + if (_17 === 0) { + drain = rt.stringDrain(s, new rt.Range(i, j)); + _71 = true; + } else if (_17 === 1) { + drain = rt.stringDrain(s, { start: i }); + _71 = true; + } else if (_17 === 2) { + drain = rt.stringDrain(s, { end: j }); + _71 = true; + } else if (_17 === 3) { + drain = rt.stringDrain(s, undefined); + _71 = true; + } else if (_17 === 4) { + drain = rt.stringDrain(s, new rt.RangeInclusive(i, j, rt.usizeStep)); + _71 = true; + } else if (_17 === 5) { + drain = rt.stringDrain(s, { end: j, inclusive: true }); + _71 = true; + } else if (_17 === 6) { + drain = rt.stringDrain(s, [{ tag: "Excluded", _0: i } as rt.RangeBound, { tag: "Included", _0: j } as rt.RangeBound] as [rt.RangeBound, rt.RangeBound]); + _71 = true; + } else { + drain = rt.stringDrain(s, [{ tag: "Included", _0: i } as rt.RangeBound, { tag: "Unbounded" } as rt.RangeBound] as [rt.RangeBound, rt.RangeBound]); + _71 = true; + } + const _32: rt.u8 = rt.u8Rem(steps, 4); + let iterStart: rt.u8 = 0; + const iterEnd: rt.u8 = _32; + while ((_35 = iterStart++) < iterEnd) { + if ((rt.u8Shr(steps, rt.u8Add(2, _35)) & 1) === 0) { + c = rt.iterToOption(drain.next()); + } else { + c = rt.iterToOption(drain.nextBack()); + } + putChar(out, c); + } + const _49: rt.u8 = rt.u8Rem(rt.u8Div(form, 8), 4); + if (_49 !== 0) { + if (_49 === 1) { + _71 = false; + } else if (_49 === 2) { + _71 = false; + put(out, rt.stringExtendChars("", drain, true)); + } else { + put(out, rt.stringDrainAsStr(drain)); + _71 = false; + const iter2: rt.Iter = rt.iterRev(drain); + while (true) { + const _65: rt.char | rt.Done = iter2.next(); + if (_65 === rt.DONE) { + break; + } + putChar(out, _65); + } + rt.drop(iter2); + } + } + if (_71) { + rt.drop(drain); + } + _71 = false; +} + +/** + * `String::drain`s, four bytes each, on text of chars of every UTF-8 width: through a + * `&mut String`, on a local, and on a field, each printed after each drain. An offset off + * a char boundary or out of bounds panics as std's does. + */ +export function drains(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + const mid: number = rt.min(data.length, rt.u8Rem(kind, 8) as rt.usize); + if (mid > data.length) { + rt.panic("mid > len"); + } + const head: rt.ByteSlice = new rt.ByteSlice(data.data, data.start, mid); + let s: string = text(head); + const named: Named.Named = { name: text(head) }; + const iter: rt.Iter = rt.byteSliceChunks(new rt.ByteSlice(data.data, data.start + mid, data.length - mid), 4); + while (true) { + const _18: rt.ByteSlice | rt.Done = iter.next(); + if (_18 === rt.DONE) { + break; + } + rt.idx(_18, 0); + const _26: Uint8Array = Uint8Array.of(_18.data[_18.start], unwrapOr$0r2bmpr(rt.byteSliceGet(_18, 1), 0), unwrapOr$0r2bmpr(rt.byteSliceGet(_18, 2), 16), unwrapOr$0r2bmpr(rt.byteSliceGet(_18, 3), 0)); + const form: rt.u8 = _26[0]; + const a: rt.u8 = _26[1]; + const b: rt.u8 = _26[2]; + const steps: rt.u8 = _26[3]; + const _44: rt.u8 = rt.u8Rem(rt.u8Div(form, 32), 4); + if (_44 === 0) { + drainBy(rt.place(() => named.name, (value6) => named.name = value6), form, a, b, steps, out); + } else if (_44 === 1) { + const _49: rt.u8 = rt.u8Rem(form, 4); + if (_49 === 0) { + const _56: { end: rt.usize } = { end: offset(s, a) }; + put(out, rt.stringExtendChars("", rt.stringDrain(rt.place(() => s, (value5) => s = value5), _56), true)); + } else if (_49 === 1) { + const _64: { start: rt.usize } = { start: offset(s, a) }; + rt.byteVecPush(out, rt.ownedCount(rt.stringDrain(rt.place(() => s, (value4) => s = value4), _64)) & 0xff); + } else if (_49 === 2) { + putChar(out, rt.ownedLast(rt.stringDrain(rt.place(() => s, (value3) => s = value3), undefined))); + } else { + const _73: rt.Range = new rt.Range(offset(s, a), offset(s, b)); + rt.drop(rt.stringDrain(rt.place(() => s, (value2) => s = value2), _73)); + } + } else { + drainBy(rt.place(() => s, (value) => s = value), form, a, b, steps, out); + } + put(out, s); + put(out, named.name); + } +} + +/** + * Chars from ranges as long as `data`'s first two bytes pick, through the adaptors `kind` + * picks, into a `String` holding `aé` and the text of the rest: std's reserves the size + * hint's lower bound first, which panics past `isize::MAX` before it reads a char, and + * else the chars panic with `consumed` past the eighth. + */ +export function hinted(kind: rt.u8, data: rt.ByteSlice, out: rt.ByteVec): void { + let _70!: rt.usize; + const pick: (a0: rt.usize) => rt.u32 = (i: rt.usize): rt.u32 => { + const _4: rt.usize = rt.usizeRem(unwrapOr$0r2bmpr(rt.byteSliceGet(data, i), 0) as rt.usize, [0, 5, 8, 9, 2147483645, 2147483648, 4294967295].length); + if (!(_4 < 7)) { + rt.panic("index out of bounds: the len is " + 7 + " but the index is " + _4); + } + return [0, 5, 8, 9, 2147483645, 2147483648, 4294967295][_4]; + }; + const _7: [rt.u32, rt.u32] = [pick(0), pick(1)]; + const n: rt.u32 = _7[0]; + const m: rt.u32 = _7[1]; + const sub: rt.usize = rt.usizeDiv(unwrapOr$0r2bmpr(rt.byteSliceGet(data, 1), 0) as rt.usize, [0, 5, 8, 9, 2147483645, 2147483648, 4294967295].length); + let t: string = "aé"; + const _25: rt.Place = rt.place(() => t, (value) => t = value); + const _26: string = text(unwrapOr$1rbuegs(rt.byteSliceGetRange(data, 2), rt.byteSliceFrom(Uint8Array.of()))); + _25.v = _25.v + _26; + const calls: rt.Cell = new rt.Cell(0); + const f: (a0: rt.u32) => rt.char = (_2: rt.u32): rt.char => { + rt.cellSet(calls, rt.usizeAdd(calls.v, 1)); + if (!(calls.v <= 8)) { + rt.panicFmt(["consumed"]); + } else { + const _127: rt.usize = calls.v; + if (!(_127 < 16)) { + rt.panic("index out of bounds: the len is " + 16 + " but the index is " + _127); + } + return [47, 92, 97, 48, 57, 127, 128, 233, 2047, 2048, 55295, 57344, 65535, 65536, 128512, 1114111][_127]; + } + }; + const _38: rt.u8 = rt.u8Rem(kind, 10); + if (_38 === 0) { + const _40: rt.Place = rt.place(() => t, (value13) => t = value13); + const _41: rt.Iter = rt.iterMap(new rt.Range(0, n), f); + _40.v = rt.stringExtendChars(_40.v, _41); + } else if (_38 === 1) { + t = rt.stringExtendChars("", rt.iterMap(rt.iterChain(new rt.RangeInclusive(0, n, rt.u32Step), new rt.Range(0, n)), f)); + } else if (_38 === 2) { + const _48: rt.Place = rt.place(() => t, (value12) => t = value12); + const _49: rt.Iter = rt.iterSkip(rt.iterMap(new rt.Range(0, rt.u64Or(rt.u64Shl(n as rt.u64, 8), m as rt.u64), rt.u64Step), (i4: rt.u64): rt.char => { + return f(rt.u64Low(i4)); + }), m as rt.usize); + _48.v = rt.stringExtendChars(_48.v, _49); + } else if (_38 === 3) { + const _61: rt.Place = rt.place(() => t, (value11) => t = value11); + const _62: rt.Iter = rt.iterTake(rt.iterMap(new rt.Range(0, n), f), m as rt.usize); + _61.v = rt.stringExtendChars(_61.v, _62); + } else if (_38 === 4) { + const _66: rt.Place = rt.place(() => t, (value10) => t = value10); + const _69: rt.Range = new rt.Range(0, n); + if (!(n === 4294967295)) { + _70 = 1; + } else { + _70 = rt.usizeAdd(1, rt.usizeRem(m as rt.usize, 2)); + } + const _67: rt.Iter = rt.iterMap(rt.iterStepBy(_69, _70), f); + _66.v = rt.stringExtendChars(_66.v, _67); + } else if (_38 === 5) { + if (rt.u32Rem(m, 2) === 0) { + const _78: rt.Place = rt.place(() => t, (value9) => t = value9); + const _79: rt.Iter = rt.iterChain(rt.iterMap(new rt.Range(0, n), f), rt.iterMap(new rt.Range(0, m), f)); + _78.v = rt.stringExtendChars(_78.v, _79); + } else { + const _84: rt.Place = rt.place(() => t, (value8) => t = value8); + const _85: rt.Iter = rt.iterMap(rt.iterZip(new rt.Range(0, n), new rt.Range(0, m), true), (_211: [rt.u32, rt.u32]): rt.char => { + return f(_211[0]); + }); + _84.v = rt.stringExtendChars(_84.v, _85); + } + } else if (_38 === 6) { + const _91: rt.Place = rt.place(() => t, (value7) => t = value7); + const _92: rt.Iter = rt.iterFlatMap(new rt.Range(0, n), (i2: rt.u32): rt.char[] => { + return [f(i2), f(i2)]; + }, (value: rt.char[]) => rt.iterSlice(value), false, 2); + _91.v = rt.stringExtendChars(_91.v, _92); + } else if (_38 === 7) { + const _96: rt.Place = rt.place(() => t, (value6) => t = value6); + const _97: rt.Iter = rt.iterChain(rt.iterChain(rt.iterChain(rt.iterChain(rt.iterMap(new rt.Range(0, rt.u32SaturatingSub(n, 5)), f), rt.iterOnce(120)), rt.iterOption(121)), rt.strChars("é😀")), rt.iterSlice([122])); + _96.v = rt.stringExtendChars(_96.v, _97); + } else if (_38 === 8) { + const _112: rt.usize = rt.usizeRem(sub, 3); + if (_112 === 0) { + const _114: rt.Place = rt.place(() => t, (value5) => t = value5); + const _115: rt.Iter = rt.iterMap(new rt.Range({ hi: 0x10000000, lo: 0 } as rt.u64, rt.u64Add({ hi: 0x10000000, lo: 0 } as rt.u64, n as rt.u64), rt.u64Step), (_210: rt.u64): rt.char => { + return f(0); + }); + _114.v = rt.stringExtendChars(_114.v, _115); + } else if (_112 === 1) { + const _128: rt.Place = rt.place(() => t, (value4) => t = value4); + const _129: rt.Iter = rt.iterMap(new rt.Range(rt.i128Neg(rt.i128Shl(1, 100)), rt.i128Add(rt.i128Neg(rt.i128Shl(1, 100)), n as rt.i128), rt.i128Step), (_27: rt.i128): rt.char => { + return f(0); + }); + _128.v = rt.stringExtendChars(_128.v, _129); + } else { + const _146: rt.Place = rt.place(() => t, (value3) => t = value3); + const _147: rt.Iter = rt.iterMap(new rt.RangeInclusive({ hi: 0x10000000, lo: 0 } as rt.u64, rt.u64Add({ hi: 0x10000000, lo: 0 } as rt.u64, n as rt.u64), rt.u64Step), (_24: rt.u64): rt.char => { + return f(0); + }); + _146.v = rt.stringExtendChars(_146.v, _147); + } + } else { + const chars: rt.Peekable = rt.iterPeekable(rt.iterFuse(rt.iterRev(rt.iterMap(rt.iterEnumerate(rt.iterMap(new rt.Range(0, n), f)), (_23: [rt.usize, rt.char]): rt.char => { + return _23[1]; + })))); + putChar(out, rt.peekablePeek(chars)); + const _172: rt.Place = rt.place(() => t, (value2) => t = value2); + _172.v = rt.stringExtendChars(_172.v, chars); + } + put(out, t); +} + //# sourceMappingURL=strs.ts.map diff --git a/ts/fixtures/lang/src/harness.rs b/ts/fixtures/lang/src/harness.rs index dd11a6943..92622d60d 100644 --- a/ts/fixtures/lang/src/harness.rs +++ b/ts/fixtures/lang/src/harness.rs @@ -148,6 +148,9 @@ fn cases() -> Vec<(&'static str, Case)> { ("pointers::writers", |_, data, out| pointers::writers(data, out)), ("errors::eqs", errors::eqs), ("slices::edits", slices::edits), + ("strs::iters", strs::iters), + ("strs::drains", strs::drains), + ("strs::hinted", strs::hinted), ] } diff --git a/ts/fixtures/lang/src/strs.rs b/ts/fixtures/lang/src/strs.rs index 1962e8331..1c1698b04 100644 --- a/ts/fixtures/lang/src/strs.rs +++ b/ts/fixtures/lang/src/strs.rs @@ -645,3 +645,253 @@ pub fn parses(kind: u8, data: &[u8], out: &mut Vec) { } } } + +/// A crate's own iterator over `items`, which counts each `next` it is asked. +struct Counted<'a, T> { + items: &'a [T], + nexts: &'a std::cell::Cell, +} + +impl Iterator for Counted<'_, T> { + type Item = T; + + fn next(&mut self) -> Option { + self.nexts.set(self.nexts.get() + 1); + let (first, rest) = self.items.split_first()?; + self.items = rest; + Some(first.clone()) + } +} + +/// `String` made from and extended by iterators of chars and of strings, each the kind +/// picks, from text of chars of every UTF-8 width and its parts between `/`s. +pub fn iters(kind: u8, data: &[u8], out: &mut Vec) { + let s = text(data); + let chars: Vec = s.chars().collect(); + let parts: Vec<&str> = s.split('/').collect(); + let nexts = std::cell::Cell::new(0); + let mut named = Named { + name: text(&data[..data.len() / 2]), + }; + match kind % 4 { + 0 => { + put(out, &s.chars().collect::()); + put( + out, + &s.chars().rev().filter(|c| !c.is_ascii_digit()).collect::(), + ); + put(out, &chars.iter().collect::()); + put(out, &String::from_iter(chars.iter().copied().skip(1))); + put(out, &std::iter::empty::().collect::()); + put(out, &char::from_u32(u32::from(kind)).into_iter().collect::()); + let counted = Counted { + items: &chars, + nexts: &nexts, + }; + put(out, &counted.collect::()); + } + 1 => { + put(out, &parts.iter().copied().collect::()); + put(out, &s.split('/').rev().collect::()); + put(out, &parts.iter().map(|p| p.to_string()).collect::()); + put( + out, + &s.split('/') + .map(|p| format!("{p}{p}")) + .filter(|p| !p.is_empty()) + .collect::(), + ); + put(out, &std::iter::empty::().collect::()); + let cow: std::borrow::Cow = s.chars().rev().collect(); + put(out, &cow); + let boxed: Box = parts.iter().copied().collect(); + put(out, &boxed); + let owned: Vec = parts.iter().map(|p| p.to_string()).collect(); + put( + out, + &owned + .iter() + .map(|p| std::borrow::Cow::Borrowed(p.as_str())) + .collect::(), + ); + let counted = Counted { + items: &owned, + nexts: &nexts, + }; + put(out, &counted.collect::()); + } + 2 => { + // Through a `&mut`, on a local, and on a field. + let mut t = String::from("é"); + extend_chars(&mut t, &chars, kind); + t.extend(s.chars().rev()); + named.name.extend(&chars); + named.name.extend(std::iter::empty::()); + let counted = Counted { + items: &chars, + nexts: &nexts, + }; + t.extend(counted); + put(out, &t); + } + _ => { + let mut t = String::new(); + t.extend(s.split('/')); + t.extend(parts.iter().map(|p| format!("{p}é"))); + named.name.extend(parts.iter().rev().copied()); + named + .name + .extend(parts.iter().map(|p| std::borrow::Cow::Owned(p.to_string()))); + named.name.extend(std::iter::empty::<&str>()); + let counted = Counted { + items: &parts, + nexts: &nexts, + }; + t.extend(counted); + put(out, &t); + } + } + put(out, &named.name); + out.push(nexts.get()); +} + +fn extend_chars(t: &mut String, chars: &[char], kind: u8) { + match kind / 4 % 3 { + 0 => t.extend(chars.iter()), + 1 => t.extend(chars.iter().copied().filter(|c| c.is_ascii_digit())), + _ => t.extend(Some('😀')), + } +} + +/// `String::drain` of `s` by the range `form` picks, between offsets from `a` and `b`, +/// read from either end by `steps`' bits, then dropped, forgotten, or read to its end. +fn drain_by(s: &mut String, form: u8, a: u8, b: u8, steps: u8, out: &mut Vec) { + let (i, j) = (offset(s, a), offset(s, b)); + let mut drain = match form % 8 { + 0 => s.drain(i..j), + 1 => s.drain(i..), + 2 => s.drain(..j), + 3 => s.drain(..), + 4 => s.drain(i..=j), + 5 => s.drain(..=j), + 6 => s.drain((Bound::Excluded(i), Bound::Included(j))), + _ => s.drain((Bound::Included(i), Bound::Unbounded)), + }; + // Up to three chars, each from the front or the back by a bit of `steps`. + for k in 0..steps % 4 { + let c = match steps >> (2 + k) & 1 { + 0 => drain.next(), + _ => drain.next_back(), + }; + put_char(out, c); + } + match form / 8 % 4 { + 0 => {} + // Forgotten, a drain leaves the string as it was. + 1 => std::mem::forget(drain), + 2 => put(out, &drain.collect::()), + _ => { + put(out, drain.as_str()); + for c in drain.rev() { + put_char(out, Some(c)); + } + } + } +} + +/// `String::drain`s, four bytes each, on text of chars of every UTF-8 width: through a +/// `&mut String`, on a local, and on a field, each printed after each drain. An offset off +/// a char boundary or out of bounds panics as std's does. +pub fn drains(kind: u8, data: &[u8], out: &mut Vec) { + let (head, ops) = data.split_at(data.len().min(usize::from(kind % 8))); + let mut s = text(head); + let mut named = Named { name: text(head) }; + for op in ops.chunks(4) { + let [form, a, b, steps] = [ + op[0], + *op.get(1).unwrap_or(&0), + *op.get(2).unwrap_or(&0x10), + *op.get(3).unwrap_or(&0), + ]; + match form / 32 % 4 { + 0 => drain_by(&mut named.name, form, a, b, steps, out), + // On the local itself, with no `&mut` between. + 1 => match form % 4 { + 0 => put(out, &s.drain(..offset(&s, a)).collect::()), + 1 => out.push(s.drain(offset(&s, a)..).count() as u8), + 2 => put_char(out, s.drain(..).last()), + _ => { + s.drain(offset(&s, a)..offset(&s, b)); + } + }, + _ => drain_by(&mut s, form, a, b, steps, out), + } + put(out, &s); + put(out, &named.name); + } +} + +/// Range lengths whose size hint, through `hinted`'s adaptors, is either a few chars or +/// past where `isize::MAX` bytes of text start: std's `String` would reserve any between, +/// up to 2 GiB. +const HINTED: [u32; 7] = [0, 5, 8, 9, 0x7fff_fffd, 0x8000_0000, u32::MAX]; + +/// Chars from ranges as long as `data`'s first two bytes pick, through the adaptors `kind` +/// picks, into a `String` holding `aé` and the text of the rest: std's reserves the size +/// hint's lower bound first, which panics past `isize::MAX` before it reads a char, and +/// else the chars panic with `consumed` past the eighth. +pub fn hinted(kind: u8, data: &[u8], out: &mut Vec) { + let pick = |i: usize| HINTED[usize::from(data.get(i).copied().unwrap_or(0)) % HINTED.len()]; + let (n, m) = (pick(0), pick(1)); + let sub = usize::from(data.get(1).copied().unwrap_or(0)) / HINTED.len(); + let mut t = String::from("aé"); + t.push_str(&text(data.get(2..).unwrap_or(&[]))); + let calls = std::cell::Cell::new(0usize); + let f = |_: u32| { + calls.set(calls.get() + 1); + assert!(calls.get() <= 8, "consumed"); + ALPHABET[calls.get()] + }; + match kind % 10 { + 0 => t.extend((0..n).map(f)), + 1 => t = (0..=n).chain(0..n).map(f).collect(), + 2 => t.extend( + (0..u64::from(n) << 8 | u64::from(m)) + .map(|i| f(i as u32)) + .skip(m as usize), + ), + 3 => t.extend((0..n).map(f).take(m as usize)), + // Only twice the longest is past `isize::MAX` stepped by two. + 4 => t.extend( + (0..n) + .step_by(if n == u32::MAX { 1 + m as usize % 2 } else { 1 }) + .map(f), + ), + 5 => match m % 2 { + 0 => t.extend((0..n).map(f).chain((0..m).map(f))), + _ => t.extend((0..n).zip(0..m).map(|(i, _)| f(i))), + }, + 6 => t.extend((0..n).flat_map(|i| [f(i), f(i)])), + // Sources whose hint is a few chars, which make it `n` only if each counts its own. + 7 => t.extend( + (0..n.saturating_sub(5)) + .map(f) + .chain(std::iter::once('x')) + .chain(Some('y')) + .chain("é😀".chars()) + .chain(['z'].iter().copied()), + ), + // Integers past 2^53. + 8 => match sub % 3 { + 0 => t.extend((1u64 << 60..(1u64 << 60) + u64::from(n)).map(|_| f(0))), + 1 => t.extend((-(1i128 << 100)..-(1i128 << 100) + i128::from(n)).map(|_| f(0))), + _ => t.extend((1u64 << 60..=(1u64 << 60) + u64::from(n)).map(|_| f(0))), + }, + _ => { + let mut chars = (0..n).map(f).enumerate().map(|(_, c)| c).rev().fuse().peekable(); + put_char(out, chars.peek().copied()); + t.extend(chars); + } + } + put(out, &t); +} diff --git a/ts/fixtures/task/expected/drops/closures.ts b/ts/fixtures/task/expected/drops/closures.ts index 32688f68b..a0b5b00a6 100644 --- a/ts/fixtures/task/expected/drops/closures.ts +++ b/ts/fixtures/task/expected/drops/closures.ts @@ -498,7 +498,7 @@ export function iterated(log: Log.Log, id: rt.u32): void { return [_2, Guard.id(captured8)]; }, (): void => { captured8[Symbol.dispose](); - }), (value: rt.u32[]) => rt.iterSlice(value), true); + }), (value: rt.u32[]) => rt.iterSlice(value), true, 2); while (rt.iterToOption(ended.next()) !== undefined) { } Log.note(log, rt.fmtFormat(["flat_map ended"])); diff --git a/ts/runtime/rt/collections.ts b/ts/runtime/rt/collections.ts index 1bca021fa..eac9474cc 100644 --- a/ts/runtime/rt/collections.ts +++ b/ts/runtime/rt/collections.ts @@ -650,6 +650,10 @@ export class HashIter extends Iter implements Disposable { return this.left; } + override lower(): number { + return this.len(); + } + [Symbol.dispose](): void { if (this.k === undefined && this.v === undefined) return; const n = this.left; @@ -1291,6 +1295,11 @@ export class BTreeIter extends Iter implements Disposable { return this.n; } + override lower(): number { + // A range's count is unknown, and std's hint of one is 0. + return Math.max(this.n, 0); + } + [Symbol.dispose](): void { if (this.k === undefined && this.v === undefined) return; const keys: K[] = []; @@ -1892,6 +1901,10 @@ class DequeIter extends Iter implements Disposable { return this.back - this.front; } + override lower(): number { + return this.len(); + } + [Symbol.dispose](): void { if (this.drop === undefined) return; const items: T[] = []; @@ -1939,6 +1952,10 @@ class DequePlaces extends Iter> { override len(): number { return this.back - this.front; } + + override lower(): number { + return this.len(); + } } /** `drain`: the items within `r`, taken out at once, which drop by `drop` as `dequeIter`'s. */ @@ -2050,6 +2067,10 @@ class VecDrain extends Iter implements Disposable { return this.back - this.front; } + override lower(): number { + return this.len(); + } + [Symbol.dispose](): void { const from = this.front; this.front = this.back; @@ -2099,6 +2120,10 @@ class ByteVecDrain extends Iter implements Disposable { return this.back - this.front; } + override lower(): number { + return this.len(); + } + [Symbol.dispose](): void { if (this.length < 0) return; this.v.data.copyWithin(this.start, this.end, this.length); diff --git a/ts/runtime/rt/iter.ts b/ts/runtime/rt/iter.ts index 014ec93d3..c172138ae 100644 --- a/ts/runtime/rt/iter.ts +++ b/ts/runtime/rt/iter.ts @@ -21,7 +21,7 @@ import { ByteSlice, ByteVec, byteVecPush, Err, type None, Ok, type Place, panic, place, type RangeBound, type Result, sliceView } from "../rt.ts"; import { AtIndex, BTreeIter, HashIter, type Ordering } from "./collections.ts"; -import { type Limbs, lt as wideLt, type Step } from "./int.ts"; +import { intToBig, type Limbs, lt as wideLt, type Step } from "./int.ts"; import { drop as dispose, dropRange } from "./task.ts"; /** What `next` returns once an iterator is exhausted, which no item is. */ @@ -36,6 +36,11 @@ const USIZE_MAX = 0xffffffff; /** What drops a value. */ type Drop = (value: T) => void; +/** `a + b`, saturating at `usize::MAX` as std's size hints do. */ +function satAdd(a: number, b: number): number { + return Math.min(a + b, USIZE_MAX); +} + /** * What only the iterators std implements these traits for have, which Rust calls only * there: declared, not stubbed, so no bundle carries a stub. @@ -87,6 +92,15 @@ export abstract class Iter implements Iterable { return this; } + /** + * `size_hint`'s lower bound, which std's `String` reserves before it reads chars: exact + * for a range and what adapts one, the only iterators whose bound reaches `isize::MAX`, + * and else 0. + */ + lower(): number { + return 0; + } + /** * `fold`, as std's of its kind, which takes the iterator by value and drops what it * holds as std's does. `release` drops what `f` owns, as std drops the closure it was @@ -481,16 +495,19 @@ export function iterPeekable(it: Iter, drop?: Drop): Peekable { * source drops once it ends unless it is `fused`, as `fuse` does. */ export function iterFlatMap(it: Iter, f: (item: T) => Iter): Iter; -export function iterFlatMap(it: Iter, f: (item: T) => U, into?: (value: U) => Iter, fused?: boolean): Iter; -export function iterFlatMap(it: Iter, f: (item: T) => U, into?: (value: U) => Iter, fused = false): Iter { - return new FlattenIter(new MapIter(it, f), into ?? (itself as (value: U) => Iter), fused); +export function iterFlatMap(it: Iter, f: (item: T) => U, into?: (value: U) => Iter, fused?: boolean, size?: number): Iter; +export function iterFlatMap(it: Iter, f: (item: T) => U, into?: (value: U) => Iter, fused = false, size?: number): Iter { + return new FlattenIter(new MapIter(it, f), into ?? (itself as (value: U) => Iter), fused, size); } -/** `flatten`: `into` makes each item an iterator, as `IntoIterator` does, and `fused` is as `flat_map`'s. */ +/** + * `flatten`: `into` makes each item an iterator, as `IntoIterator` does, `fused` is as + * `flat_map`'s, and `size` is each item's length if it is an array. + */ export function iterFlatten(it: Iter>): Iter; -export function iterFlatten(it: Iter, into?: (value: T) => Iter, fused?: boolean): Iter; -export function iterFlatten(it: Iter, into?: (value: T) => Iter, fused = false): Iter { - return new FlattenIter(it, into ?? (itself as (value: T) => Iter), fused); +export function iterFlatten(it: Iter, into?: (value: T) => Iter, fused?: boolean, size?: number): Iter; +export function iterFlatten(it: Iter, into?: (value: T) => Iter, fused = false, size?: number): Iter { + return new FlattenIter(it, into ?? (itself as (value: T) => Iter), fused, size); } // Consumers. Each that takes an iterator by value that `drops` takes it as std's does: by @@ -1039,6 +1056,10 @@ class SliceIter extends Iter implements Disposable { return this.back - this.front; } + override lower(): number { + return this.len(); + } + [Symbol.dispose](): void { if (this.drop === undefined) return; const from = this.front; @@ -1088,6 +1109,10 @@ class SliceMutIter extends Iter> { return this.back - this.front; } + override lower(): number { + return this.len(); + } + override advanceBy(n: number): number { const step = Math.min(n, this.back - this.front); this.front += step; @@ -1158,6 +1183,10 @@ class PartsIter extends Iter { return this.back - this.front; } + override lower(): number { + return this.len(); + } + override advanceBy(n: number): number { const step = Math.min(n, this.back - this.front); this.front += step; @@ -1200,6 +1229,10 @@ class OptionIter extends Iter implements Disposable { return this.some ? 1 : 0; } + override lower(): number { + return this.len(); + } + [Symbol.dispose](): void { if (this.drop === undefined) return; const item = this.next(); @@ -1252,6 +1285,10 @@ class SuccessorsIter extends Iter implements Disposable { return item; } + override lower(): number { + return this.item === undefined ? 0 : 1; + } + [Symbol.dispose](): void { if (this.dropped) return; this.dropped = true; @@ -1274,6 +1311,10 @@ class Borrowed extends Iter { this.inner = inner; } + override lower(): number { + return this.inner.lower(); + } + next(): T | Done { return this.inner.next(); } @@ -1313,6 +1354,10 @@ class MapIter extends Calling U> { super(inner, f); } + override lower(): number { + return this.inner.lower(); + } + next(): U | Done { const item = this.inner.next(); return item === DONE ? DONE : this.f(item); @@ -1435,6 +1480,10 @@ class FilterMapIter extends Calling U | undefined> { class EnumerateIter extends Adaptor { index = 0; + override lower(): number { + return this.inner.lower(); + } + next(): [number, T] | Done { const item = this.inner.next(); return item === DONE ? DONE : [this.index++, item]; @@ -1513,6 +1562,10 @@ class ChainIter extends Iter implements Disposable { dispose(b); } + override lower(): number { + return satAdd(this.a?.lower() ?? 0, this.b?.lower() ?? 0); + } + next(): T | Done { if (this.a !== undefined) { const item = this.a.next(); @@ -1645,6 +1698,10 @@ class ZipIter extends Iter<[A, B]> implements Disposable { return Math.min(this.a.len(), this.b.len()); } + override lower(): number { + return Math.min(this.a.lower(), this.b.lower()); + } + [Symbol.dispose](): void { dispose(this.a); dispose(this.b); @@ -1679,6 +1736,10 @@ class TrustedZipIter extends ZipIter { } class RevIter extends Adaptor { + override lower(): number { + return this.inner.lower(); + } + next(): T | Done { return this.inner.nextBack(); } @@ -1727,6 +1788,10 @@ class SkipIter extends Adaptor { this.drop = drop; } + override lower(): number { + return Math.max(this.inner.lower() - this.n, 0); + } + next(): T | Done { if (this.n === 0) return this.inner.next(); const n = this.n; @@ -1811,6 +1876,10 @@ class TakeIter extends Adaptor { this.drop = drop; } + override lower(): number { + return this.n === 0 ? 0 : Math.min(this.inner.lower(), this.n); + } + next(): T | Done { if (this.n === 0) return DONE; this.n--; @@ -1987,7 +2056,15 @@ class StepByIter extends Adaptor { } override len(): number { - const n = this.inner.len(); + return this.size(this.inner.len()); + } + + override lower(): number { + return this.size(this.inner.lower()); + } + + /** How many of `n` items it yields. */ + private size(n: number): number { if (this.firstTake) return n === 0 ? 0 : 1 + Math.floor((n - 1) / (this.step + 1)); return Math.floor(n / (this.step + 1)); } @@ -2054,6 +2131,10 @@ class StepByIter extends Adaptor { } class InspectIter extends Calling void> { + override lower(): number { + return this.inner.lower(); + } + next(): T | Done { return this.inspect(this.inner.next()); } @@ -2102,6 +2183,10 @@ class FuseIter extends Iter implements Disposable { this.fused = fused; } + override lower(): number { + return this.inner?.lower() ?? 0; + } + /** `item`, dropping its iterator first if it ended there and is not fused. */ clear(item: T | Done): T | Done { if (item === DONE && !this.fused) { @@ -2163,14 +2248,24 @@ class FlattenIter extends Iter implements Disposable { outer: Iter | undefined; readonly into: (value: T) => Iter; readonly fused: boolean; + /** How many items each of its source's has, if each is an array. */ + readonly size: number | undefined; front: Iter | undefined; back: Iter | undefined; - constructor(outer: Iter, into: (value: T) => Iter, fused: boolean) { + constructor(outer: Iter, into: (value: T) => Iter, fused: boolean, size?: number) { super(); this.outer = outer; this.into = into; this.fused = fused; + this.size = size; + } + + /** std's: what its front and back hold, and its source's arrays, if it knows their size. */ + override lower(): number { + const held = satAdd(this.front?.lower() ?? 0, this.back?.lower() ?? 0); + if (this.size === undefined) return held; + return satAdd((this.outer?.lower() ?? 0) * this.size, held); } /** The source's next item, or `DONE`, which drops it unless it is fused. */ @@ -2342,6 +2437,12 @@ export class Peekable extends Adaptor { return item; } + /** std's: none past a peeked end, and one more than its iterator's for a peeked item. */ + override lower(): number { + if (!this.peeked) return this.inner.lower(); + return this.item === DONE ? 0 : satAdd(this.inner.lower(), 1); + } + next(): T | Done { if (!this.peeked) return this.inner.next(); return this.take(); @@ -2494,6 +2595,13 @@ function step(n: T, by: number, int: Step | undefined): T | undefined { return over ? undefined : sum; } +/** std's `Step::steps_between` of `a` and `b`, at most `b`, saturating at `usize::MAX`. */ +function stepsBetween(a: T, b: T): number { + if (typeof a === "number" && typeof b === "number") return Math.min(b - a, USIZE_MAX); + const n = intToBig(b as number | Limbs) - intToBig(a as number | Limbs); + return n < USIZE_MAX ? Number(n) : USIZE_MAX; +} + /** * `ops::Range`: `start..end`, which iterates its integers by moving `start`. A 64- or * 128-bit range takes its type's `Step`, `rt.u64Step`, to step past 2^53. @@ -2548,6 +2656,10 @@ export class Range extends Iter { override len(): number { return lt(this.start, this.end) ? Number(this.end) - Number(this.start) : 0; } + + override lower(): number { + return lt(this.start, this.end) ? stepsBetween(this.start, this.end) : 0; + } } /** `Range::is_empty`. */ @@ -2603,6 +2715,10 @@ export class RangeInclusive extends Iter { return rangeInclusiveIsEmpty(this) ? 0 : Number(this.hi) - Number(this.lo) + 1; } + override lower(): number { + return rangeInclusiveIsEmpty(this) ? 0 : satAdd(stepsBetween(this.lo, this.hi), 1); + } + /** std's `nth`: `start` moves past the item, wrapping if it overflows, which ends the range. */ override nth(n: number): T | Done { if (rangeInclusiveIsEmpty(this)) return DONE; diff --git a/ts/runtime/rt/str.ts b/ts/runtime/rt/str.ts index 916086dec..3b375fcaa 100644 --- a/ts/runtime/rt/str.ts +++ b/ts/runtime/rt/str.ts @@ -6,10 +6,11 @@ // 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 { fmtCharDebug, type Place, panic, panicFmt, stringLen } from "../rt.ts"; import { type Bounds, boundsRange } from "./collections.ts"; import type { char, u8, usize } from "./int.ts"; -import { DONE, type Done, Iter, iterSlice } from "./iter.ts"; +import { DONE, type Done, Iter, iterForEach, iterSlice } from "./iter.ts"; +import { drop as dispose } from "./task.ts"; const encoder = /* @__PURE__ */ new TextEncoder(); @@ -119,6 +120,16 @@ class Chars extends Iter { this.#back = before(this.#s, this.#back); return this.#s.codePointAt(this.#back) as char; } + + /** std's: a char per four bytes of what is left. */ + override lower(): number { + return Math.ceil(utf8Len(this.#s, this.#front, this.#back) / 4); + } + + /** `as_str`: what is left. */ + rest(): string { + return this.#s.slice(this.#front, this.#back); + } } /** @@ -450,3 +461,79 @@ export function stringSplitOff(s: string, i: usize): [string, string] { const at = boundary(s, i, "assertion failed: self.is_char_boundary(at)"); return [s.slice(0, at), s.slice(at)]; } + +// `String` from and into iterators. Each reads the iterator by `for_each`, as std's does, +// which `drops` makes its own `fold`. + +/** + * `Extend` and `Extend<&char>` of `s`, and `collect` of chars from `""`: `s` and each + * char, once it reserved the iterator's lower bound, which panics past `isize::MAX`. + */ +export function stringExtendChars(s: string, it: Iter, drops = false): string { + stringReserve(s, it.lower()); + iterForEach( + it, + (c) => { + s += String.fromCodePoint(c); + }, + drops, + ); + return s; +} + +/** `Extend` of `s` by strings, `&str`, `String`, or `Cow`, and `collect` of `&str`s from `""`. */ +export function stringExtend(s: string, it: Iter, drops = false): string { + iterForEach( + it, + (text) => { + s += text; + }, + drops, + ); + return s; +} + +/** `collect` of owned strings, a `String` or `Cow`: the first by `next`, then the rest extend it. */ +export function stringFromStrings(it: Iter, drops = false): string { + const first = it.next(); + if (first !== DONE) return stringExtend(first, it, drops); + if (drops) dispose(it); + return ""; +} + +/** + * `String::drain`: the chars within the byte range `r` of the string at `s`, which leaves + * the string without them as it drops, and as it was if it never does, as `mem::forget` + * leaves it. + */ +export function stringDrain(s: Place, r: Bounds): Iter { + const text = s.v; + const [start, end] = boundsRange(r, utf8Len(text, 0, text.length)); + const from = boundary(text, start, "assertion failed: self.is_char_boundary(start)"); + const to = boundary(text, end, "assertion failed: self.is_char_boundary(end)"); + return new StringDrain(s, text, from, to); +} + +/** `Drain::as_str`: the chars `drain` has yet to yield. */ +export function stringDrainAsStr(drain: Iter): string { + return (drain as Chars).rest(); +} + +class StringDrain extends Chars implements Disposable { + readonly #s: Place; + readonly #text: string; + readonly #from: number; + readonly #to: number; + + constructor(s: Place, text: string, from: number, to: number) { + super(text.slice(from, to)); + this.#s = s; + this.#text = text; + this.#from = from; + this.#to = to; + } + + [Symbol.dispose](): void { + this.#s.v = this.#text.slice(0, this.#from) + this.#text.slice(this.#to); + } +} diff --git a/ts/src/lower/drop.rs b/ts/src/lower/drop.rs index 981a5b507..b779a8c1d 100644 --- a/ts/src/lower/drop.rs +++ b/ts/src/lower/drop.rs @@ -65,8 +65,15 @@ impl<'a, 'tcx> Translator<'a, 'tcx> { ) => args.types().any(|t| self.drops(t)), // An entry borrows its map, and holds only the key it was given. Some(Std::Entry) => args.types().next().is_some_and(|k| self.drops(k)), - // A `Vec`'s drain moves the vector's tail back as it drops. - None if library::std_path(tcx, adt.did()) == ["vec", "drain", "Drain"] => true, + // A `Vec`'s drain moves the vector's tail back as it drops, and a `String`'s takes + // the drained text out of it. + None if { + let path = library::std_path(tcx, adt.did()); + path == ["vec", "drain", "Drain"] || path == ["string", "Drain"] + } => + { + true + } None | Some(Std::Peekable) if self.std_iter(ty) => match adt.has_dtor(tcx) { // What owns its items, an `IntoIter` or a `Drain`, drops those it still holds. true => args.types().any(|t| self.drops(t)), diff --git a/ts/src/lower/iter.rs b/ts/src/lower/iter.rs index 4bbfc66b1..cba617bcd 100644 --- a/ts/src/lower/iter.rs +++ b/ts/src/lower/iter.rs @@ -325,6 +325,14 @@ pub fn call<'tcx>( _ => {} } + // What `collect` calls. + if (owner, name) == ("FromIterator", "from_iter") { + let source = targ(2)?; + let iter = iter_of(b.t, source)?; + let items = into_iter(b, source, args.pop()?, span)?; + return collect(b, targ(0)?, targ(1)?, items, iter, span); + } + // `Vec::extend` with an iterator, which it drops once done. if (owner, name) == ("Extend", "extend") { let (vec, item, source) = (targ(0)?, targ(1)?, targ(2)?); @@ -594,14 +602,16 @@ pub fn call<'tcx>( flattens(b, inner, span); let into = into_fn(b, targ(1)?, span)?.unwrap_or_else(Expr::undefined); let fused = fused(b, self_ty, Some(targ(2)?)); - method("flatMap", tail(rest, vec![into, fused])) + let size = array_size(b, targ(1)?); + method("flatMap", tail(rest, vec![into, fused, size])) } "flatten" => { let inner = iter_of(b.t, item)?; flattens(b, inner, span); let into = into_fn(b, item, span)?.unwrap_or_else(Expr::undefined); let fused = fused(b, self_ty, None); - method("flatten", tail(Vec::new(), vec![into, fused])) + let size = array_size(b, item); + method("flatten", tail(Vec::new(), vec![into, fused, size])) } "copied" | "cloned" => { let ty::Ref(_, inner, _) = item.kind() else { @@ -659,6 +669,21 @@ fn fused<'tcx>(b: &mut Body<'_, '_, 'tcx>, ty: Ty<'tcx>, held: Option>) }) } +/// How many items `ty` has, if it is an array or a reference to one, which std's `flatten` +/// counts toward its size hint: else `undefined`. +fn array_size<'tcx>(b: &Body<'_, '_, 'tcx>, ty: Ty<'tcx>) -> Expr { + let ty = match *ty.kind() { + ty::Ref(_, inner, _) => inner, + _ => ty, + }; + match ty.kind() { + ty::Array(_, len) => len + .try_to_target_usize(b.t.tcx) + .map_or_else(Expr::undefined, |n| Expr::lit(n.to_string())), + _ => Expr::undefined(), + } +} + /// The path of the std type `ty` is, if it is one. fn std_iter_path(tcx: ty::TyCtxt<'_>, ty: Ty<'_>) -> Option> { let ty::Adt(adt, _) = ty.kind() else { @@ -822,10 +847,48 @@ fn collect<'tcx>( let args = vec![iter, Expr::Lambda(vec![param], Box::new(values))]; Some(Expr::rt(collect, tail(args, vec![dropper]))) } + // From chars or text, as `extend` of an empty one, but for an owned string's, which + // the rest extend once `next` gives it. A `Cow` and a `Box` own the `String` + // std's collects. + Std::String | Std::Cow | Std::Box if std == Std::String || args.types().next().is_some_and(|t| t.is_str()) => { + match b.t.std_of(item) { + Some(Std::String | Std::Cow) => { + let drops = iter_drops(b, from, span); + Some(Expr::rt("stringFromStrings", tail(vec![iter], vec![drops]))) + } + _ => string_extend(b, Expr::Str(String::new()), iter, item, from, span), + } + } _ => super::collections::collect(b, ty, iter, from, span), } } +/// `s` extended by `items`, an iterator of type `iter` of `item`s, as `String`'s `Extend` +/// reads it, by `for_each`: of chars, through any `&`, or of text, a `&str`, `String`, +/// `Box`, or `Cow`. +pub(super) fn string_extend<'tcx>( + b: &mut Body<'_, '_, 'tcx>, + s: Expr, + items: Expr, + item: Ty<'tcx>, + iter: Ty<'tcx>, + span: Span, +) -> Option { + let text = match *item.peel_refs().kind() { + ty::Char => false, + ty::Str => true, + ty::Adt(_, args) => match b.t.std_of(item)? { + Std::String => true, + Std::Box | Std::Cow => args.types().next().is_some_and(|t| t.is_str()), + _ => return None, + }, + _ => return None, + }; + let extend = if text { "stringExtend" } else { "stringExtendChars" }; + let drops = iter_drops(b, iter, span); + Some(Expr::rt(extend, tail(vec![s, items], vec![drops]))) +} + /// Whether an iterator of type `ty` that a runtime function takes by value drops what it /// holds, which its `rt.Iter` drops as std's does: `true`, or `undefined` for one that /// drops nothing. diff --git a/ts/src/lower/library.rs b/ts/src/lower/library.rs index 1a7df8874..ef686283d 100644 --- a/ts/src/lower/library.rs +++ b/ts/src/lower/library.rs @@ -1790,6 +1790,13 @@ fn mapping<'tcx>( ("String", "as_str") => args.pop()?, ("String", "new") => Expr::Str(String::new()), ("String", "with_capacity") => Expr::rt("stringWithCapacity", args), + // Of the place the `&mut` is, which it writes as it drops, and of a range, as `Vec::drain`. + ("String", "drain") => { + let range = args.pop()?; + args.push(super::collections::bounds(b, call, range)); + Expr::rt("stringDrain", args) + } + ("Drain", "as_str") => Expr::rt("stringDrainAsStr", args), ("ParseIntError", "kind") => args.pop()?.member("kind"), // `str::parse::`, by std's grammar rather than `Number`'s. ("str", "parse") | ("FromStr", "from_str") if super::float::is(targ(gargs, 0)) => { diff --git a/ts/src/lower/mir/call.rs b/ts/src/lower/mir/call.rs index 41da6506d..01627771d 100644 --- a/ts/src/lower/mir/call.rs +++ b/ts/src/lower/mir/call.rs @@ -555,6 +555,17 @@ impl<'m, 'b, 'a, 'tcx> Fx<'m, 'b, 'a, 'tcx> { self.set_borrowed(&place, pushed, span, out); done(Expr::undefined(), false) } + // The extended string, from the iterator read as std's `Extend` reads it. + ("Extend", "extend") if targ(0).is_some_and(|t| self.b.t.std_of(t) == Some(Std::String)) => { + let (item, source) = (targ(1)?, targ(2)?); + let place = self.borrowed_place(&args[0].node, span, out)?; + let items = self.operand(&args[1].node, span, out); + let iter = crate::lower::iter::iter_of(self.b.t, source)?; + let items = crate::lower::iter::into_iter(self.b, source, items, span)?; + let extended = crate::lower::iter::string_extend(self.b, place.0.clone(), items, item, iter, span)?; + self.set_borrowed(&place, extended, span, out); + done(Expr::undefined(), false) + } // No capacity is kept, and reading one is refused, but reserving past `isize::MAX` // panics as std's does. ("String", "reserve" | "reserve_exact") => { diff --git a/ts/src/lower/pointer.rs b/ts/src/lower/pointer.rs index 088887549..94177bd2f 100644 --- a/ts/src/lower/pointer.rs +++ b/ts/src/lower/pointer.rs @@ -247,12 +247,14 @@ pub fn keeps(tcx: ty::TyCtxt<'_>, def_id: rustc_hir::def_id::DefId) -> bool { /// Whether std's `def_id` is one a mapping takes the `rt.Place` a `&mut` is for, rather /// than what it points at: a `MaybeUninit`'s, which it points into, an `Arc`'s that -/// `make_mut` replaces, and a well-known crate's, such as a `Bytes` that `Buf::advance` -/// moves past its front. +/// `make_mut` replaces, a `String`'s that a drain edits as it drops, and a well-known +/// crate's, such as a `Bytes` that `Buf::advance` moves past its front. pub fn takes_place(tcx: ty::TyCtxt<'_>, def_id: rustc_hir::def_id::DefId) -> bool { matches!( super::library::key(tcx, def_id), - Some((owner, name)) if owner == "MaybeUninit" || matches!(owner.as_str(), "Arc" | "Rc") && name == "make_mut" + Some((owner, name)) if owner == "MaybeUninit" + || matches!(owner.as_str(), "Arc" | "Rc") && name == "make_mut" + || (owner.as_str(), name.as_str()) == ("String", "drain") ) || super::stock::is_stock(tcx, def_id) }