Skip to content

Commit

Permalink
Add aux of_cstr function to string module
Browse files Browse the repository at this point in the history
  • Loading branch information
tjammer committed Oct 20, 2024
1 parent 6da8a97 commit a0d9f41
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions std/string.smu
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a0d9f41

Please sign in to comment.