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

@@ -65,8 +65,10 @@
package fmt;
import io;
import memio;
import os;
import strconv;
import strings;
// fd_ctx — vt at offset 0 for the intrusive vstream→*fd_ctx cast.
// Mirrors lib/memio.fixed_ctx (memio/vstream.ww:48); single tagged
@@ -556,3 +558,111 @@ export fn fdprintfln_v(fd: i32, fmt: str, args: field...) (size | io.error) = {
};
return total;
};
// ---- V-side compositions over the vfprint / vfprintf primitives.
// Project #94 fold-e7. drew-approved bundle (memio.fixed_string /
// memio.dynamic_string land alongside as direct enablers, not churn —
// feedback_refactor_routing_same_class_drops); ken cs==ww mechanical
// (additive only). eFinal (#50) collapses both surfaces and renames
// `v` suffix off.
//
// 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
// vfprintln — vfprint + trailing newline. Mirror fmt.fprintln
// (fmt.ww:240); vputbytes + (size | io.error) routing.
export fn vfprintln(vs: io.vstream, args: formattable...) (size | io.error) = {
let total: size = 0;
match (vfprint(vs, args...)) {
case let n: size => { total = n; };
case let e: io.error => return e;
};
match (vputbytes(vs, "\n".ptr, 1)) {
case let n: size => { total += n; };
case let e: io.error => return e;
};
return total;
};
// vfprintfln — vfprintf + trailing newline. Mirror fmt.fprintfln
// (fmt.ww:740).
export fn vfprintfln(vs: io.vstream, fmt: str, args: field...) (size | io.error) = {
let total: size = 0;
match (vfprintf(vs, fmt, args...)) {
case let n: size => { total = n; };
case let e: io.error => return e;
};
match (vputbytes(vs, "\n".ptr, 1)) {
case let n: size => { total += n; };
case let e: io.error => return e;
};
return total;
};
// vbsprintf — render into `buf` through memio.fixed_vstream; return
// the str view of bytes actually written. Mirror fmt.bsprintf
// (fmt.ww:839); nomem from fixed_vstream widens into io.error (Hare
// wrappers.ha:42 returns `(const str | nomem)` directly — ww collapses
// to (str | io.error) so the vfprintf path's io.error arm stays
// uniform). Short writes surface as a prefix (memio.fixed contract,
// vstream.ww:71 note).
export fn vbsprintf(buf: []u8, fmt: str, args: field...) (str | io.error) = {
let r: (io.vstream | nomem) = memio.fixed_vstream(buf);
let vs: io.vstream = nil: *io.vtable;
match (r) {
case let v: io.vstream => { vs = v; };
case let nm: nomem => { let e: io.error = nm; return e; };
};
match (vfprintf(vs, fmt, args...)) {
case let n: size => { return memio.fixed_string(vs); };
case let e: io.error => return e;
};
};
// vasprintf — render `fmt`/`args` into a heap-allocated str through
// memio.dynamic_vstream, shrink-copy to a tight allocation, close the
// dynamic backing. Mirror fmt.asprintf (fmt.ww:873) modulo: bare `str`
// return (Hare returns `(str | nomem)`; ww has no `nomem` variant on
// the public surface — os.alloc faults on OOM per lib/os.ww). Caller
// frees with `os.free(r.ptr, r.len: u64)` when r.len > 0; r.len == 0
// is a no-op free (same shape strings.dup uses at strings.ww:70).
// Shrink-to-fit rationale: memio.dynamic_vstream's cap doubles past
// pos during growth (memio/vstream.ww:230); io.st_close frees the
// cap-sized mapping. Returning memio.dynamic_string directly would
// leak the cap-vs-len slack (skip close) or dangle the view (close
// first); the copy lets the caller free with r.len.
export fn vasprintf(fmt: str, args: field...) str = {
let out: str;
out.ptr = nil;
out.len = 0;
let r: (io.vstream | nomem) = memio.dynamic_vstream();
let vs: io.vstream = nil: *io.vtable;
match (r) {
case let v: io.vstream => { vs = v; };
case nomem => { return out; };
};
let wres: (size | io.error) = vfprintf(vs, fmt, args...);
match (wres) {
case let n: size => {};
case let e: io.error => {};
};
let view: str = memio.dynamic_string(vs);
if (view.len == 0) {
let cres: (void | io.error) = io.st_close(vs);
match (cres) { case void => {}; case let e: io.error => {}; };
return out;
};
let tight: []u8 = alloc([], view.len: u64)!;
let i: i32 = 0;
for (i < view.len) {
tight[i] = view.ptr[i];
i += 1;
};
let cres: (void | io.error) = io.st_close(vs);
match (cres) { case void => {}; case let e: io.error => {}; };
tight.len = view.len;
return strings.frombytes(tight);
};

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;
};