replace pointers with slices
This commit is contained in:
parent
8abe0ab545
commit
197e1542cb
|
|
@ -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<stop; i++) {
|
||||
function slice(str src, nat start, nat stop) str {
|
||||
nat len = stop - start;
|
||||
str result = alloc(len) as str;
|
||||
for (nat i=start; i<stop; i++) {
|
||||
result[i] = src[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print with a newline
|
||||
*/
|
||||
function pln(str string) {
|
||||
for (byte c in string) {
|
||||
write = c;
|
||||
}
|
||||
write = '\n';
|
||||
}
|
||||
|
||||
/**
|
||||
* int to string
|
||||
*/
|
||||
function itos(int src) str {
|
||||
byte buffer[6];
|
||||
nat i = 6;
|
||||
byte buffer[6];
|
||||
nat i = 6;
|
||||
bool neg = n < 0;
|
||||
|
||||
if (neg) n = -n;
|
||||
|
|
@ -105,8 +89,8 @@ function itos(int src) str {
|
|||
|
||||
if(neg) buffer[--i] = '-';
|
||||
|
||||
str result = malloc(6 - i);
|
||||
memcpy(result, &buffer[0] + i, 6 - i);
|
||||
str result = alloc(6 - i) as str;
|
||||
result[0:] = buffer[i:6 - i];
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -122,8 +106,8 @@ function ntos(nat src) str {
|
|||
n /= 10;
|
||||
} while(n > 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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue