feat: stdlib daily APIs, macro tt/type paste, riscv64 cross smoke
ci / build (ubuntu) (push) Has been cancelled
ci / unit + fmt (push) Has been cancelled
ci / examples (push) Has been cancelled
ci / goldens + tools (push) Has been cancelled
ci / apps (push) Has been cancelled
ci / selfhost smoke (push) Has been cancelled
ci / macos smoke (push) Has been cancelled
ci / windows smoke (push) Has been cancelled
ci / CI gate (push) Has been cancelled
selfhost-loop / bootstrap determinism (push) Has been cancelled
ci / build (ubuntu) (push) Has been cancelled
ci / unit + fmt (push) Has been cancelled
ci / examples (push) Has been cancelled
ci / goldens + tools (push) Has been cancelled
ci / apps (push) Has been cancelled
ci / selfhost smoke (push) Has been cancelled
ci / macos smoke (push) Has been cancelled
ci / windows smoke (push) Has been cancelled
ci / CI gate (push) Has been cancelled
selfhost-loop / bootstrap determinism (push) Has been cancelled
Sessions 83–87: grow Array/Map/String/Test ergonomics; delimiter-balanced and juxta :tt macros plus $t:type fragments; expression-level $(…),* in templates; selfhost slice lits; riscv64/aarch64 cross smoke helper and freestanding docs. Null-safe CBE type names and String_StartsWith.
This commit is contained in:
@@ -140,4 +140,80 @@ module Std::Array {
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove element at `index`, shifting later elements left. Returns the removed value.
|
||||
func Array_RemoveAt<T>(self: *Array<T>, index: uint) -> T {
|
||||
bux_bounds_check(index, self.len);
|
||||
let val: T = self.data[index];
|
||||
var i: uint = index;
|
||||
while i + 1 < self.len {
|
||||
self.data[i] = self.data[i + 1];
|
||||
i = i + 1;
|
||||
}
|
||||
self.len = self.len - 1;
|
||||
return val;
|
||||
}
|
||||
|
||||
/// Insert `value` at `index` (`0..=len`), shifting later elements right.
|
||||
func Array_Insert<T>(self: *Array<T>, index: uint, value: T) {
|
||||
// allow index == len (append); panic if index > len
|
||||
bux_bounds_check(index, self.len + 1);
|
||||
if self.len >= self.cap {
|
||||
var newCap: uint = self.cap * 2;
|
||||
if newCap == 0 {
|
||||
newCap = 4;
|
||||
}
|
||||
self.cap = newCap;
|
||||
self.data = bux_realloc(self.data as *void, self.cap * sizeof(T)) as *T;
|
||||
}
|
||||
var i: uint = self.len;
|
||||
while i > index {
|
||||
self.data[i] = self.data[i - 1];
|
||||
i = i - 1;
|
||||
}
|
||||
self.data[index] = value;
|
||||
self.len = self.len + 1;
|
||||
}
|
||||
|
||||
/// O(1) remove: swap `index` with last, then pop. Does not preserve order.
|
||||
func Array_SwapRemove<T>(self: *Array<T>, index: uint) -> T {
|
||||
bux_bounds_check(index, self.len);
|
||||
let val: T = self.data[index];
|
||||
self.len = self.len - 1;
|
||||
if index < self.len {
|
||||
self.data[index] = self.data[self.len];
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
/// Shallow clone: new buffer, elements copied by value.
|
||||
func Array_Clone<T>(self: *Array<T>) -> Array<T> {
|
||||
var cap: uint = self.len;
|
||||
if cap == 0 {
|
||||
cap = 1;
|
||||
}
|
||||
var out: Array<T> = Array_New<T>(cap);
|
||||
var i: uint = 0;
|
||||
while i < self.len {
|
||||
Array_Push<T>(&out, self.data[i]);
|
||||
i = i + 1;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/// Reverse elements in place.
|
||||
func Array_Reverse<T>(self: *Array<T>) {
|
||||
if self.len < 2 {
|
||||
return;
|
||||
}
|
||||
var i: uint = 0;
|
||||
var j: uint = self.len - 1;
|
||||
while i < j {
|
||||
let tmp: T = self.data[i];
|
||||
self.data[i] = self.data[j];
|
||||
self.data[j] = tmp;
|
||||
i = i + 1;
|
||||
j = j - 1;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+16
@@ -63,6 +63,14 @@ module Std::Map {
|
||||
return zero;
|
||||
}
|
||||
|
||||
/// Like `Map_Get`, but returns `defaultVal` when the key is absent.
|
||||
func Map_GetOr<K, V>(m: *Map<K, V>, key: K, defaultVal: V) -> V {
|
||||
if Map_Has<K, V>(m, key) {
|
||||
return Map_Get<K, V>(m, key);
|
||||
}
|
||||
return defaultVal;
|
||||
}
|
||||
|
||||
func Map_Has<K, V>(m: *Map<K, V>, key: K) -> bool {
|
||||
var keyPtr: *K = &key;
|
||||
let hash: uint = bux_hash_bytes(keyPtr as *void, sizeof(K));
|
||||
@@ -186,6 +194,14 @@ module Std::Map {
|
||||
return zero;
|
||||
}
|
||||
|
||||
/// Like `StringMap_Get`, but returns `defaultVal` when the key is absent.
|
||||
func StringMap_GetOr<V>(m: *StringMap<V>, key: String, defaultVal: V) -> V {
|
||||
if StringMap_Has<V>(m, key) {
|
||||
return StringMap_Get<V>(m, key);
|
||||
}
|
||||
return defaultVal;
|
||||
}
|
||||
|
||||
func StringMap_Has<V>(m: *StringMap<V>, key: String) -> bool {
|
||||
let hash: uint = bux_hash_string(key);
|
||||
var idx: uint = hash % m.cap;
|
||||
|
||||
@@ -71,6 +71,8 @@ module Std::String {
|
||||
|
||||
/// True if `s` begins with `prefix`.
|
||||
func String_StartsWith(s: String, prefix: String) -> bool {
|
||||
if s == null as String { return false; }
|
||||
if prefix == null as String { return false; }
|
||||
let s_len: uint = bux_strlen(s);
|
||||
let p_len: uint = bux_strlen(prefix);
|
||||
if p_len > s_len {
|
||||
@@ -82,6 +84,8 @@ module Std::String {
|
||||
|
||||
/// True if `s` ends with `suffix`.
|
||||
func String_EndsWith(s: String, suffix: String) -> bool {
|
||||
if s == null as String { return false; }
|
||||
if suffix == null as String { return false; }
|
||||
let s_len: uint = bux_strlen(s);
|
||||
let suf_len: uint = bux_strlen(suffix);
|
||||
if suf_len > s_len {
|
||||
@@ -294,4 +298,56 @@ module Std::String {
|
||||
return bux_str_format(pattern, a0, a1, a2, "", "", "", "", "");
|
||||
}
|
||||
|
||||
/// Lexicographic compare: `<0` if a<b, `0` if equal, `>0` if a>b (`strcmp`).
|
||||
func String_Cmp(a: String, b: String) -> int {
|
||||
return bux_strcmp(a, b);
|
||||
}
|
||||
|
||||
/// Byte index of first `needle` in `s`, or `-1` if not found.
|
||||
func String_IndexOf(s: String, needle: String) -> int {
|
||||
let pos: String = bux_strstr(s, needle);
|
||||
if String_IsNull(pos) {
|
||||
return -1;
|
||||
}
|
||||
return String_Offset(pos, s) as int;
|
||||
}
|
||||
|
||||
/// ASCII lower → upper (`a`–`z` only). Non-ASCII bytes unchanged.
|
||||
func String_ToUpper(s: String) -> String {
|
||||
let n: uint = bux_strlen(s);
|
||||
var sb: StringBuilder = StringBuilder_NewCap(n + 1);
|
||||
var i: uint = 0;
|
||||
while i < n {
|
||||
let c: int = s[i] as int;
|
||||
if c >= 97 && c <= 122 {
|
||||
StringBuilder_AppendChar(&sb, (c - 32) as char8);
|
||||
} else {
|
||||
StringBuilder_AppendChar(&sb, c as char8);
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
let result: String = StringBuilder_Build(&sb);
|
||||
StringBuilder_Free(&sb);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// ASCII upper → lower (`A`–`Z` only). Non-ASCII bytes unchanged.
|
||||
func String_ToLower(s: String) -> String {
|
||||
let n: uint = bux_strlen(s);
|
||||
var sb: StringBuilder = StringBuilder_NewCap(n + 1);
|
||||
var i: uint = 0;
|
||||
while i < n {
|
||||
let c: int = s[i] as int;
|
||||
if c >= 65 && c <= 90 {
|
||||
StringBuilder_AppendChar(&sb, (c + 32) as char8);
|
||||
} else {
|
||||
StringBuilder_AppendChar(&sb, c as char8);
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
let result: String = StringBuilder_Build(&sb);
|
||||
StringBuilder_Free(&sb);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -46,6 +46,15 @@ module Std::Test {
|
||||
}
|
||||
}
|
||||
|
||||
/// Assert two strings differ.
|
||||
func Test_AssertNeqString(a: String, b: String) {
|
||||
if String_Eq(a, b) {
|
||||
PrintLine("ASSERT_NEQ_STRING FAILED: both are");
|
||||
PrintLine(b);
|
||||
bux_exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/// Assert two bools are equal.
|
||||
func Test_AssertEqBool(a: bool, b: bool) {
|
||||
if a != b {
|
||||
|
||||
Reference in New Issue
Block a user