From e3f49234f9c9e922c184542304187a0b2364cd83 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Tue, 2 Jun 2026 00:33:49 +0900 Subject: [PATCH] lib/encoding/hex: align to Hare io-streaming surface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The old buffer surface (encodedsize/decodedsize + encode(dst,src) i32 + decode(dst,src) (i32|invalid)) does not exist in Hare — it predates the #94 io vtable and mis-cited hex.ha:175 while implementing a different signature. Replace it with Hare's real surface (ref/hare/encoding/hex/hex.ha): - newencoder(out: io.handle) (:28) — write-only encoder stream. - encode(out: io.handle, in) (size | io.error) (:91). - encodestr(in) str (:68). - decodestr(s) ([]u8 | errors.invalid) (:175). Divergences (documented at-site): - The streaming DECODER (newdecoder/decode_reader, :120,:129) is DEFERRED to #247, blocked on #199b: Hare's decode_reader returns errors::invalid, which fits Hare's io::error (spreads ...errors::error). ww's io.error (lib/io/types.ww:55-62) does not carry errors.invalid, and io.read's (size|eof|error) can't propagate it, so a hex decoder *stream* can't faithfully report invalid hex through io.read yet. decodestr ships as a direct transform meanwhile. - nomem dropped from encodestr/decodestr returns (ww memio.dynamic has no failure path — same memio.string rule-9 carve-out, memio.ww:208). - The local hex.invalid type is deleted in favor of errors.invalid (that was the original divergence). - encode uses a single io.write rather than Hare's io::writeall (ww has none — fmt.fprint:498-501: callers drive write-all over raw io.write; encode_writer is whole-slice so a single write is equivalent). - dump (:212) deferred: ww has no default-arg support and fmt's formattable lacks u64 (#209), so the address column can't be ported faithfully yet. hex is now import-bearing, so it moves off the 900_stdlib standalone- compile list (like fmt/os/strings/bufio/bytes/errors before it); coverage stays at 979_hex_run.c. The stale "mirrors lib/encoding/hex.encode" comments in lib/encoding/utf8/utf8.ww are updated, which regenerates the 6 selfhost combined.ww (5 cmd + test/smoke) (comment-only, byte-id-neutral). --- lib/encoding/hex/hex.ww | 191 +++++++++++------- lib/encoding/hex/hextest.ww | 277 +++++++++++---------------- lib/encoding/utf8/utf8.ww | 7 +- selfhost/cmd/w6a/main.combined.ww | 7 +- selfhost/cmd/w6c/main.combined.ww | 7 +- selfhost/cmd/w6l/main.combined.ww | 7 +- selfhost/cmd/ww/main.combined.ww | 7 +- selfhost/cmd/wwdump/main.combined.ww | 7 +- selfhost/test/smoke.combined.ww | 7 +- test/wcc/900_stdlib.c | 13 +- 10 files changed, 263 insertions(+), 267 deletions(-) diff --git a/lib/encoding/hex/hex.ww b/lib/encoding/hex/hex.ww index e0452a32..4902880f 100644 --- a/lib/encoding/hex/hex.ww +++ b/lib/encoding/hex/hex.ww @@ -1,80 +1,141 @@ -// encoding/hex — RFC 4648 base16 (hexadecimal) encode/decode, -// buffer-based. +// encoding/hex — RFC 4648 base16 (hexadecimal) over the io-streaming +// surface. Port of ref/hare/encoding/hex/hex.ha. // -// Mirrors Hare's encoding::hex surface, modulo Hare's stream-based -// encoder/decoder and memio-backed allocator helpers. ww ships the -// in-memory subset only — same shape as lib/encoding/base32: caller -// provides dst, fn returns the byte count or invalid. +// The streaming DECODER (newdecoder/decode_reader, ref hex.ha:120,129) +// is DEFERRED — project #247, blocked on #199b. Hare's decode_reader +// returns errors::invalid on bad hex, which fits Hare's io::error (it +// spreads ...errors::error). ww's io.error (lib/io/types.ww:55-62) does +// NOT carry errors.invalid (the #199b deferral) and io.read's +// (size | eof | error) can't propagate it either, so a hex decoder +// *stream* cannot faithfully report invalid through io.read until #199b +// lands. decodestr ships as a direct transform meanwhile (its own +// return union carries errors.invalid; it is not io.error-constrained). // -// Output is always lowercase per ref/hare/encoding/hex/README:13; -// decode accepts both upper- and lower-case per the same line. - -// invalid — input wasn't a valid hex sequence. Mirrors the -// `errors::invalid` (!void) that ref/hare/encoding/hex/hex.ha:175 -// returns from decodestr. lib/encoding/base32's local !i32 spelling -// is a pre-existing divergence; this module follows Hare. +// Output is always lowercase per ref/hare/encoding/hex/README:13; the +// decoder accepts both upper- and lower-case per the same line +// (strconv.stou8 is case-insensitive, ref/hare/strconv/stou.ha:8-15). package hex; -export type invalid = !void; +import errors; +import fmt; +import io; +import memio; +import strconv; +import strings; -// encodedsize — bytes required to encode `n` source bytes. -// ref/hare/encoding/hex/hex.ha:91 writes 2 chars per input byte -// unconditionally. -export fn encodedsize(n: i32) i32 = { return n * 2; }; - -// decodedsize — bytes produced when decoding `n` encoded bytes. -// `n` must be even or decode returns invalid. -export fn decodedsize(n: i32) i32 = { return n / 2; }; - -// 4-bit value to lowercase hex char. '0'..'9' is +48; 'a'..'f' is -// +87 (= 'a' - 10). -fn nibble(v: u8) u8 = { - if (v < 10u8) { return v + 48u8; }; - return v + 87u8; +// encoder — a write-only io stream that lowercase-hex-encodes writes +// before forwarding them to `out`. ref/hare/encoding/hex/hex.ha:14. +// `vt` at offset 0 for the intrusive stream→io.stream cast (&e.vt) and +// the writer's reverse `s: *encoder` cast — same shape as memio.stream. +// Hare keeps a shared const encoder_vtable; ww embeds the vtable INLINE +// and wires the writer slot post-construction (memio convention). +export type encoder = struct { + vt: io.vtable, + out: io.handle, + err: (void | io.error), }; -// Hex char to 4-bit value, accepting upper or lower case. Returns -// 255 on invalid char (sentinel; '=' isn't legal in hex). -fn denibble(c: u8) u8 = { - if (c >= 48u8) { if (c <= 57u8) { return c - 48u8; }; }; // '0'..'9' - if (c >= 65u8) { if (c <= 70u8) { return c - 55u8; }; }; // 'A'..'F' - if (c >= 97u8) { if (c <= 102u8) { return c - 87u8; }; }; // 'a'..'f' - return 255u8; +// newencoder — wire an encoder over `out`. ref/hare/encoding/hex/hex.ha:28. +// Returns BY VALUE (memio constructor convention, proven sret round-trip); +// the caller passes &enc.vt to the io dispatchers. +export fn newencoder(out: io.handle) encoder = { + let r: encoder; + r.vt.writer = (&encode_writer): *io.writer; + r.out = out; + r.err = void; + return r; }; -// encode — encode `src` into `dst` as lowercase hex pairs. Returns -// bytes written. `dst` must hold at least encodedsize(src.len) -// bytes. Mirrors ref/hare/encoding/hex/hex.ha:91 `encode(out, in)` -// (sans the io::handle wrapper). -export fn encode(dst: []u8, src: []u8) i32 = { +// encode_writer — the encoder's io.writer slot. ref/hare/encoding/hex/hex.ha:36. +// Recovers the encoder via the offset-0 cast, lowercase-hex-encodes each +// byte (strconv.u8tos plus a leading "0" pad for single-digit nibbles, +// as Hare does), forwards to `out` via fmt.fprint, and caches the first +// error in `err` like Hare. Returns the count of bytes written to `out` +// (= 2 * len(in)). +fn encode_writer(s: io.stream, in: []u8) (size | io.error) = { + let e: *encoder = s: *encoder; + match (e.err) { + case let er: io.error => return er; + case void => void; + }; + let z: size = 0; let i: i32 = 0; - let j: i32 = 0; - for (i < src.len) { - let b: u8 = src[i]; - dst[j] = nibble(b >> 4u8); - dst[j + 1] = nibble(b & 15u8); + for (i < in.len) { + let r: str = strconv.u8tos(in[i], strconv.base.HEX_LOWER); + if (r.len == 1) { + match (fmt.fprint(e.out, "0")) { + case let b: size => z += b; + case let er: io.error => { e.err = er; return er; }; + }; + }; + match (fmt.fprint(e.out, r)) { + case let b: size => z += b; + case let er: io.error => { e.err = er; return er; }; + }; i += 1; - j += 2; }; - return j; + return z; }; -// decode — decode hex pairs from `src` into `dst`. Returns count -// of bytes written or invalid on odd-length input or non-hex char. -// Mirrors ref/hare/encoding/hex/hex.ha:175 `decodestr(s)` (sans -// the allocator return). -export fn decode(dst: []u8, src: []u8) (i32 | invalid) = { - if ((src.len & 1) != 0) { let e: invalid; return e; }; - let i: i32 = 0; - let j: i32 = 0; - for (i < src.len) { - let hi: u8 = denibble(src[i]); - let lo: u8 = denibble(src[i + 1]); - if (hi == 255u8) { let e: invalid; return e; }; - if (lo == 255u8) { let e: invalid; return e; }; - dst[j] = (hi << 4u8) | lo; - i += 2; - j += 1; - }; - return j; +// encode — hex-encode `in` and write it to `out`; returns the byte count +// written. ref/hare/encoding/hex/hex.ha:91. Hare wraps io::writeall; ww +// has no io.writeall (fmt.fprint:498-501 — callers drive write-all over +// raw io.write) and encode_writer is whole-slice (never partial), so a +// single io.write is equivalent here. +export fn encode(out: io.handle, in: []u8) (size | io.error) = { + let enc: encoder = newencoder(out); + return io.write(&enc.vt, in); +}; + +// encodestr — hex-encode `in` and return it as a string. +// ref/hare/encoding/hex/hex.ha:68. Hare returns (str | nomem) and the +// caller frees; ww returns a bare `str` per the memio.string rule-9 +// carve-out (memio.ww:208-214 — ww memio.dynamic has no nomem path) and +// leaks the backing buffer (no-GC, process-exit reclaims). +export fn encodestr(in: []u8) str = { + let out: memio.stream = memio.dynamic(); + let enc: encoder = newencoder(&out.vt); + match (io.write(&enc.vt, in)) { + case let n: size => void; + case let e: io.error => abort("hex.encodestr: dynamic memio write failed"); + }; + return memio.string(&out); +}; + +// decodestr — decode a string of hexadecimal bytes into a byte slice. +// ref/hare/encoding/hex/hex.ha:175. Hare returns ([]u8 | errors::invalid +// | nomem) and decodes by copying through a newdecoder stream; ww drops +// nomem (memio rule-9 carve-out, as encodestr) and decodes DIRECTLY — +// the streaming decoder is deferred to #247 (see file header). The +// decode logic mirrors Hare's decode_reader (hex.ha:154-170): odd length +// → invalid, then strconv.stou8 per 2-char pair (case-insensitive). +export fn decodestr(s: str) ([]u8 | errors.invalid) = { + let in: []u8 = strings.toutf8(s); + if ((in.len & 1) != 0) { + let e: errors.invalid; + return e; + }; + let l: i32 = in.len / 2; + // empty bypass: ww alloc([],0) routes through nomem (cf ascii.ww:143). + if (l == 0) { + let empty: []u8; + empty.ptr = nil; + empty.len = 0; + return empty; + }; + let out: []u8 = alloc([], l: u64)!; + let i: i32 = 0; + for (i < l) { + let lo: i32 = i * 2; + let oct: str = strings.frombytes(in[lo : lo + 2]); + let u: u8 = match (strconv.stou8(oct, strconv.base.HEX)) { + case let v: u8 => yield v; + case let e: strconv.invalid => { let er: errors.invalid; return er; }; + case let e: strconv.overflow => { let er: errors.invalid; return er; }; + }; + out[i] = u; + i += 1; + }; + out.len = l; + return out; }; diff --git a/lib/encoding/hex/hextest.ww b/lib/encoding/hex/hextest.ww index 7edd3192..d8142089 100644 --- a/lib/encoding/hex/hextest.ww +++ b/lib/encoding/hex/hextest.ww @@ -1,200 +1,144 @@ -// hextest — exercises lib/encoding/hex. Run with -// `out/bin/ww run lib/encoding/hex/hextest.ww`. Same -// signalled-then-fail()-with-+10 pattern as the rest of the 9xx -// stdlib tests; non-zero exit pinpoints the failing scenario. +// hextest — exercises lib/encoding/hex's io-streaming surface. Run with +// `out/bin/ww run lib/encoding/hex/hextest.ww`. Mirrors Hare's hex +// @test fns (ref/hare/encoding/hex/hex.ha:82,96,194) plus a full-byte +// round-trip. Same signalled-then-fail()-with-+10 pattern as the rest of +// the 9xx stdlib tests; non-zero exit pinpoints the failing scenario. +// +// The streaming decoder (newdecoder) is deferred (#247), so the decode +// side is exercised through decodestr only. package hex; import bytes; +import errors; import hex; +import io; +import memio; import os; let signalled: i32 = 0; fn fail() void = { os.exit(signalled + 10); }; -fn putstr(s: str, into: []u8, off: i32) i32 = { +fn streq(a: str, b: str) bool = { + if (a.len != b.len) { return false; }; let i: i32 = 0; - for (i < s.len) { - into[off + i] = s[i]; - i += 1; - }; - return off + s.len; -}; - -fn streq(buf: []u8, expect: str) bool = { - if (buf.len != expect.len) { return false; }; - let i: i32 = 0; - for (i < buf.len) { - if (buf[i] != expect[i]) { return false; }; + for (i < a.len) { + if (a[i] != b[i]) { return false; }; i += 1; }; return true; }; -// ---- encodedsize / decodedsize ----------------------------------------- - -@test fn sizes() void = { - if (hex.encodedsize(0) != 0) { fail(); }; - if (hex.encodedsize(1) != 2) { fail(); }; - if (hex.encodedsize(8) != 16) { fail(); }; - if (hex.decodedsize(0) != 0) { fail(); }; - if (hex.decodedsize(2) != 1) { fail(); }; - if (hex.decodedsize(16) != 8) { fail(); }; +fn cafebabe() [8]u8 = { + let r: [8]u8; + r[0] = 0xCAu8; r[1] = 0xFEu8; r[2] = 0xBAu8; r[3] = 0xBEu8; + r[4] = 0xDEu8; r[5] = 0xADu8; r[6] = 0xF0u8; r[7] = 0x0Du8; + return r; }; -// ---- encode: lowercase, all-bytes coverage ----------------------------- -// -// Hare test vector ref/hare/encoding/hex/hex.ha:82. +// ---- encodestr ---- ref/hare/encoding/hex/hex.ha:82 -@test fn encode_basic() void = { - let src: [8]u8; - src[0] = 0xCAu8; src[1] = 0xFEu8; src[2] = 0xBAu8; src[3] = 0xBEu8; - src[4] = 0xDEu8; src[5] = 0xADu8; src[6] = 0xF0u8; src[7] = 0x0Du8; - let dst: [16]u8; - let n: i32 = hex.encode(dst[0:16], src[0:8]); - if (n != 16) { fail(); }; - if (!streq(dst[0:16], "cafebabedeadf00d")) { fail(); }; +@test fn encodestr_basic() void = { + let in: [8]u8 = cafebabe(); + if (!streq(hex.encodestr(in[0:8]), "cafebabedeadf00d")) { fail(); }; }; -// 0x00 in / "00" out catches a sign-extend / signed-shift miscompile -// on the high nibble. - -@test fn encode_zero() void = { - let src: [1]u8; - src[0] = 0u8; - let dst: [2]u8; - let n: i32 = hex.encode(dst[0:2], src[0:1]); - if (n != 2) { fail(); }; - if (!streq(dst[0:2], "00")) { fail(); }; +// 0x00 → "00" guards a sign-extend / signed-shift on the high nibble. +@test fn encodestr_zero() void = { + let in: [1]u8; in[0] = 0u8; + if (!streq(hex.encodestr(in[0:1]), "00")) { fail(); }; }; -// 0xFF in / "ff" out catches an off-by-one in the nibble lookup or -// a wrong-width shift. - -@test fn encode_ff() void = { - let src: [1]u8; - src[0] = 0xFFu8; - let dst: [2]u8; - let n: i32 = hex.encode(dst[0:2], src[0:1]); - if (n != 2) { fail(); }; - if (!streq(dst[0:2], "ff")) { fail(); }; +// 0xFF → "ff" guards an off-by-one in the digit lookup. +@test fn encodestr_ff() void = { + let in: [1]u8; in[0] = 0xFFu8; + if (!streq(hex.encodestr(in[0:1]), "ff")) { fail(); }; }; -// Empty input is a no-op encode. - -@test fn encode_empty() void = { - let src: [1]u8; - let dst: [1]u8; - let n: i32 = hex.encode(dst[0:0], src[0:0]); - if (n != 0) { fail(); }; +@test fn encodestr_empty() void = { + let in: [1]u8; + if (hex.encodestr(in[0:0]).len != 0) { fail(); }; }; -// ---- decode: lowercase, uppercase, mixed ------------------------------- +// ---- encode (io.handle sink) ---- ref/hare/encoding/hex/hex.ha:96 -@test fn decode_lower() void = { - let inbuf: [16]u8; - let n: i32 = putstr("cafebabedeadf00d", inbuf[0:16], 0); - let dst: [8]u8; - let r: (i32 | hex.invalid) = hex.decode(dst[0:8], inbuf[0:n]); - match (r) { - case let m: i32 => { - if (m != 8) { fail(); }; - let want: [8]u8; - want[0] = 0xCAu8; want[1] = 0xFEu8; want[2] = 0xBAu8; want[3] = 0xBEu8; - want[4] = 0xDEu8; want[5] = 0xADu8; want[6] = 0xF0u8; want[7] = 0x0Du8; - if (!bytes.equal(dst[0:8], want[0:8])) { fail(); }; +@test fn encode_stream() void = { + let in: [8]u8 = cafebabe(); + let out: memio.stream = memio.dynamic(); + match (hex.encode(&out.vt, in[0:8])) { + case let n: size => { if (n: i32 != 16) { fail(); }; }; + case let e: io.error => fail(); }; - case let e: hex.invalid => { fail(); }; + if (!streq(memio.string(&out), "cafebabedeadf00d")) { fail(); }; +}; + +// ---- decodestr round-trip ---- ref/hare/encoding/hex/hex.ha:194 + +@test fn decodestr_lower() void = { + match (hex.decodestr("cafebabedeadf00d")) { + case let b: []u8 => { + let want: [8]u8 = cafebabe(); + if (!bytes.equal(b, want[0:8])) { fail(); }; + }; + case let e: errors.invalid => fail(); }; }; -@test fn decode_upper() void = { - let inbuf: [16]u8; - let n: i32 = putstr("CAFEBABEDEADF00D", inbuf[0:16], 0); - let dst: [8]u8; - let r: (i32 | hex.invalid) = hex.decode(dst[0:8], inbuf[0:n]); - match (r) { - case let m: i32 => { - if (m != 8) { fail(); }; - let want: [8]u8; - want[0] = 0xCAu8; want[1] = 0xFEu8; want[2] = 0xBAu8; want[3] = 0xBEu8; - want[4] = 0xDEu8; want[5] = 0xADu8; want[6] = 0xF0u8; want[7] = 0x0Du8; - if (!bytes.equal(dst[0:8], want[0:8])) { fail(); }; - }; - case let e: hex.invalid => { fail(); }; - }; -}; - -// Mixed-case must decode too; Hare's encoder is lowercase-only but +// Mixed/upper case must decode too; the encoder is lowercase-only but // the decoder accepts both per ref/hare/encoding/hex/README:13. - -@test fn decode_mixed() void = { - let inbuf: [16]u8; - let n: i32 = putstr("CaFeBaBeDeAdF00d", inbuf[0:16], 0); - let dst: [8]u8; - let r: (i32 | hex.invalid) = hex.decode(dst[0:8], inbuf[0:n]); - match (r) { - case let m: i32 => { - if (m != 8) { fail(); }; - let want: [8]u8; - want[0] = 0xCAu8; want[1] = 0xFEu8; want[2] = 0xBAu8; want[3] = 0xBEu8; - want[4] = 0xDEu8; want[5] = 0xADu8; want[6] = 0xF0u8; want[7] = 0x0Du8; - if (!bytes.equal(dst[0:8], want[0:8])) { fail(); }; +@test fn decodestr_upper() void = { + match (hex.decodestr("CAFEBABEDEADF00D")) { + case let b: []u8 => { + let want: [8]u8 = cafebabe(); + if (!bytes.equal(b, want[0:8])) { fail(); }; }; - case let e: hex.invalid => { fail(); }; + case let e: errors.invalid => fail(); }; }; -@test fn decode_empty() void = { - let inbuf: [1]u8; - let dst: [1]u8; - let r: (i32 | hex.invalid) = hex.decode(dst[0:0], inbuf[0:0]); - match (r) { - case let m: i32 => { if (m != 0) { fail(); }; }; - case let e: hex.invalid => { fail(); }; +@test fn decodestr_mixed() void = { + match (hex.decodestr("CaFeBaBeDeAdF00d")) { + case let b: []u8 => { + let want: [8]u8 = cafebabe(); + if (!bytes.equal(b, want[0:8])) { fail(); }; + }; + case let e: errors.invalid => fail(); }; }; -// ---- decode: error cases ----------------------------------------------- +@test fn decodestr_empty() void = { + match (hex.decodestr("")) { + case let b: []u8 => { if (b.len != 0) { fail(); }; }; + case let e: errors.invalid => fail(); + }; +}; + +// ---- decodestr error cases ---- ref/hare/encoding/hex/hex.ha:154,199 // -// Odd length and non-hex chars both return invalid. Hare's -// decode_reader at ref/hare/encoding/hex/hex.ha:154 returns -// errors::invalid for both. +// Odd length and non-hex chars both return errors.invalid. -@test fn decode_odd_length() void = { - let inbuf: [3]u8; - let n: i32 = putstr("abc", inbuf[0:3], 0); - let dst: [2]u8; - let r: (i32 | hex.invalid) = hex.decode(dst[0:2], inbuf[0:n]); - match (r) { - case let m: i32 => { fail(); }; - case let e: hex.invalid => void; +@test fn decodestr_odd() void = { + match (hex.decodestr("abc")) { + case let b: []u8 => fail(); + case let e: errors.invalid => void; }; }; -@test fn decode_bad_char() void = { - let inbuf: [4]u8; - let n: i32 = putstr("zz00", inbuf[0:4], 0); // 'z' isn't hex - let dst: [2]u8; - let r: (i32 | hex.invalid) = hex.decode(dst[0:2], inbuf[0:n]); - match (r) { - case let m: i32 => { fail(); }; - case let e: hex.invalid => void; +@test fn decodestr_bad() void = { + match (hex.decodestr("zz00")) { // 'z' isn't hex + case let b: []u8 => fail(); + case let e: errors.invalid => void; }; }; -@test fn decode_bad_char_mid() void = { - let inbuf: [6]u8; - let n: i32 = putstr("aabbgg", inbuf[0:6], 0); // 'g' isn't hex - let dst: [3]u8; - let r: (i32 | hex.invalid) = hex.decode(dst[0:3], inbuf[0:n]); - match (r) { - case let m: i32 => { fail(); }; - case let e: hex.invalid => void; +@test fn decodestr_bad_mid() void = { + match (hex.decodestr("aabbgg")) { // 'g' isn't hex + case let b: []u8 => fail(); + case let e: errors.invalid => void; }; }; -// ---- roundtrip: every byte value 0..255 -------------------------------- +// ---- round-trip every byte value 0..255 -------------------------------- @test fn roundtrip_all_bytes() void = { let src: [256]u8; @@ -203,33 +147,30 @@ fn streq(buf: []u8, expect: str) bool = { src[i] = i: u8; i += 1; }; - let enc: [512]u8; - let n: i32 = hex.encode(enc[0:512], src[0:256]); - if (n != 512) { fail(); }; - let dec: [256]u8; - let r: (i32 | hex.invalid) = hex.decode(dec[0:256], enc[0:n]); - match (r) { - case let m: i32 => { - if (m != 256) { fail(); }; - if (!bytes.equal(src[0:256], dec[0:256])) { fail(); }; + let s: str = hex.encodestr(src[0:256]); + if (s.len != 512) { fail(); }; + match (hex.decodestr(s)) { + case let b: []u8 => { + if (b.len != 256) { fail(); }; + if (!bytes.equal(b, src[0:256])) { fail(); }; }; - case let e: hex.invalid => { fail(); }; + case let e: errors.invalid => fail(); }; }; export fn main() i32 = { - signalled = 1; sizes(); - signalled = 2; encode_basic(); - signalled = 3; encode_zero(); - signalled = 4; encode_ff(); - signalled = 5; encode_empty(); - signalled = 6; decode_lower(); - signalled = 7; decode_upper(); - signalled = 8; decode_mixed(); - signalled = 9; decode_empty(); - signalled = 10; decode_odd_length(); - signalled = 11; decode_bad_char(); - signalled = 12; decode_bad_char_mid(); + signalled = 1; encodestr_basic(); + signalled = 2; encodestr_zero(); + signalled = 3; encodestr_ff(); + signalled = 4; encodestr_empty(); + signalled = 5; encode_stream(); + signalled = 6; decodestr_lower(); + signalled = 7; decodestr_upper(); + signalled = 8; decodestr_mixed(); + signalled = 9; decodestr_empty(); + signalled = 10; decodestr_odd(); + signalled = 11; decodestr_bad(); + signalled = 12; decodestr_bad_mid(); signalled = 13; roundtrip_all_bytes(); return 0; }; diff --git a/lib/encoding/utf8/utf8.ww b/lib/encoding/utf8/utf8.ww index 60e5c97f..fc254a37 100644 --- a/lib/encoding/utf8/utf8.ww +++ b/lib/encoding/utf8/utf8.ww @@ -11,8 +11,7 @@ // // - `encoderune` takes a caller-supplied `out: []u8` and returns // the byte count. Hare returns a slice into a `static let buf`; -// the caller-buffer form mirrors lib/encoding/hex.encode and -// skips the static-buffer/slice-return pair. +// the caller-buffer form skips the static-buffer/slice-return pair. // // Deferred (no in-tree caller, follow-up tasks): `appendrune`, // `strencode`, `strdecode`. Hare's string-iteration surface @@ -309,8 +308,8 @@ export fn utf8sz(c: u8) (i32 | invalid) = { // same precondition Hare asserts at ref/hare/encoding/utf8/encode.ha:9. // // Surface deviation: Hare returns `[]u8` (slice into a static buf). -// ww uses the caller-buffer form (matches lib/encoding/hex.encode); -// caller can reuse a [4]u8 stack scratch across encodes. +// ww uses the caller-buffer form; caller can reuse a [4]u8 stack +// scratch across encodes. export fn encoderune(out: []u8, r: rune) i32 = { let ch: u32 = r: u32; if (ch >= 0xD800u32) { diff --git a/selfhost/cmd/w6a/main.combined.ww b/selfhost/cmd/w6a/main.combined.ww index 60e38acc..952dc159 100644 --- a/selfhost/cmd/w6a/main.combined.ww +++ b/selfhost/cmd/w6a/main.combined.ww @@ -1442,8 +1442,7 @@ export fn rcut(in: []u8, delim: (u8 | []u8)) ([]u8, []u8) = { // // - `encoderune` takes a caller-supplied `out: []u8` and returns // the byte count. Hare returns a slice into a `static let buf`; -// the caller-buffer form mirrors lib/encoding/hex.encode and -// skips the static-buffer/slice-return pair. +// the caller-buffer form skips the static-buffer/slice-return pair. // // Deferred (no in-tree caller, follow-up tasks): `appendrune`, // `strencode`, `strdecode`. Hare's string-iteration surface @@ -1740,8 +1739,8 @@ export fn utf8sz(c: u8) (i32 | invalid) = { // same precondition Hare asserts at ref/hare/encoding/utf8/encode.ha:9. // // Surface deviation: Hare returns `[]u8` (slice into a static buf). -// ww uses the caller-buffer form (matches lib/encoding/hex.encode); -// caller can reuse a [4]u8 stack scratch across encodes. +// ww uses the caller-buffer form; caller can reuse a [4]u8 stack +// scratch across encodes. export fn encoderune(out: []u8, r: rune) i32 = { let ch: u32 = r: u32; if (ch >= 0xD800u32) { diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index c300308c..37075500 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -1442,8 +1442,7 @@ export fn rcut(in: []u8, delim: (u8 | []u8)) ([]u8, []u8) = { // // - `encoderune` takes a caller-supplied `out: []u8` and returns // the byte count. Hare returns a slice into a `static let buf`; -// the caller-buffer form mirrors lib/encoding/hex.encode and -// skips the static-buffer/slice-return pair. +// the caller-buffer form skips the static-buffer/slice-return pair. // // Deferred (no in-tree caller, follow-up tasks): `appendrune`, // `strencode`, `strdecode`. Hare's string-iteration surface @@ -1740,8 +1739,8 @@ export fn utf8sz(c: u8) (i32 | invalid) = { // same precondition Hare asserts at ref/hare/encoding/utf8/encode.ha:9. // // Surface deviation: Hare returns `[]u8` (slice into a static buf). -// ww uses the caller-buffer form (matches lib/encoding/hex.encode); -// caller can reuse a [4]u8 stack scratch across encodes. +// ww uses the caller-buffer form; caller can reuse a [4]u8 stack +// scratch across encodes. export fn encoderune(out: []u8, r: rune) i32 = { let ch: u32 = r: u32; if (ch >= 0xD800u32) { diff --git a/selfhost/cmd/w6l/main.combined.ww b/selfhost/cmd/w6l/main.combined.ww index ab14d111..43d846b9 100644 --- a/selfhost/cmd/w6l/main.combined.ww +++ b/selfhost/cmd/w6l/main.combined.ww @@ -1551,8 +1551,7 @@ export fn rcut(in: []u8, delim: (u8 | []u8)) ([]u8, []u8) = { // // - `encoderune` takes a caller-supplied `out: []u8` and returns // the byte count. Hare returns a slice into a `static let buf`; -// the caller-buffer form mirrors lib/encoding/hex.encode and -// skips the static-buffer/slice-return pair. +// the caller-buffer form skips the static-buffer/slice-return pair. // // Deferred (no in-tree caller, follow-up tasks): `appendrune`, // `strencode`, `strdecode`. Hare's string-iteration surface @@ -1849,8 +1848,8 @@ export fn utf8sz(c: u8) (i32 | invalid) = { // same precondition Hare asserts at ref/hare/encoding/utf8/encode.ha:9. // // Surface deviation: Hare returns `[]u8` (slice into a static buf). -// ww uses the caller-buffer form (matches lib/encoding/hex.encode); -// caller can reuse a [4]u8 stack scratch across encodes. +// ww uses the caller-buffer form; caller can reuse a [4]u8 stack +// scratch across encodes. export fn encoderune(out: []u8, r: rune) i32 = { let ch: u32 = r: u32; if (ch >= 0xD800u32) { diff --git a/selfhost/cmd/ww/main.combined.ww b/selfhost/cmd/ww/main.combined.ww index bf9563e3..941c6cc5 100644 --- a/selfhost/cmd/ww/main.combined.ww +++ b/selfhost/cmd/ww/main.combined.ww @@ -1442,8 +1442,7 @@ export fn rcut(in: []u8, delim: (u8 | []u8)) ([]u8, []u8) = { // // - `encoderune` takes a caller-supplied `out: []u8` and returns // the byte count. Hare returns a slice into a `static let buf`; -// the caller-buffer form mirrors lib/encoding/hex.encode and -// skips the static-buffer/slice-return pair. +// the caller-buffer form skips the static-buffer/slice-return pair. // // Deferred (no in-tree caller, follow-up tasks): `appendrune`, // `strencode`, `strdecode`. Hare's string-iteration surface @@ -1740,8 +1739,8 @@ export fn utf8sz(c: u8) (i32 | invalid) = { // same precondition Hare asserts at ref/hare/encoding/utf8/encode.ha:9. // // Surface deviation: Hare returns `[]u8` (slice into a static buf). -// ww uses the caller-buffer form (matches lib/encoding/hex.encode); -// caller can reuse a [4]u8 stack scratch across encodes. +// ww uses the caller-buffer form; caller can reuse a [4]u8 stack +// scratch across encodes. export fn encoderune(out: []u8, r: rune) i32 = { let ch: u32 = r: u32; if (ch >= 0xD800u32) { diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index e5014765..1824216a 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -2936,8 +2936,7 @@ export fn rcut(in: []u8, delim: (u8 | []u8)) ([]u8, []u8) = { // // - `encoderune` takes a caller-supplied `out: []u8` and returns // the byte count. Hare returns a slice into a `static let buf`; -// the caller-buffer form mirrors lib/encoding/hex.encode and -// skips the static-buffer/slice-return pair. +// the caller-buffer form skips the static-buffer/slice-return pair. // // Deferred (no in-tree caller, follow-up tasks): `appendrune`, // `strencode`, `strdecode`. Hare's string-iteration surface @@ -3234,8 +3233,8 @@ export fn utf8sz(c: u8) (i32 | invalid) = { // same precondition Hare asserts at ref/hare/encoding/utf8/encode.ha:9. // // Surface deviation: Hare returns `[]u8` (slice into a static buf). -// ww uses the caller-buffer form (matches lib/encoding/hex.encode); -// caller can reuse a [4]u8 stack scratch across encodes. +// ww uses the caller-buffer form; caller can reuse a [4]u8 stack +// scratch across encodes. export fn encoderune(out: []u8, r: rune) i32 = { let ch: u32 = r: u32; if (ch >= 0xD800u32) { diff --git a/selfhost/test/smoke.combined.ww b/selfhost/test/smoke.combined.ww index fc203adf..afe38ebe 100644 --- a/selfhost/test/smoke.combined.ww +++ b/selfhost/test/smoke.combined.ww @@ -2936,8 +2936,7 @@ export fn rcut(in: []u8, delim: (u8 | []u8)) ([]u8, []u8) = { // // - `encoderune` takes a caller-supplied `out: []u8` and returns // the byte count. Hare returns a slice into a `static let buf`; -// the caller-buffer form mirrors lib/encoding/hex.encode and -// skips the static-buffer/slice-return pair. +// the caller-buffer form skips the static-buffer/slice-return pair. // // Deferred (no in-tree caller, follow-up tasks): `appendrune`, // `strencode`, `strdecode`. Hare's string-iteration surface @@ -3234,8 +3233,8 @@ export fn utf8sz(c: u8) (i32 | invalid) = { // same precondition Hare asserts at ref/hare/encoding/utf8/encode.ha:9. // // Surface deviation: Hare returns `[]u8` (slice into a static buf). -// ww uses the caller-buffer form (matches lib/encoding/hex.encode); -// caller can reuse a [4]u8 stack scratch across encodes. +// ww uses the caller-buffer form; caller can reuse a [4]u8 stack +// scratch across encodes. export fn encoderune(out: []u8, r: rune) i32 = { let ch: u32 = r: u32; if (ch >= 0xD800u32) { diff --git a/test/wcc/900_stdlib.c b/test/wcc/900_stdlib.c index b81ab05f..4c69ac10 100644 --- a/test/wcc/900_stdlib.c +++ b/test/wcc/900_stdlib.c @@ -17,7 +17,6 @@ static const char *modules[] = { "lib/sort/sort.ww", "lib/path/path.ww", "lib/encoding/utf8/utf8.ww", - "lib/encoding/hex/hex.ww", "lib/encoding/base32/base32.ww", "lib/encoding/base64/base64.ww", "lib/hash/fnv/fnv.ww", @@ -39,13 +38,15 @@ static const char *modules[] = { * os.strerror (ww folds Hare's sys role into os); lib/strings.iterator * + lib/strings.next reference utf8.decoder / utf8.done; * lib/bytes.tokenize references os.assert + types.I64_MAX/MIN per - * ref/hare/bytes/tokenize.ha:23-24,42-43. Coverage lives at + * ref/hare/bytes/tokenize.ha:23-24,42-43; lib/encoding/hex graduated + * to Hare's io-streaming surface (references io.handle / fmt.fprint / + * memio.dynamic / strconv / errors.invalid). Coverage lives at * lib/bufio/bufiotest.ww + lib/bytes/bytestest.ww + * lib/errors/errnotest.ww + lib/fmt/fmttest.ww + lib/os/stattest.ww - * + lib/strings/stringstest.ww (wired at 998_bufio_run.c, - * 967_bytes_run.c, 902_errno_run.c, 970_fmt_run.c, 976_stat_run.c, - * 966_strings_run.c), plus the bufio.scanline / fmt.println e2e - * rows in test/wcc/700_e2e.c. */ + * + lib/strings/stringstest.ww + lib/encoding/hex/hextest.ww (wired + * at 998_bufio_run.c, 967_bytes_run.c, 902_errno_run.c, 970_fmt_run.c, + * 976_stat_run.c, 966_strings_run.c, 979_hex_run.c), plus the + * bufio.scanline / fmt.println e2e rows in test/wcc/700_e2e.c. */ "lib/net/net.ww", NULL };