lib: rename stdlib surface to Hare names; add endian/math
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.
This commit is contained in:
@@ -1,23 +1,21 @@
|
||||
// strings — operations over the immutable str type ({ *u8, len }).
|
||||
// Mirrors Hare's strings::; `len` and `is-empty` aren't functions
|
||||
// (callers use `s.len` and `s.len == 0` directly).
|
||||
|
||||
use os;
|
||||
|
||||
export fn len(s: str) i32 = {
|
||||
return s.len;
|
||||
};
|
||||
|
||||
export fn isempty(s: str) bool = {
|
||||
return s.len == 0;
|
||||
};
|
||||
|
||||
export fn equal(a: str, b: str) bool = {
|
||||
if (a.len != b.len) { return false; };
|
||||
// compare — bytewise three-way comparison: negative if a<b, 0 if equal,
|
||||
// positive if a>b. Matches Hare's strings::compare. ASCII-order, not
|
||||
// locale-aware. Callers that just need equality use `compare(a, b) == 0`.
|
||||
export fn compare(a: str, b: str) i32 = {
|
||||
let n: i32 = a.len;
|
||||
if (b.len < n) { n = b.len; };
|
||||
let i: i32 = 0;
|
||||
for (i < a.len) {
|
||||
if (a[i] != b[i]) { return false; };
|
||||
for (i < n) {
|
||||
if (a[i] != b[i]) { return (a[i]: i32) - (b[i]: i32); };
|
||||
i += 1;
|
||||
};
|
||||
return true;
|
||||
return a.len - b.len;
|
||||
};
|
||||
|
||||
export fn hasprefix(s: str, p: str) bool = {
|
||||
@@ -41,10 +39,11 @@ export fn hassuffix(s: str, suf: str) bool = {
|
||||
return true;
|
||||
};
|
||||
|
||||
// indexbyte — first index of `c` in `s`, or -1 if absent. Plan 9-
|
||||
// style sentinel return; callers that prefer a fallible shape can
|
||||
// wrap this in their own (i32 | str). No allocation.
|
||||
export fn indexbyte(s: str, c: u8) i32 = {
|
||||
// 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 = {
|
||||
let i: i32 = 0;
|
||||
for (i < s.len) {
|
||||
if (s[i] == c) { return i; };
|
||||
@@ -53,6 +52,17 @@ export fn indexbyte(s: str, c: u8) i32 = {
|
||||
return -1;
|
||||
};
|
||||
|
||||
// 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 = {
|
||||
let i: i32 = s.len - 1;
|
||||
for (i >= 0) {
|
||||
if (s[i] == c) { return i; };
|
||||
i -= 1;
|
||||
};
|
||||
return -1;
|
||||
};
|
||||
|
||||
// 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.
|
||||
|
||||
Reference in New Issue
Block a user