Sweeping rename so the lib/ surface mirrors Hare's stdlib spellings. - ascii: rune-taking predicates; ishex -> isxdigit - bufio: rinit -> init; take1/takeline -> readbyte/readline - bytes: indexsub -> index - encoding/utf8: runelen -> runesz - errors: eEOF/eShortRead/... -> eof/underread/... - fmt: errln -> errorln; println/fprintln return i64 - os: readfull/writefull -> readall/writeall; unlink -> remove - path: isabs -> abs; drop lastindex (now strings.rbyteindex) - strconv: u64toa/i64toa -> u64tos/i64tos; parse64/parseu64 -> stoi64/stou64 - strings: drop len/isempty; equal -> compare; indexbyte -> byteindex; +rbyteindex - types: drop numeric helpers (moved to math) - new lib/endian (htonu16/ntohu16), lib/math (absi32/absi64) - net: drop htons (use endian.htonu16) Callers in selfhost/, lib/ww/, cmd/w6c/cgen.c, and test/wcc/700_e2e.c updated to match.
53 lines
1.1 KiB
Plaintext
53 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;
|
|
};
|
|
|
|
export fn indexbyte(s: []u8, c: u8) i32 = {
|
|
let i: i32 = 0;
|
|
for (i < s.len) {
|
|
if (s[i] == c) { return i; };
|
|
i += 1;
|
|
};
|
|
return -1;
|
|
};
|
|
|
|
export fn copy(dst: []u8, src: []u8) i32 = {
|
|
let n: i32 = dst.len;
|
|
if (src.len < n) { n = src.len; };
|
|
let i: i32 = 0;
|
|
for (i < n) {
|
|
dst[i] = src[i];
|
|
i += 1;
|
|
};
|
|
return n;
|
|
};
|
|
|
|
// index — first index of `sub` in `s`, or -1. 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 = {
|
|
if (sub.len == 0) { return 0; };
|
|
if (sub.len > s.len) { return -1; };
|
|
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 -1;
|
|
};
|