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:
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user