lib/encoding/hex: align to Hare io-streaming surface

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).
This commit is contained in:
2026-06-02 00:33:49 +09:00
parent 9d383288d2
commit e3f49234f9
10 changed files with 263 additions and 267 deletions

View File

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