contains(s, needle: (u8|[]u8)) -> contains(s: []u8, needles:
(u8|[]u8)...) bool per ref/hare/bytes/contains.ha:6.
Body: for-loop over needles.len; inner match (needles[i]) with u8/
[]u8 arms each forwarding to index(s, ...) with early return true
on the i32-match arm. 0-arg returns false per Hare spec. Mirrors
sister #9 strings.contains body shape modulo element type.
Sister of #9 (7c5463c). Element shape (u8|[]u8) — slice payload +
scalar u8 — structurally distinct from (str|rune). Was flagged as
potential new-latent surface; verified clean by 967_bytes_run +
cross-module 750_mklabel_modscoped[bytes_strings_contains] +
995_self_rebuild byte-id. No cgen wedge fired — #15 frame growth +
#12 sum-tag forward + #16 fnparamslookupmod close it on the slice-
payload variant too.
contains_cases adds 5 variadic rows (signalled 1700+i): 0-arg false,
1-arg slice hit, 1-arg u8 hit, 3-arg mixed middle-hit, 3-arg all-miss.
Module-header non-variadic divergence note removed.
make test 126/126; ww2==ww3==ww4 byte-id holds via 995_self_rebuild.
162 lines
3.7 KiB
Plaintext
162 lines
3.7 KiB
Plaintext
// bytes — slice operations over []u8. Mirrors Hare's bytes module
|
|
// (ref/hare/bytes/) for the in-tree subset: search/equality/prefix
|
|
// helpers used by lib/encoding, lib/bufio, lib/memio.
|
|
//
|
|
// Documented divergences from Hare:
|
|
// - index_slice / rindex_slice use naive O(n·m); Hare specialises
|
|
// 2/3/4-byte needles and falls back to two_way (Crochemore-Perrin)
|
|
// for longer (ref/hare/bytes/index.ha:61, ref/hare/bytes/two_way.ha).
|
|
// Correctness equivalent.
|
|
|
|
// equal — true iff `a` and `b` have the same length and contents.
|
|
// ref/hare/bytes/equal.ha:9.
|
|
package bytes;
|
|
|
|
export fn equal(a: []u8, b: []u8) bool = {
|
|
if (a.len != b.len) { return false; };
|
|
let i: i32 = 0;
|
|
for (i < a.len) {
|
|
if (a[i] != b[i]) { return false; };
|
|
i += 1;
|
|
};
|
|
return true;
|
|
};
|
|
|
|
// index — first offset of `needle` in `s`. u8 needle scans for the
|
|
// byte; []u8 needle scans for the substring. void if absent.
|
|
// ref/hare/bytes/index.ha:6.
|
|
export fn index(s: []u8, needle: (u8 | []u8)) (i32 | void) = {
|
|
match (needle) {
|
|
case let c: u8 => {
|
|
let i: i32 = 0;
|
|
for (i < s.len) {
|
|
if (s[i] == c) { return i; };
|
|
i += 1;
|
|
};
|
|
return;
|
|
};
|
|
case let sub: []u8 => {
|
|
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;
|
|
};
|
|
};
|
|
return;
|
|
};
|
|
|
|
// rindex — last offset of `needle` in `s`. Empty []u8 needle returns
|
|
// s.len (ref/hare/bytes/index.ha:103 — Hare's loop yields r-0 at i=0).
|
|
// ref/hare/bytes/index.ha:86.
|
|
export fn rindex(s: []u8, needle: (u8 | []u8)) (i32 | void) = {
|
|
match (needle) {
|
|
case let c: u8 => {
|
|
let i: i32 = s.len - 1;
|
|
for (i >= 0) {
|
|
if (s[i] == c) { return i; };
|
|
i -= 1;
|
|
};
|
|
return;
|
|
};
|
|
case let sub: []u8 => {
|
|
if (sub.len == 0) { return s.len; };
|
|
if (sub.len > s.len) { return; };
|
|
let i: i32 = s.len - sub.len;
|
|
for (i >= 0) {
|
|
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;
|
|
};
|
|
};
|
|
return;
|
|
};
|
|
|
|
// contains — true iff any of `needles` (byte or sub-slice) appears in `s`.
|
|
// ref/hare/bytes/contains.ha:6.
|
|
export fn contains(s: []u8, needles: (u8 | []u8)...) bool = {
|
|
let i: i32 = 0;
|
|
for (i < needles.len) {
|
|
match (needles[i]) {
|
|
case let b: u8 => {
|
|
match (index(s, b)) {
|
|
case let bo: i32 => return true;
|
|
case void => void;
|
|
};
|
|
};
|
|
case let n: []u8 => {
|
|
match (index(s, n)) {
|
|
case let bo: i32 => return true;
|
|
case void => void;
|
|
};
|
|
};
|
|
};
|
|
i += 1;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
// hasprefix — true iff `s` starts with `pre`.
|
|
// ref/hare/bytes/contains.ha:21.
|
|
export fn hasprefix(s: []u8, pre: []u8) bool = {
|
|
if (pre.len > s.len) { return false; };
|
|
let i: i32 = 0;
|
|
for (i < pre.len) {
|
|
if (s[i] != pre[i]) { return false; };
|
|
i += 1;
|
|
};
|
|
return true;
|
|
};
|
|
|
|
// hassuffix — true iff `s` ends with `suf`.
|
|
// ref/hare/bytes/contains.ha:35.
|
|
export fn hassuffix(s: []u8, suf: []u8) bool = {
|
|
if (suf.len > s.len) { return false; };
|
|
let off: i32 = s.len - suf.len;
|
|
let i: i32 = 0;
|
|
for (i < suf.len) {
|
|
if (s[off + i] != suf[i]) { return false; };
|
|
i += 1;
|
|
};
|
|
return true;
|
|
};
|
|
|
|
// reverse — in-place reverse of `s`. ref/hare/bytes/reverse.ha:5.
|
|
export fn reverse(s: []u8) void = {
|
|
let i: i32 = 0;
|
|
let j: i32 = s.len - 1;
|
|
for (i < j) {
|
|
let t: u8 = s[i];
|
|
s[i] = s[j];
|
|
s[j] = t;
|
|
i += 1;
|
|
j -= 1;
|
|
};
|
|
};
|
|
|
|
// zero — set every byte of `s` to 0. ref/hare/bytes/zero.ha:5.
|
|
export fn zero(s: []u8) void = {
|
|
let i: i32 = 0;
|
|
for (i < s.len) {
|
|
s[i] = 0u8;
|
|
i += 1;
|
|
};
|
|
};
|