mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
array: fix arr << arr bug
This commit is contained in:
parent
6e9822929f
commit
cf1fd6e950
@ -66,19 +66,20 @@ fn new_array_from_c_array_no_alloc(len, cap, elm_size int, c_array voidptr) arra
|
||||
|
||||
// Private function. Doubles array capacity if needed
|
||||
fn (a mut array) ensure_cap(required int) {
|
||||
if required > a.cap {
|
||||
mut cap := if a.cap == 0 { 2 } else { a.cap * 2 }
|
||||
for required > cap && true {
|
||||
cap *= 2
|
||||
}
|
||||
if a.cap == 0 {
|
||||
a.data = calloc(cap * a.element_size)
|
||||
}
|
||||
else {
|
||||
a.data = C.realloc(a.data, cap * a.element_size)
|
||||
}
|
||||
a.cap = cap
|
||||
if required <= a.cap {
|
||||
return
|
||||
}
|
||||
mut cap := if a.cap == 0 { 2 } else { a.cap * 2 }
|
||||
for required > cap {
|
||||
cap *= 2
|
||||
}
|
||||
if a.cap == 0 {
|
||||
a.data = calloc(cap * a.element_size)
|
||||
}
|
||||
else {
|
||||
a.data = C.realloc(a.data, cap * a.element_size)
|
||||
}
|
||||
a.cap = cap
|
||||
}
|
||||
|
||||
// array.repeat returns new array with the given array elements
|
||||
@ -280,8 +281,16 @@ fn (a mut array) push(val voidptr) {
|
||||
// `val` is array.data
|
||||
// TODO make private, right now it's used by strings.Builder
|
||||
pub fn (a mut array) push_many(val voidptr, size int) {
|
||||
a.ensure_cap(a.len + size)
|
||||
C.memcpy(a.data + a.element_size * a.len, val, a.element_size * size)
|
||||
// handle `arr << arr`
|
||||
if a.data == val {
|
||||
copy := a.clone()
|
||||
a.ensure_cap(a.len + size)
|
||||
//C.memcpy(a.data, copy.data, copy.element_size * copy.len)
|
||||
C.memcpy(a.data + a.element_size * a.len, copy.data, a.element_size * size)
|
||||
} else {
|
||||
a.ensure_cap(a.len + size)
|
||||
C.memcpy(a.data + a.element_size * a.len, val, a.element_size * size)
|
||||
}
|
||||
a.len += size
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user