undar-lang/test/str.ul

110 lines
1.7 KiB
Plaintext

/**
* Constants
*/
const str nl = "\n";
const str terminal_namespace = "/dev/term/0";
plex Terminal {
nat handle;
}
/**
* Print with a newline
*/
function pln(str string) {
Terminal term = open(terminal_namespace, 0);
write(term, string, string.length);
write(term, nl, nl.length);
// implied return because it is a void function.
}
/**
* Concatinates 2 strings
*/
function concat(str src1, str src2) str {
str result = malloc(src1.length + src2.length);
// note result[0].ref is much different than result.ref[0] !
// result[0].ref means the pointer at char 0 (pointer to first char)
memcpy(result[0].ref, src1.ref);
memcpy(result[src1.length].ref, src2.ref);
return result;
}
/**
* finds index of string, -1 if not found
*/
function str_index_of(str haystack, byte needle) int {
int i = -1;
for (byte c in haystack) {
if (c == needle) {
break;
}
}
return i;
}
/**
* checks if 2 strings are equal
*/
function str_eq(str src1, str src2) bool {
if (src1.length != src2.length) return false;
do (int i=0; src1.length; i++) {
if (src1[i] != src2[i]) return false;
}
return true;
}
/**
* Slice string
*/
function str_slice(str src, int start, int stop) str {
int len = stop - start;
str result = malloc(len);
do (int i=start; i<stop; i++) {
result[i] = src[i];
}
return result;
}
/**
* int to string
*/
function itos(int src) str {
}
/**
* nat to string
*/
function ntos(nat src) str {
}
/**
* real to string
*/
function rtos(real src) str {
}
/**
* string to int
*/
function stoi(str src) int {
}
/**
* string to nat
*/
function ston(str src) nat {
}
/**
* string to real
*/
function stor(str src) real {
}