lib: complete the parallel vstream surface to Hare value-return shape (#94 fold-eFinal prep)

The #94 Option-C vstream surface was left incomplete and structurally
divergent from Hare: constructors heap-allocated and returned (X | nomem)
or used out-params instead of Hare's by-value stack ownership; memio lacked
reset/buffer/borrowedread; bufio's scanner was never ported to the vtable.
This is the additive half of the eFinal collapse — OLD surface stays fully
live; the destructive FLIP (delete OLD + drop _v + repoint) is the next
commit.

Reshape all constructors to VALUE-RETURN (field-by-field sret; the heap +
nomem was an unnecessary crutch — wide slice-bearing struct return-by-value
is byte-id-proven, cf 925_sret_struct_return_run). memio fixed/dynamic/
dynamicfrom, bufio init, log new now return the struct by value; the nomem
is gone with the alloc that forced it.

memio: unify the per-flavour ctx structs onto one `stream` (vt at offset 0);
collapse fixed_string + dynamic_string into a single string() over the common
header (bare-str return is the ratified rule-9 frombytes carve-out, cited at
the site per ref/hare/memio/stream.ha:81); port reset/buffer/borrowedread as
single fns over the header.

bufio: collapse the EXISTING scanner subset (newscannerbuf/scanbyte/scanbytes/
scanline/finish + setflush/flush/unread/isbuffered) onto the vtable, with src
now io.vstream so reads go through io.st_read. The Hare scanner functions ww
never implemented (scanrune/scanstring-arbitrary-delim/readtok/readline/
auto-grow newscanner) are out of scope and deferred to #217 — eFinal is a
collapse, not a feature expansion.

Keep the explicit (&fn): *io.T casts on vtable-slot stores (cgen-neutral;
avoids the #214 (X|void) over-acceptance surface; dropping the casts is a
deferred #206 payoff gated on #214).

Self-gate: 776 (memio) 18/18 and 778 (bufio) 27/27, every row carrying a
cs.s == ww.s byte-id check — bufio/fmt/log are not compiler-embedded, so
these rows are their only byte-id coverage. 779/781 stay STAGE_CS-only
pending #209. Regen w6c+wwdump combined.ww (io+memio are the embedded
modules).
This commit is contained in:
2026-05-29 21:14:39 +09:00
parent f9f83faca7
commit 10cb835f99
10 changed files with 1068 additions and 860 deletions

View File

@@ -1,105 +1,79 @@
// vstream — Hare-shaped vtable wrappers over [[bufio.stream]]. Project
// #94 fold-e4 (Option C, parallel API for lib/bufio).
// vstream — Hare-shaped vtable port of lib/bufio. Project #94
// fold-eFinal(PREP).
//
// Adds bufio_vstream + isbuffered_v alongside the pre-vtable
// bufio.init / bufio.isbuffered surface in bufio.ww. The OLD surface
// stays untouched here — fold-eFinal (task #50) atomically flips the
// package shape: deletes the OLD constructors + callbacks, renames
// `_v` suffix off (drops the `isbuffered_v` to `isbuffered` per Hare's
// ref/hare/bufio/stream.ha:179), and migrates the few callers.
// Collapses the OLD bufio surface (bufio.ww's pre-vtable `stream` +
// `scanner` over a legacy `*io.stream` src) onto the io.vtable surface,
// as parallel `_v`-suffixed additions. The OLD surface stays untouched
// here — fold-eFinal (FLIP) deletes it, renames the `_v` suffix off
// (bufio_ctx→stream, bufio_vstream→init, scanner_v→scanner, scanbytes_v
// →scanbytes, isbuffered_v→isbuffered, …) per Hare's
// ref/hare/bufio/{stream,scanner}.ha, and migrates the few callers.
//
// Hare's ref/hare/bufio/stream.ha:69 (init) returns the stream by
// value, with `vtable: nullable *io::vtable` pre-resolved per-mode
// (vtable_r / vtable_w / vtable_rw module-static singletons at
// stream.ha:8-25); ww doesn't ship static-storage tagged-union
// singletons yet, so we heap-allocate bufio_ctx, fill the vtable
// post-alloc per slot, and return &c.vt as the io.vstream. Same
// intrusive shape memio/vstream.ww + fmt/vstream.ww use.
// SCOPE (#94 fold-eFinal): this is a COLLAPSE of the surface ww ALREADY
// ships, NOT a port of Hare's full scanner. The features OLD bufio
// never implemented — auto-grow newscanner, multibyte-delim scanbytes /
// scanstring, scanrune / readbyte / readtok / readline / readrune /
// unreadrune — are a separate future feature fold (#217) and are OUT of
// eFinal. The ported set mirrors OLD exactly: the fixed-buffer
// newscanner_buf, single-byte scanbytes (OLD scantok), single-byte
// scanline, scanbyte, finish; and the buffered-stream init / setflush /
// flush / unread / isbuffered.
//
// drew-approved deferrals (Hare-shape, not collapsed here):
// VALUE-RETURN (drew, gating): Hare's bufio constructors return BY
// VALUE (ref/hare/bufio/stream.ha:69 init, scanner.ha:92
// newscanner_buf). The OLD out-param shape existed only because
// wide-struct sret was unimplemented — it now round-trips
// (test/wcc/925 + 776/778 byte-id), so each constructor builds in a
// local `let r: T;`, field-assigns every slot, and `return r;`. The
// caller owns the returned struct (stack ownership, no-GC). For the
// buffered stream the caller passes `&b.vt` to the io.st_* dispatchers;
// the scanner is a plain read-ahead tokenizer (no embedded vtable —
// matching OLD), driven directly via scanbyte_v / scanbytes_v / …
//
// - Hare uses three vtable singletons (vtable_r / vtable_w /
// vtable_rw) to discriminate r-only / w-only / r+w mode;
// bufio_vstream always installs all three callbacks per
// bufio.ww:32-38's pre-existing divergence (zero-length rbuf or
// wbuf degenerates the matching callback in-cb). The Hare-shape
// mode-keyed singletons graduate with io fold-2 (handle).
// VTABLE-NATIVE SRC (the eFinal point): the buffered stream's `src` and
// the scanner's `src` are now `io.vstream`, and every underlying read /
// write / close goes through io.st_read / io.st_write / io.st_close
// (the vtable dispatchers) — NOT the legacy io.read / io.write /
// io.close over `*io.stream`. The io.st_* surface returns io.error
// directly, so the OLD `io.closed → nomem-widen → io.error` dance
// (per the #173 deferral) is GONE: the error simply forwards.
//
// - Hare's defer-handle (`flag::MANAGED_*` ownership bits) deferred
// to io fold-2 (drew). Caller owns rbuf/wbuf/src for now;
// bclose_v flushes and forwards close to src but doesn't free
// any buffers (mirrors bufio.ww:60-64 OLD-side ownership note).
// Cast workaround per #206-payoff (ken: KEEP the explicit casts; they
// are cgen-neutral and sidestep the #214 over-acceptance surface).
// `(&fn_name): *io.<role>` at each vtable store + the isbuffered_v
// fn-ptr-equality comparand — same workaround memio/vstream.ww +
// test/wcc/775 use.
//
// Cast workaround per #206: bare `&fn_name` does not type-check as
// a `(*<alias> | void)` field-init / let-binding. Explicit
// `(&fn_name): *io.<role>` cast at each store site is the Hare-
// faithful minimum-touch route — same workaround memio/vstream.ww
// and fmt/vstream.ww use. 3 cast tokens at the vtable wire-up site,
// plus 2 in isbuffered_v for the fn-ptr-equality comparand. All 5
// drop out wholesale once #206 closes.
// #207 (struct-lit multi-tagged-field copy drops past first) and #210
// (slice-field struct-lit drop under alloc): not reachable from the
// value-return form — every struct is built field-by-field in a local
// then sret-returned, no `alloc(T{...})` struct-lit. Both filings stay
// open for the struct-lit path; this fold doesn't take it.
//
// Slice-field workaround per #210 (sibling of #207, surfaced here):
// struct-lit slice-typed field through `alloc(bufio_ctx{rbuf = rbuf,
// ...})?` SILENTLY drops the slice (reads back as zero-length).
// Scalar i32 and pointer fields in the same struct-lit populate
// correctly. Workaround: post-alloc `c.rbuf = rbuf;` field-assign,
// which works through the *bufio_ctx pointer. Same alloc-zero-chain
// shape as memio/vstream.ww uses for #207 over vt's tagged fields.
// 3 slice-field stores (rbuf / wbuf / flush) move post-alloc.
// Drops out wholesale on #210 close.
// #209 (wwstage formattable match-arm bail when fmt imported): bufio
// does NOT import fmt (only io), so test/wcc/778 runs both stages +
// cs.s == ww.s byte-id on every row — bufio's only byte-id coverage
// (it is not compiler-embedded).
//
// fn-ptr-equality discriminator per ref/hare/bufio/stream.ha:179-184:
// isbuffered_v pattern-extracts the reader/writer slot and compares
// against `(&bread_v): *io.reader` / `(&bwrite_v): *io.writer`. Hare
// uses static-singleton symbol-address equality; ww routes through
// the cast-then-eq form because each slot is a tagged union
// (`(*reader | void)`) rather than a plain pointer. eFinal collapses
// to the Hare-direct form (`s.reader == &read`) once #192 closes and
// the slot becomes a plain nullable.
//
// io.closed → io.error conversion at the OLD-API boundary: the
// underlying *io.stream's io.read/io.write return (.. | io.closed)
// but the V API surface returns (.. | io.error). We construct nomem
// and widen to io.error per the fdsinkwrite_v precedent
// (fmt/vstream.ww:88-92) — same shape memio/vstream.ww uses for its
// os.write < 0 path. nomem is the carrier (not a semantic match for
// "closed") until io fold-2 lands a properly-shaped error variant.
//
// Sibling tasks parked here (filed, NOT fixed):
//
// - io fold-2 (handle port): drew-deferred. Hare's
// ref/hare/bufio/stream.ha:69 takes `src: io::handle`; once
// handle = (file | int) lands, bufio_vstream's src parameter
// collapses accordingly. eFinal (#50) graduates.
//
// - #206 (bare &fn → (*alias|void)): 5 cast sites here; drop
// out wholesale on close.
//
// - #207 (struct-lit multi-tagged-field copy drops past first):
// alloc-zero-chain pattern (`vt = zero` + post-alloc
// `c.vt.X = ...`) sidesteps; same shape memio/vstream.ww uses.
// Only one tagged field in bufio_ctx (vt), but the local-zero
// route stays consistent with memio's idiom.
//
// - #210 (struct-lit slice-typed field drops under alloc):
// surfaced by this fold. rbuf/wbuf/flush move post-alloc;
// drops out wholesale on close.
//
// - #209 (wwstage formattable match-arm bail when fmt imported):
// bufio doesn't import fmt transitively (only io), so the
// test/wcc/778 probe runs both stages.
// Mode discrimination (drew-deferred): Hare uses three vtable
// singletons (vtable_r / vtable_w / vtable_rw); bufio_vstream installs
// all three callbacks per bufio.ww:32-38's divergence (zero-length
// rbuf/wbuf degenerates the matching callback in-cb). Defer-handle
// (MANAGED_* ownership bits) also deferred — caller owns rbuf/wbuf/src;
// bclose_v flushes + forwards close but frees nothing. Both graduate
// with io fold-2 (#5).
package bufio;
import io;
// bufio_ctx — heap-alloc'd state for bufio_vstream. `vt` at offset 0
// for the intrusive vstream→*bufio_ctx cast. Mirrors lib/bufio.stream
// (bufio.ww:252) modulo the new io.vtable in place of the OLD
// io.stream first-field embed.
// bufio_ctx — heap-free state for bufio_vstream. `vt` at offset 0 for
// the intrusive vstream→*bufio_ctx cast. `src` is an io.vstream (the
// vtable-native underlying handle), not the OLD `*io.stream`. Mirrors
// lib/bufio.stream (bufio.ww:252) modulo io.vtable + io.vstream src.
export type bufio_ctx = struct {
vt: io.vtable,
src: *io.stream,
src: io.vstream,
rbuf: []u8,
rstart: i32,
rend: i32,
@@ -108,42 +82,89 @@ export type bufio_ctx = struct {
flush: []u8,
};
// bufio_vstream — wire a vstream over an underlying *io.stream with
// caller-supplied read/write buffers. Both rbuf and wbuf may be
// empty; bread_v / bwrite_v degenerate per bufio.ww:34-38 in that
// case. The flush byte-set defaults to "\n" (line-buffered writes);
// per-stream setflush graduates with #50 / io fold-2.
// bufio_vstream — wire a buffered stream over an underlying io.vstream
// with caller-supplied read/write buffers. Both rbuf and wbuf may be
// empty; bread_v / bwrite_v degenerate per bufio.ww:34-38. The flush
// byte-set defaults to "\n" (line-buffered writes); setflush_v swaps it.
//
// Returns the ctx BY VALUE (sret — field-by-field build then
// `return r;`); the caller passes `&b.vt` to the io.st_* dispatchers.
//
// Mirrors ref/hare/bufio/stream.ha:69 (init).
export fn bufio_vstream(src: *io.stream, rbuf: []u8, wbuf: []u8) (io.vstream | nomem) = {
let zero: io.vtable;
let c: *bufio_ctx = alloc(bufio_ctx{
vt = zero,
src = src,
rstart = rbuf.len,
rend = rbuf.len,
wend = 0,
})?;
// Post-alloc slice-field stores per #210 (sibling of #207):
// struct-lit `rbuf = rbuf` silently drops slice-typed fields
// under alloc(T{...}). Scalar/ptr fields in the same struct-lit
// store correctly. Same alloc-zero-chain shape memio/vstream.ww
// uses for #207 over vt's tagged fields. 3 stores; drops out on
// #210 close.
c.rbuf = rbuf;
c.wbuf = wbuf;
c.flush = flushdefault[0:1];
c.vt.reader = (&bread_v): *io.reader;
c.vt.writer = (&bwrite_v): *io.writer;
c.vt.closer = (&bclose_v): *io.closer;
return &c.vt;
export fn bufio_vstream(src: io.vstream, rbuf: []u8, wbuf: []u8) bufio_ctx = {
let r: bufio_ctx;
r.vt.reader = (&bread_v): *io.reader;
r.vt.writer = (&bwrite_v): *io.writer;
r.vt.closer = (&bclose_v): *io.closer;
r.src = src;
r.rbuf = rbuf;
r.rstart = rbuf.len;
r.rend = rbuf.len;
r.wbuf = wbuf;
r.wend = 0;
r.flush = flushdefault[0:1];
return r;
};
// setflush_v — install a new flush byte-set. Any byte from `bs`
// appearing in a write payload triggers an automatic flush after the
// write copies into wbuf. Mirrors OLD setflush (bufio.ww:287) +
// ref/hare/bufio/stream.ha:128.
export fn setflush_v(b: *bufio_ctx, bs: []u8) void = {
b.flush = bs;
};
// flush_v — drain any pending wbuf data to src. Public API AND the
// internal drain called by bwrite_v / bclose_v — OLD bufio.flush is
// itself dual-purpose (bufio.ww:294, called from bwrite/bclose), so a
// single fn serves both; no separate private callback to rename. The
// io.closed → nomem-widen of the OLD V-side is gone: io.st_write
// returns io.error directly. A 0-byte non-error write means the sink
// made no progress (memio.fixed full) — surfaced as a nomem-carried
// io.error rather than spinning, matching what io.writeall would do in
// Hare. Mirrors OLD flush (bufio.ww:294) + ref/hare/bufio/stream.ha.
export fn flush_v(b: *bufio_ctx) (void | io.error) = {
if (b.wend == 0) { return; };
let off: i32 = 0;
for (off < b.wend) {
let r: (size | io.error) = io.st_write(b.src, b.wbuf[off:b.wend]);
match (r) {
case let n: size => {
if (n == 0: size) {
let nm: nomem;
let e: io.error = nm;
return e;
};
off += n: i32;
};
case let e: io.error => return e;
};
};
b.wend = 0;
return;
};
// unread_v — push `buf` back into the read buffer so the next bread_v
// returns it first. The bytes must fit in front of the pending region
// (rstart >= buf.len); Hare aborts on overflow, ww does the same via
// rtabort (declared in bufio.ww, same package). Mirrors OLD unread
// (bufio.ww:321) + ref/hare/bufio/stream.ha:164.
export fn unread_v(b: *bufio_ctx, buf: []u8) void = {
if (b.rstart < buf.len) {
rtabort("bufio.unread_v: more data than rbuf has room for");
};
let i: i32 = 0;
for (i < buf.len) {
b.rbuf[b.rstart - buf.len + i] = buf[i];
i += 1;
};
b.rstart -= buf.len;
};
// isbuffered_v — true when `s` was returned by [[bufio_vstream]].
// Hare uses callback-identity (ref/hare/bufio/stream.ha:179);
// matches the OLD isbuffered shape (bufio.ww:338) but over the new
// `_v` fn symbols. Either reader or writer matching is sufficient
// (closer alone isn't unique to bufio).
// Hare uses callback-identity (ref/hare/bufio/stream.ha:179); matches
// the OLD isbuffered shape (bufio.ww:338) over the new `_v` symbols.
// Either reader or writer matching is sufficient.
export fn isbuffered_v(s: io.vstream) bool = {
match (s.reader) {
case let r: *io.reader => {
@@ -160,14 +181,12 @@ export fn isbuffered_v(s: io.vstream) bool = {
return false;
};
// ---- vtable callbacks ----------------------------------------------------
// ---- buffered-stream vtable callbacks ------------------------------------
// bread_v — vstream-side read. Recover ctx via the intrusive
// vstream→*bufio_ctx cast. Mirrors bufio.ww:346 (OLD bread) modulo
// the (size | io.eof | io.error) return surface; io.closed from the
// underlying *io.stream's io.read widens to io.error via nomem
// (same shape fmt/vstream.ww:88-92 + memio/vstream.ww:71-74 use,
// per #173 deferral).
// bread_v — vstream-side buffered read. Recover ctx via the intrusive
// cast; refill rbuf from src via io.st_read on empty pending region.
// Mirrors bufio.ww:346 (OLD bread) modulo the io.vstream src +
// io.st_read dispatch (io.error forwards directly — no nomem-widen).
fn bread_v(s: io.vstream, buf: []u8) (size | io.eof | io.error) = {
let b: *bufio_ctx = s: *bufio_ctx;
if (b.rbuf.len == 0) {
@@ -176,15 +195,11 @@ fn bread_v(s: io.vstream, buf: []u8) (size | io.eof | io.error) = {
if (b.rstart >= b.rend) {
b.rstart = 0;
b.rend = 0;
let r: (i32 | io.eof | io.closed) = io.read(b.src, b.rbuf);
let r: (size | io.eof | io.error) = io.st_read(b.src, b.rbuf);
match (r) {
case let n: i32 => { b.rend = n; };
case let n: size => { b.rend = n: i32; };
case io.eof => { let e: io.eof; return e; };
case io.closed => {
let nm: nomem;
let e: io.error = nm;
return e;
};
case let e: io.error => return e;
};
};
let avail: i32 = b.rend - b.rstart;
@@ -199,24 +214,15 @@ fn bread_v(s: io.vstream, buf: []u8) (size | io.eof | io.error) = {
return n: size;
};
// bwrite_v — vstream-side write. Mirrors bufio.ww:376 (OLD bwrite)
// modulo the (size | io.error) return surface; io.closed widens to
// io.error per the bread_v note. Default-flush scan + per-batch copy
// + post-write conditional flush — same flush-byte-set semantics as
// the OLD path (ref/hare/bufio/stream.ha:236-246, labeled-break
// inlined per bufio.ww:382's note).
// bwrite_v — vstream-side buffered write. wbuf-empty passes through to
// src; otherwise default-flush scan + per-batch copy + post-write
// conditional flush. Mirrors bufio.ww:376 (OLD bwrite) modulo the
// io.vstream src + io.st_write dispatch. Labeled-break inlined per
// bufio.ww:382 (ww has no labeled break).
fn bwrite_v(s: io.vstream, buf: []u8) (size | io.error) = {
let b: *bufio_ctx = s: *bufio_ctx;
if (b.wbuf.len == 0) {
let r: (i32 | io.closed) = io.write(b.src, buf);
match (r) {
case let n: i32 => { return n: size; };
case io.closed => {
let nm: nomem;
let e: io.error = nm;
return e;
};
};
return io.st_write(b.src, buf);
};
let doflush: bool = false;
if (b.flush.len != 0) {
@@ -265,12 +271,9 @@ fn bwrite_v(s: io.vstream, buf: []u8) (size | io.error) = {
return buf.len: size;
};
// bclose_v — flush pending wbuf, forward close to src. Mirrors
// bufio.ww:431. Hare's close_buffered also handles the
// MANAGED_HANDLE / MANAGED_RDBUF / MANAGED_WRBUF ownership bits
// (ref/hare/bufio/stream.ha:188-202); ww drops them per bufio.ww:33-47
// (caller-owned today; defer-handle graduates with io fold-2,
// drew-deferred).
// bclose_v — flush pending wbuf, forward close to src via io.st_close.
// Mirrors bufio.ww:431. Caller-owned buffers/src are not freed (drew
// defer-handle deferral, bufio.ww:33-47).
fn bclose_v(s: io.vstream) (void | io.error) = {
let b: *bufio_ctx = s: *bufio_ctx;
let fr: (void | io.error) = flush_v(b);
@@ -278,43 +281,149 @@ fn bclose_v(s: io.vstream) (void | io.error) = {
case void => { };
case let e: io.error => return e;
};
let cr: (void | io.closed) = io.close(b.src);
let cr: (void | io.error) = io.st_close(b.src);
match (cr) {
case void => return void;
case io.closed => {
let nm: nomem;
let e: io.error = nm;
return e;
};
case let e: io.error => return e;
};
};
// flush_v — drain pending wbuf to src. Mirror of bufio.ww:294 (OLD
// flush) over the V surface; io.closed widens to io.error per the
// bread_v note. Module-prefixed `_v` suffix keeps the cstage flat-TU
// private-fn scope from colliding with OLD flush (same shape
// memio.dynamicgrow_v at memio/vstream.ww:229).
fn flush_v(b: *bufio_ctx) (void | io.error) = {
if (b.wend == 0) { return; };
let off: i32 = 0;
for (off < b.wend) {
let r: (i32 | io.closed) = io.write(b.src, b.wbuf[off:b.wend]);
// ---- scanner (read-ahead tokenizer over an io.vstream src) ---------------
//
// Plain tokenizer — no embedded vtable (matching OLD bufio.scanner,
// bufio.ww:104); the scanner is driven directly via scanbyte_v /
// scanbytes_v / scanline_v, not through io.st_*. Its `src` is an
// io.vstream and refills go through io.st_read. Value-return
// constructor (Hare newscanner_buf, scanner.ha:92).
//
// ptr/cap kept flat (no `buf: []u8`) per the OLD scanner shape
// (bufio.ww:104-110); the scanner predates struct-held slice support.
export type scanner_v = struct {
src: io.vstream,
ptr: *u8,
cap: i32,
start: i32, // index where the pending region starts in ptr
avail: i32, // pending byte count; pending = ptr[start..start+avail]
};
// newscannerbuf_v — wire a scanner to read through `src` using `buf` as
// the fixed read-ahead window. Returns the scanner BY VALUE. This is
// Hare's newscanner_buf (ref/hare/bufio/scanner.ha:92); the auto-grow
// newscanner is a separate feature (#217, OUT of eFinal). Mirrors OLD
// newscanner (bufio.ww:116), which is itself the fixed-buffer form.
export fn newscannerbuf_v(src: io.vstream, buf: []u8) scanner_v = {
let r: scanner_v;
r.src = src;
r.ptr = buf.ptr;
r.cap = buf.len;
r.start = 0;
r.avail = 0;
return r;
};
// finish_v — release scanner-owned resources. No-op (buffer is
// caller-owned, src isn't closed); kept on the surface so callers won't
// churn. Mirrors OLD finish (bufio.ww:127) + scanner.ha:110.
export fn finish_v(s: *scanner_v) void = { };
// readahead_v — make room and read once from src into the back of the
// pending region. Returns bytes newly buffered (>=0), or io.eof/io.error
// from src. Mirrors OLD readahead (bufio.ww:133) modulo io.st_read +
// io.error (no nomem-widen). Returns i32 (OLD shape); the size from
// io.st_read narrows to i32 (buffer-length type).
fn readahead_v(s: *scanner_v) (i32 | io.eof | io.error) = {
if (s.start + s.avail == s.cap && s.start > 0) {
let i: i32 = 0;
for (i < s.avail) {
s.ptr[i] = s.ptr[s.start + i];
i += 1;
};
s.start = 0;
};
let off: i32 = s.start + s.avail;
let v: []u8;
v.ptr = s.ptr + (off: u64);
v.len = s.cap - off;
let r: (size | io.eof | io.error) = io.st_read(s.src, v);
match (r) {
case let n: size => {
s.avail += n: i32;
return n: i32;
};
case io.eof => { let e: io.eof; return e; };
case let e: io.error => return e;
};
};
// scanbyte_v — pop one byte, refilling from src on demand. Mirrors OLD
// scanbyte (bufio.ww:160) + ref/hare/bufio/scanner.ha:204.
export fn scanbyte_v(s: *scanner_v) (u8 | io.eof | io.error) = {
for (s.avail == 0) {
let r: (i32 | io.eof | io.error) = readahead_v(s);
match (r) {
case let n: i32 => {
if (n == 0) {
let nm: nomem;
let e: io.error = nm;
return e;
};
off += n;
};
case io.closed => {
let nm: nomem;
let e: io.error = nm;
return e;
};
case let n: i32 => { };
case io.eof => { let e: io.eof; return e; };
case let e: io.error => return e;
};
};
b.wend = 0;
return;
let b: u8 = s.ptr[s.start];
s.start += 1;
s.avail -= 1;
return b;
};
// scanbytes_v — read up to (and not including) the next byte equal to
// `delim`. The delim is consumed but not returned. The returned slice
// borrows from the scanner buffer and is invalidated by the next scan.
// EOF without delim discards the trailing fragment and returns io.eof
// (Hare EOF_DISCARD default); buffer-full without delim returns
// overflow. Single-byte delim only — Hare's `(u8 | []u8)` multibyte
// form is #217 (OUT). drew renames OLD scantok → scanbytes. Mirrors OLD
// scantok (bufio.ww:186) + ref/hare/bufio/scanner.ha:220 (narrowed).
export fn scanbytes_v(s: *scanner_v, delim: u8) ([]u8 | io.eof | io.error | overflow) = {
let i: i32 = 0;
for (true) {
for (i < s.avail) {
if (s.ptr[s.start + i] == delim) {
let v: []u8;
v.ptr = s.ptr + (s.start: u64);
v.len = i;
s.start += i + 1;
s.avail -= i + 1;
return v;
};
i += 1;
};
if (s.start + s.avail == s.cap && s.start == 0) {
let e: overflow; return e;
};
let r: (i32 | io.eof | io.error) = readahead_v(s);
match (r) {
case let n: i32 => { };
case io.eof => { let e: io.eof; return e; };
case let e: io.error => return e;
};
};
let e: io.eof; return e;
};
// scanline_v — read up to (and not including) the next '\n'. The newline
// is consumed; the returned str view borrows from the scanner buffer.
// Single-byte route (Hare's scan_line = scan_string(s, "\n"); the
// arbitrary multibyte-delim scan_string is #217, OUT). Mirrors OLD
// scanline (bufio.ww:225) + ref/hare/bufio/scanner.ha:307.
export fn scanline_v(s: *scanner_v) (str | io.eof | io.error | overflow) = {
let r: ([]u8 | io.eof | io.error | overflow) = scanbytes_v(s, 10u8);
match (r) {
case let bs: []u8 => {
let v: str;
v.ptr = bs.ptr;
v.len = bs.len;
return v;
};
case io.eof => { let e: io.eof; return e; };
case let e: io.error => return e;
case overflow => { let e: overflow; return e; };
};
};