Skip to content

Commit

Permalink
Grow std lib
Browse files Browse the repository at this point in the history
  • Loading branch information
tjammer committed Oct 10, 2024
1 parent 9b49b34 commit 9e43871
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
7 changes: 7 additions & 0 deletions std/option.smu
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ fun iter(opt, f) {
#none: ()
}
}

fun bind(opt, f) {
match opt {
#some(thing): f(thing)
#none: #none
}
}
19 changes: 17 additions & 2 deletions std/string.smu
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ signature {
val equal: (t, t) -> bool
val append : (t&, t) -> unit
val concat : (t, array[t]) -> t
val substr : (t, int, int) -> t
val boyer_moore_horspool : (t, t) -> option/t[int]

val modify_buf : (t&, (array[u8]&) -> unit) -> unit
Expand Down Expand Up @@ -72,6 +73,22 @@ fun concat(delim, strings) {
ret
}

external memcpy : (raw_ptr[u8], raw_ptr[u8], int) -> unit

fun substr(str : t, start, end) {
assert(start >= 0)
assert(end < len(str))

let size = end - start
assert(size >= 0)

let arr& = array/create(size + 1)
memcpy(array/data(arr), __unsafe_ptr_at(data(str), start), size)
&arr.[size] = '\000'
&__unsafe_array_length(arr) = size
arr
}

fun make_skips(pattern) {
let len = array/length(pattern)
let skips& = #256[len]
Expand Down Expand Up @@ -123,8 +140,6 @@ fun modify_buf(str& : t, f) {
fun of_array(arr! : array[u8]) { arr }
fun to_array(str! : t) { str }

external memcpy : (raw_ptr[u8], raw_ptr[u8], int) -> unit

fun of_fixed_array(arr : array#?[u8]) {
let ret = array/create(__fixed_array_length(arr))
memcpy(array/data(ret), __fixed_array_data(arr), __fixed_array_length(arr))
Expand Down

0 comments on commit 9e43871

Please sign in to comment.