lib/path: #153 c3 buffer-ops — abs/isroot-widen/local/parent/split/pop/peek faithful port, table-driven @test (cstage)
Faithful ref/hare/path/buffer.ha port of the buffer-mutating ops. isroot widened *buffer->(*buffer|str) (existing callers ptr-ident-widen); abs str-arm strings.hasprefix(p,sepstr) (rides #154); local lifts Hare's `static let buf` to a module-private [MAX]u8 index-write (no ww static-append, regex.ww:1177). Three documented respells (never silent): split binds each (str|void) elem to an ident before return (#22b); peek binds split() to a local (#48); local's index-write. parent via appendnorm + frombytes. Tests mirror Hare's pop() @test 1:1 (stack.ha:147) + table-driven abs/isroot/local/parent rows off a local buffer. cstage-first; wwstage str-arms ride deferred #146 (path stays M_WWREJECT). lib/path non-embedded (no combined.ww regen). Completes the c3 stage of the path arc.
This commit is contained in:
@@ -49,11 +49,24 @@ export type buffer = struct {
|
||||
const dot: []u8 = ['.'];
|
||||
const dotdot: []u8 = ['.', '.'];
|
||||
|
||||
// ref/hare/path/buffer.ha:44-51. c3 WIDENS to isroot(path:(*buffer|str))
|
||||
// alongside abs; c2 needs only the *buffer arm (appendnorm/appendlit
|
||||
// callers).
|
||||
export fn isroot(buf: *buffer) bool = {
|
||||
return buf.end == 1 && buf.buf[0] == SEP;
|
||||
// ref/hare/path/buffer.ha:34-41. str-arm = str-prefix (vs const global,
|
||||
// correct only post-#154); buffer-arm = byte. Hare `0 < buf.end` order kept.
|
||||
export fn abs(path: (*buffer | str)) bool = {
|
||||
match (path) {
|
||||
case let p: str => { return strings.hasprefix(p, sepstr); };
|
||||
case let b: *buffer => { return 0 < b.end && b.buf[0] == SEP; };
|
||||
};
|
||||
};
|
||||
|
||||
// ref/hare/path/buffer.ha:44-51. WIDENED from the c2 *buffer-only body to
|
||||
// the (*buffer | str) union (str-arm = str==, post-#154 correct). The
|
||||
// appendnorm/appendlit callers pass a concrete *buffer that ptr-ident
|
||||
// arg-widens into the union (#55 leg).
|
||||
export fn isroot(path: (*buffer | str)) bool = {
|
||||
match (path) {
|
||||
case let p: str => { return p == sepstr; };
|
||||
case let b: *buffer => { return b.end == 1 && b.buf[0] == SEP; };
|
||||
};
|
||||
};
|
||||
|
||||
// ref/hare/path/stack.ha:63-74.
|
||||
@@ -139,6 +152,79 @@ export fn set(buf: *buffer, items: str...) (str | error) = {
|
||||
return push(buf, items...);
|
||||
};
|
||||
|
||||
// ---- c3 buffer-ops (ref/hare/path/buffer.ha, stack.ha) ----------------
|
||||
|
||||
// lifts Hare's fn-`static let buf` (buffer.ha:53-56): statically allocated,
|
||||
// overwritten on the next local() call. strconv *tos-buf precedent.
|
||||
let localbuf: [MAX]u8 = [0...];
|
||||
|
||||
// ref/hare/path/buffer.ha:55-77. Div: Hare `_local` returns (str|nomem) —
|
||||
// ww has no nomem axis, so the MAX-guard IS the loud bound (→ too_long);
|
||||
// `_local` folded inline (local is its only caller). fromutf8→frombytes
|
||||
// (rule-9 carve-out); `..` reslice→colon; `0z`→`0i32` (div #1,#8).
|
||||
export fn local(path: str) (str | too_long) = {
|
||||
let bs: []u8 = strings.toutf8(path);
|
||||
if (MAX < bs.len) { let e: too_long = too_long; return e; };
|
||||
let n: i32 = 0;
|
||||
for (let b .. bs) {
|
||||
// no ww static-append → index-write (regex.ww:1177 precedent:
|
||||
// static append→plain; index-write keeps the static-view contract)
|
||||
if (b == '/') { localbuf[n] = SEP; } else { localbuf[n] = b; };
|
||||
n += 1;
|
||||
};
|
||||
return strings.frombytes(localbuf[0:n]);
|
||||
};
|
||||
|
||||
// ref/hare/path/stack.ha:133-145. Helper for pop/peek: returns (new end,
|
||||
// result). Over-cap tuple SRET (i32 .0 + (str|void) .1 = 40B > 32B, #22b).
|
||||
// Div: (size,…)→(i32,…) (div #1); `0z`/`1z`→`0i32`/`1`; `..`→`:`;
|
||||
// fromutf8→frombytes; Hare if-expr → if-statement (div #7,#8,#4,#2).
|
||||
fn split(buf: *buffer) (i32, (str | void)) = {
|
||||
if (buf.end == 0 || isroot(buf)) {
|
||||
let e: i32 = buf.end;
|
||||
let r: (str | void) = void; // #22b: bind-then-return
|
||||
return (e, r);
|
||||
};
|
||||
match (bytes.rindex(buf.buf[0:buf.end], SEP)) {
|
||||
case void => {
|
||||
let e: i32 = 0i32;
|
||||
let r: (str | void) = strings.frombytes(buf.buf[0:buf.end]); // #22b: bind-then-return
|
||||
return (e, r);
|
||||
};
|
||||
case let i: i32 => {
|
||||
let e: i32 = i;
|
||||
if (i == 0) { e = 1; };
|
||||
let r: (str | void) = strings.frombytes(buf.buf[i+1:buf.end]); // #22b: bind-then-return
|
||||
return (e, r);
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
// ref/hare/path/stack.ha:125-131. Remove and return the final segment;
|
||||
// void if the path is empty or the root dir.
|
||||
export fn pop(buf: *buffer) (str | void) = {
|
||||
const (end, res) = split(buf);
|
||||
buf.end = end;
|
||||
return res;
|
||||
};
|
||||
|
||||
// ref/hare/path/stack.ha:121-123. Examine the final segment without
|
||||
// mutating the buffer; void if empty or root.
|
||||
export fn peek(buf: *buffer) (str | void) = {
|
||||
let t = split(buf); // #48: bind split to local (not .1 off a CALL)
|
||||
return t.1;
|
||||
};
|
||||
|
||||
// ref/hare/path/stack.ha:172-180. Parent directory; root → root. Hare's
|
||||
// doc says "without modifying the buffer" but the BODY mutates via
|
||||
// appendnorm — ported verbatim, not "fixed". Div: drop `const` ptr-qual;
|
||||
// fromutf8→frombytes; `..`→`:` (div #2,#4).
|
||||
export fn parent(buf: *buffer) (str | error) = {
|
||||
let newend: i32 = appendnorm(buf, dotdot)?;
|
||||
if (newend == 0) { return "."; };
|
||||
return strings.frombytes(buf.buf[0:newend]);
|
||||
};
|
||||
|
||||
// ---- POSIX jail (ref/hare/path/posix.ha) ------------------------------
|
||||
// POSIX-compliant dirname/basename. They do NOT normalize the input and
|
||||
// operate on plain str (no buffer), so they sit apart from the stack
|
||||
|
||||
Reference in New Issue
Block a user