diff --git a/std/string.smu b/std/string.smu index 6a189ed..53e9f27 100644 --- a/std/string.smu +++ b/std/string.smu @@ -20,6 +20,7 @@ signature { val of_array : (array[u8]!) -> t val to_array : (t!) -> array[u8] val of_fixed_array : (array#?[u8]) -> t + val of_cstr : (cstr) -> t val println : (t) -> unit val print : (t) -> unit @@ -146,6 +147,18 @@ fun of_fixed_array(arr : array#?[u8]) { ret } +external strlen : (raw_ptr[u8]) -> int + +fun of_cstr(cstr) { + -- NOTE we could implement this without traversing the string twice + let size = strlen(cstr) + let arr& = array/create(size + 1) + -- copy null terminator + memcpy(array/data(arr), cstr, size + 1) + &__unsafe_array_length(arr) = size + arr +} + type file = raw_ptr[u8] external stdout : file external fwrite : (cstr, int, int, file) -> unit