From: Brendan Hansen Date: Tue, 7 Mar 2023 20:41:38 +0000 (-0600) Subject: bugfix: `dyn_str` functions X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=e345dfd26a6f17657b624dc36672213af4a7a889;p=onyx.git bugfix: `dyn_str` functions --- diff --git a/core/container/array.onyx b/core/container/array.onyx index 9a6cb4ba..cfe352e6 100644 --- a/core/container/array.onyx +++ b/core/container/array.onyx @@ -120,6 +120,7 @@ insert :: #match #local {} #overload insert :: (arr: &[..] $T, idx: u32, x: T) -> bool { + if idx >= arr.count do return false; if !ensure_capacity(arr, arr.count + 1) do return false; arr.count += 1; @@ -134,6 +135,7 @@ insert :: (arr: &[..] $T, idx: u32, x: T) -> bool { #overload insert :: (arr: &[..] $T, idx: u32, new_arr: [] T) -> bool { + if idx >= arr.count do return false; if !ensure_capacity(arr, arr.count + new_arr.count) do return false; arr.count += new_arr.count; diff --git a/core/string/string.onyx b/core/string/string.onyx index 425d1c5c..4295c327 100644 --- a/core/string/string.onyx +++ b/core/string/string.onyx @@ -644,9 +644,32 @@ 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 +// + +delete :: macro (x: &dyn_str, idx: u32) -> u8 { + return (package core.array).delete(x, idx); +} + +append :: macro (x: &dyn_str, other: str) { + (package core.array).concat(x, other); +} + +clear :: macro (x: &dyn_str) { + (package core.array).clear(x); +} + +retreat :: macro (x: &dyn_str, chars := 1) { + (package core.array).pop(x, chars); +} + +insert :: #match #locked { + macro (x: &dyn_str, idx: u32, new_str: str) -> bool { + return (package core.array).insert(x, idx, new_str); + }, + + macro (x: &dyn_str, idx: u32, ch: u8) -> bool { + return (package core.array).insert(x, idx, ch); + } +}