lib/fmt: port V-side fprintln/fprintfln/bsprintf/asprintf (#94 fold-e7)

V had vfprint / vfprintf but no compositions over them, so callers
needing the newline / printf-newline / bounded-buffer / heap-grow
shapes still routed through the OLD io.stream-shaped fprintln /
fprintfln / bsprintf / asprintf. Port the four compositions into
vstream.ww as the v* twins: vfprintln + vfprintfln chain a
"\n" vputbytes after the underlying primitive; vbsprintf threads a
caller buffer through memio.fixed_vstream and returns the prefix view;
vasprintf grows through memio.dynamic_vstream and shrink-copies to a
tight allocation before io.st_close.

Bundles the two memio enablers (fixed_string / dynamic_string in
lib/memio/vstream.ww) that vbsprintf / vasprintf depend on directly,
per drew-approved exception to one-class-one-commit
(feedback_refactor_routing_same_class_drops applies — helpers are
direct prereqs, not unrelated churn; the bus-routing site lives in
v* fmt code, not in memio). They mirror OLD memio.string (memio.ww:
102) over the per-flavour *fixed_ctx / *dynamic_ctx intrusive cast,
same shape as the read/write callback split at memio/vstream.ww:144.

Mirror sites:
  vfprintln    fmt.ww:240 fprintln          ref/hare/fmt/wrappers.ha:48
  vfprintfln   fmt.ww:740 fprintfln         ref/hare/fmt/wrappers.ha:69
  vbsprintf    fmt.ww:839 bsprintf          ref/hare/fmt/wrappers.ha:42
  vasprintf    fmt.ww:873 asprintf          ref/hare/fmt/wrappers.ha:29

Divergence vs Hare on vbsprintf: Hare returns `(const str | nomem)`;
ww collapses to `(str | io.error)` so the underlying vfprintf io.error
arm stays uniform. The fixed_vstream nomem widens into io.error
explicitly (no `memio.fixed_vstream(buf)?`) because #173 (TRY-on-
tagged-return both-stages broken) is still open — same shape memio/
vstream.ww adopted at line 87-99 for fixed_vstream itself. vasprintf
keeps OLD's bare `str` return (no nomem variant on public surface).

ken cs==ww mechanical: additive only, both stages compile identically.
fmt is NOT embedded in any selfhost main.combined.ww (grep verified
pre-impl: zero `^package fmt;` hits in selfhost/cmd/*/main.combined.
ww). memio.vstream.ww IS embedded in w6c + wwdump combined.ww (lib/
ww/cgen.ww uses memio.dynamic for buffer growth); the two memio
helpers regen-and-commit via ww build per #110 SSoT.

test/wcc/781_fmt_vstream_compositions_run.c (cstage-only per #209): 4
rows pin all four V wrappers — fdprintln_v_run_basic (newline shape),
fdprintfln_v_run_fmt ({n}-placeholder + newline), bsprintf_v_basic
(fixed buffer + returned view + caller bytes), asprintf_v_basic
(owned heap str + os.free roundtrip). Mirror of 777/780 cstage carve-
out (#209 wwstage formattable match-arm bail). Byte-id graduates with
#209 close. 214 total tests green (was 213).
This commit is contained in:
2026-05-29 12:28:12 +09:00
parent a3f3153943
commit df287846c5
6 changed files with 542 additions and 0 deletions

View File

@@ -240,3 +240,36 @@ fn dynamicgrow_v(d: *dynamic_ctx, need: i32) void = {
d.ptr = nbuf;
d.cap = newcap;
};
// fixed_string / dynamic_string — borrowed str view of buf[0..pos] for
// the matching ctx flavour. Project #94 fold-e7 bundles them here as
// direct enablers for fmt.vbsprintf / fmt.vasprintf (drew-approved per
// feedback_refactor_routing_same_class_drops — prereqs, not churn).
// Mirrors OLD memio.string (memio.ww:102) over `*state`; the V-side
// twins key off the intrusive *<flavour>_ctx cast same shape as the
// vtable callbacks (vstream.ww:151,184). Two flavours kept distinct
// (vs one fn over a shared header) so each cast targets the matching
// ctx type — same rationale as the split fixedread_v / dynread_v at
// line 144-148. Unchecked str view per CLAUDE.md rule 9 carve-out
// (utf8.validate at IO source is opt-in, not wrapped per-construction).
//
// Mirrors ref/hare/memio/ops.ha:51 string(s: io::handle) and the
// per-handle dispatch shape. fold-e8 (task #50 prereq) extends with
// buffer/reset/borrowedread accessors; this fold ships only the
// vbsprintf / vasprintf enablers.
export fn fixed_string(vs: io.vstream) str = {
let c: *fixed_ctx = vs: *fixed_ctx;
let r: str;
r.ptr = c.ptr;
r.len = c.pos;
return r;
};
export fn dynamic_string(vs: io.vstream) str = {
let c: *dynamic_ctx = vs: *dynamic_ctx;
let r: str;
r.ptr = c.ptr;
r.len = c.pos;
return r;
};