From 08f5259d77e843fa62023eebdab29c09638bf529 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 24 Jul 2022 23:07:35 +0100 Subject: [PATCH] Replace `insert_at` with `inject_at` and `assign_at` --- core/runtime/core_builtin.odin | 52 +++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/core/runtime/core_builtin.odin b/core/runtime/core_builtin.odin index cebb91987..4f698a270 100644 --- a/core/runtime/core_builtin.odin +++ b/core/runtime/core_builtin.odin @@ -413,7 +413,7 @@ append_nothing :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) { @builtin -insert_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #caller_location) -> (ok: bool) #no_bounds_check { +inject_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #caller_location) -> (ok: bool) #no_bounds_check { if array == nil { return } @@ -432,7 +432,7 @@ insert_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #calle } @builtin -insert_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #caller_location) -> (ok: bool) #no_bounds_check { +inject_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #caller_location) -> (ok: bool) #no_bounds_check { if array == nil { return } @@ -456,7 +456,7 @@ insert_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #c } @builtin -insert_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string, loc := #caller_location) -> (ok: bool) #no_bounds_check { +inject_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string, loc := #caller_location) -> (ok: bool) #no_bounds_check { if array == nil { return } @@ -477,7 +477,51 @@ insert_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string return } -@builtin insert_at :: proc{insert_at_elem, insert_at_elems, insert_at_elem_string} +@builtin inject_at :: proc{inject_at_elem, inject_at_elems, inject_at_elem_string} + + + +@builtin +assign_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #caller_location) -> (ok: bool) #no_bounds_check { + if index < len(array) { + array[index] = arg + ok = true + } else if resize(array, index+1, loc) { + array[index] = arg + ok = true + } + return +} + + +@builtin +assign_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #caller_location) -> (ok: bool) #no_bounds_check { + if index+len(args) < len(array) { + copy(array[index:], args) + ok = true + } else if resize(array, index+1+len(args), loc) { + copy(array[index:], args) + ok = true + } + return +} + + +@builtin +assign_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string, loc := #caller_location) -> (ok: bool) #no_bounds_check { + if len(args) == 0 { + ok = true + } else if index+len(args) < len(array) { + copy(array[index:], args) + ok = true + } else if resize(array, index+1+len(args), loc) { + copy(array[index:], args) + ok = true + } + return +} + +@builtin assign_at :: proc{assign_at_elem, assign_at_elems, assign_at_elem_string}