From 197e1542cbfe0602f3b1d99270d45cac8c503d29 Mon Sep 17 00:00:00 2001 From: zongor Date: Fri, 10 Jul 2026 18:45:21 -0700 Subject: [PATCH] replace pointers with slices --- test/stdlib.ul | 74 ++++++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 45 deletions(-) diff --git a/test/stdlib.ul b/test/stdlib.ul index 5a2cfb1..f4a6737 100644 --- a/test/stdlib.ul +++ b/test/stdlib.ul @@ -5,57 +5,31 @@ const nat U16_MAX = 65535; byte[10000] heap; nat heap_allocated = 0; -function malloc(nat size) ref byte[] { +subroutine alloc(nat size) byte[] { nat pos = heap_allocated; heap_allocated = heap_allocated + size; - return ref heap[pos]; + return heap[pos:pos + size]; } -function free() { +subroutine heap_reset() { heap_allocated = 0; } -function memcpy(ref byte[] to, ref byte[] from, nat length) { - for (nat i = 0; i < length; i++) { - to[i] = from[i]; - } - return ptr; -} - -function mallocpy(ref byte[] from, nat length) ref byte[] { - ref byte[] ptr = malloc(length); - for (nat i = 0; i < length; i++) { - ptr[i] = from[i]; - } - return ptr; -} - - -/** - * Print with a newline - */ -function pln(str string) { - for (byte c in string) { - write = c; - } - write = '\n'; -} - /** * Concatinates 2 strings */ function strcat(str src1, str src2) str { - str result = malloc(src1.length + src2.length); - memcpy(ref result[0], ref src1[0]); - memcpy((ref result[0] + src1.length), ref src2[0]); + str result = alloc(src1.length + src2.length) as str; + result[0:src1.length] = src1; + result[src1.length:] = src2; return result; } /** * finds index of string, -1 if not found */ -function stridx(str haystack, char needle) int { - int i = -1; +function stridx(str haystack, char needle) nat { + nat i = -1; for (char c in haystack) { i = i + 1; if (c == needle) { @@ -70,7 +44,7 @@ function stridx(str haystack, char needle) int { */ function streq(str src1, str src2) bool { if (src1.length != src2.length) return false; - for (int i=0; src1.length; i++) { + for (nat i=0; src1.length; i++) { if (src1[i] != src2[i]) return false; } return true; @@ -79,21 +53,31 @@ function streq(str src1, str src2) bool { /** * Slice string */ -function slice(str src, int start, int stop) str { - int len = stop - start; - str result = malloc(len); - for (int i=start; i 0); - str result = malloc(5 - i); - memcpy(result, &buffer[0] + i, 5 - i); + str result = alloc(6 - i) as str; + result[0:] = buffer[i:6 - i]; return result; }