From: Brendan Hansen Date: Tue, 7 Mar 2023 20:13:13 +0000 (-0600) Subject: added: `dyn_str` type ([..] u8) X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=6032f62edaf81c9adf96cd4528f4e64f8ad62f68;p=onyx.git added: `dyn_str` type ([..] u8) --- diff --git a/core/builtin.onyx b/core/builtin.onyx index d809e2b8..af99c4ca 100644 --- a/core/builtin.onyx +++ b/core/builtin.onyx @@ -26,8 +26,9 @@ package builtin // The builtin string and C-string types. // A string is simply a slice of bytes, and a c-string is a pointer // to byte, with a null-terminator ('\0') at the end. -str :: #type [] u8; -cstr :: #type & u8; +str :: #type [] u8; +cstr :: #type & u8; +dyn_str :: #type [..] u8; diff --git a/core/container/array.onyx b/core/container/array.onyx index 0a55bfaf..9a6cb4ba 100644 --- a/core/container/array.onyx +++ b/core/container/array.onyx @@ -116,6 +116,9 @@ push :: (arr: &[..] $T, x: T) -> bool { // Semi-useful shortcut for adding something to an array. #operator << macro (arr: [..] $T, v: T) do core.array.push(&arr, v); +insert :: #match #local {} + +#overload insert :: (arr: &[..] $T, idx: u32, x: T) -> bool { if !ensure_capacity(arr, arr.count + 1) do return false; @@ -129,6 +132,22 @@ insert :: (arr: &[..] $T, idx: u32, x: T) -> bool { return true; } +#overload +insert :: (arr: &[..] $T, idx: u32, new_arr: [] T) -> bool { + if !ensure_capacity(arr, arr.count + new_arr.count) do return false; + + arr.count += new_arr.count; + while i := arr.count; i > idx { + arr.data[i] = arr.data[i - 1]; + i -= 1; + } + + for i: 0 .. new_arr.count { + arr.data[i + idx] = new_arr[i]; + } + return true; +} + insert_empty :: (arr: &[..] $T, idx: u32) -> bool { if !ensure_capacity(arr, arr.count + 1) do return false; @@ -176,10 +195,11 @@ fast_delete :: (arr: &[..] $T, idx: u32) -> T { return to_return; } -pop :: (arr: &[..] $T) -> T { +pop :: (arr: &[..] $T, n := 1) -> T { if arr.count == 0 do return .{}; - arr.count -= 1; + c := core.math.min(n, arr.count); + arr.count -= n; return arr.data[arr.count]; } diff --git a/core/string/string.onyx b/core/string/string.onyx index 0b1b465b..425d1c5c 100644 --- a/core/string/string.onyx +++ b/core/string/string.onyx @@ -644,3 +644,9 @@ bisect :: (s: str, substr: str) -> (str, str) { return s[0 .. index], s[index+substr.length .. s.length]; } +// Used by dyn_str +delete :: core.array.delete +append :: core.array.concat +clear :: core.array.clear +retreat :: core.array.pop +insert :: core.array.insert