Files
ww/lib/bytes/bytes.ww
Hojun-Cho 9bc973dc96 lib: drop non-Hare extras (ascii.digitval, bytes.copy, fmt.errpos)
ascii.digitval/isidstart/isidpart are lexer-private, not Hare-stdlib.
Moved into lib/ww/lex/lex.ww as fn (renamed digitval to hexval since
its sole job is the \\xHH escape decoder).

bytes.copy and fmt.errpos have no Hare counterpart and no external
callers; gone.
2026-05-13 03:24:33 +09:00

44 lines
1.1 KiB
Plaintext

// bytes — slice operations over []u8.
export fn equal(a: []u8, b: []u8) bool = {
let i: i32 = 0;
for (i < a.len) {
if (i >= b.len) { return false; };
if (a[i] != b[i]) { return false; };
i += 1;
};
return i == b.len;
};
// indexbyte — first index of byte `c` in `s`. Hare-shaped optional:
// (i32 | void). void variant indicates "not found".
export fn indexbyte(s: []u8, c: u8) (i32 | void) = {
let i: i32 = 0;
for (i < s.len) {
if (s[i] == c) { return i; };
i += 1;
};
return;
};
// index — first index of `sub` in `s`. Mirrors Hare's bytes::index
// (the []u8 needle variant; the u8 needle stays as indexbyte until we
// have union-arg dispatch). Empty `sub` matches at 0.
export fn index(s: []u8, sub: []u8) (i32 | void) = {
if (sub.len == 0) { return 0; };
if (sub.len > s.len) { return; };
let last: i32 = s.len - sub.len;
let i: i32 = 0;
for (i <= last) {
let j: i32 = 0;
let ok: bool = true;
for (j < sub.len) {
if (s[i + j] != sub[j]) { ok = false; j = sub.len; }
else { j += 1; };
};
if (ok) { return i; };
i += 1;
};
return;
};