lib: graduate bytes/strings find-funcs to (i32 | void)

Replaces the -1 sentinel return on indexbyte/byteindex/rbyteindex/
index with Hare's optional-shaped tagged union. Callers `match` on
the result and bind the index from the i32 variant.

Two cgen fixes were needed first:

1. resolve_type for N_TTAGGED rounded value payload up to an 8-byte
   multiple. (i32 | void) was sized 12 — tag (8) + payload (4) —
   which made the reg-passing ABI compute size/8 = 1 word and drop
   the value word.

2. The call-arg push path special-cased struct and slice args but
   not tagged-return calls. A nested `f(g())` where g returns a
   tagged union pushed only AX (tag); the matching pop loaded a
   stale DX/SI for the value. Now pushes AX/DX[/CX] in order so
   the pop side drains tag → arg-reg[0], value(s) → arg-reg[1..].

strings.contains rewritten to match on the new tagged result. No
other callers existed in lib/ — bufio/io still use their own
shapes.
This commit is contained in:
2026-05-12 01:49:00 +09:00
parent 41a82021a3
commit 1e2f55aed8
5 changed files with 64 additions and 33 deletions

View File

@@ -39,36 +39,34 @@ export fn hassuffix(s: str, suf: str) bool = {
return true;
};
// byteindex — first index of byte `c` in `s`, or -1 if absent. Hare
// name (strings::byteindex). Plan 9-style sentinel return; callers that
// prefer a fallible shape can wrap this in their own (i32 | str). No
// allocation.
export fn byteindex(s: str, c: u8) i32 = {
// byteindex — first index of byte `c` in `s`. Hare-shaped optional:
// (i32 | void). void variant indicates "not found".
export fn byteindex(s: str, c: u8) (i32 | void) = {
let i: i32 = 0;
for (i < s.len) {
if (s[i] == c) { return i; };
i += 1;
};
return -1;
return;
};
// rbyteindex — last index of byte `c` in `s`, or -1 if absent. Mirrors
// Hare's strings::rbyteindex.
export fn rbyteindex(s: str, c: u8) i32 = {
// rbyteindex — last index of byte `c` in `s`. Mirrors Hare's
// strings::rbyteindex.
export fn rbyteindex(s: str, c: u8) (i32 | void) = {
let i: i32 = s.len - 1;
for (i >= 0) {
if (s[i] == c) { return i; };
i -= 1;
};
return -1;
return;
};
// index — first index of `sub` in `s`, or -1. Naive scan; fine for
// short patterns and small strings, which dominate config and CLI
// parsing. Empty `sub` matches at 0.
export fn index(s: str, sub: str) i32 = {
// index — first index of `sub` in `s`. Naive scan; fine for short
// patterns and small strings, which dominate config and CLI parsing.
// Empty `sub` matches at 0.
export fn index(s: str, sub: str) (i32 | void) = {
if (sub.len == 0) { return 0; };
if (sub.len > s.len) { return -1; };
if (sub.len > s.len) { return; };
let last: i32 = s.len - sub.len;
let i: i32 = 0;
for (i <= last) {
@@ -81,11 +79,16 @@ export fn index(s: str, sub: str) i32 = {
if (ok) { return i; };
i += 1;
};
return -1;
return;
};
export fn contains(s: str, sub: str) bool = {
return index(s, sub) >= 0;
let r: (i32 | void) = index(s, sub);
match (r) {
case let i: i32 => return true;
case void => return false;
};
return false;
};
// concat — joins two strings into a fresh str. Caller owns the