aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2022-03-15 19:44:05 +0100
committerGitHub <noreply@github.com>2022-03-15 19:44:05 +0100
commit5534c031b3aae616af442703a71e6aa6951452d7 (patch)
tree5678ec9687e0f174ffb3038b1ff5524095b37a4f
parent1d147ba99339afbef47bf689a0e0d7591a33c633 (diff)
parent19dc84e300386b3f3262c78371c085a005045566 (diff)
Merge pull request #1624 from Kelimion/insert_at_fix
[runtime] fix `insert_at` procedure group.
-rw-r--r--core/runtime/core_builtin.odin31
1 files changed, 17 insertions, 14 deletions
diff --git a/core/runtime/core_builtin.odin b/core/runtime/core_builtin.odin
index 3bafc0b1d..67b1bd37b 100644
--- a/core/runtime/core_builtin.odin
+++ b/core/runtime/core_builtin.odin
@@ -386,12 +386,13 @@ insert_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #calle
if array == nil {
return
}
- n := len(array)
+ n := max(len(array), index)
m :: 1
- resize(array, n+m, loc)
- if n+m <= len(array) {
+ new_size := n + m
+
+ if resize(array, new_size, loc) {
when size_of(E) != 0 {
- copy(array[index+m:], array[index:])
+ copy(array[index + m:], array[index:])
array[index] = arg
}
ok = true
@@ -409,12 +410,13 @@ insert_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #c
return
}
- n := len(array)
+ n := max(len(array), index)
m := len(args)
- resize(array, n+m, loc)
- if n+m <= len(array) {
+ new_size := n + m
+
+ if resize(array, new_size, loc) {
when size_of(E) != 0 {
- copy(array[index+m:], array[index:])
+ copy(array[index + m:], array[index:])
copy(array[index:], args)
}
ok = true
@@ -427,17 +429,18 @@ insert_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string
if array == nil {
return
}
- if len(args) == 0 {
+ if len(arg) == 0 {
ok = true
return
}
- n := len(array)
- m := len(args)
- resize(array, n+m, loc)
- if n+m <= len(array) {
+ n := max(len(array), index)
+ m := len(arg)
+ new_size := n + m
+
+ if resize(array, new_size, loc) {
copy(array[index+m:], array[index:])
- copy(array[index:], args)
+ copy(array[index:], arg)
ok = true
}
return