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,9 +66,11 @@ fn new_array_from_c_array_no_alloc(len, cap, elm_size int, c_array voidptr) arra
|
|||||||
|
|
||||||
// Private function. Doubles array capacity if needed
|
// Private function. Doubles array capacity if needed
|
||||||
fn (a mut array) ensure_cap(required int) {
|
fn (a mut array) ensure_cap(required int) {
|
||||||
if required > a.cap {
|
if required <= a.cap {
|
||||||
|
return
|
||||||
|
}
|
||||||
mut cap := if a.cap == 0 { 2 } else { a.cap * 2 }
|
mut cap := if a.cap == 0 { 2 } else { a.cap * 2 }
|
||||||
for required > cap && true {
|
for required > cap {
|
||||||
cap *= 2
|
cap *= 2
|
||||||
}
|
}
|
||||||
if a.cap == 0 {
|
if a.cap == 0 {
|
||||||
@ -78,7 +80,6 @@ fn (a mut array) ensure_cap(required int) {
|
|||||||
a.data = C.realloc(a.data, cap * a.element_size)
|
a.data = C.realloc(a.data, cap * a.element_size)
|
||||||
}
|
}
|
||||||
a.cap = cap
|
a.cap = cap
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// array.repeat returns new array with the given array elements
|
// 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
|
// `val` is array.data
|
||||||
// TODO make private, right now it's used by strings.Builder
|
// TODO make private, right now it's used by strings.Builder
|
||||||
pub fn (a mut array) push_many(val voidptr, size int) {
|
pub fn (a mut array) push_many(val voidptr, size int) {
|
||||||
|
// 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)
|
a.ensure_cap(a.len + size)
|
||||||
C.memcpy(a.data + a.element_size * a.len, val, a.element_size * size)
|
C.memcpy(a.data + a.element_size * a.len, val, a.element_size * size)
|
||||||
|
}
|
||||||
a.len += size
|
a.len += size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user