builtin: new_array: cap should not be less than len
parent
673acdbd00
commit
722a2c71c3
|
@ -17,7 +17,7 @@ pub mut:
|
||||||
|
|
||||||
// Internal function, used by V (`nums := []int`)
|
// Internal function, used by V (`nums := []int`)
|
||||||
fn __new_array(mylen int, cap int, elm_size int) array {
|
fn __new_array(mylen int, cap int, elm_size int) array {
|
||||||
cap_ := if cap == 0 { 1 } else { cap }
|
cap_ := if cap < mylen { mylen } else { cap }
|
||||||
arr := array{
|
arr := array{
|
||||||
len: mylen
|
len: mylen
|
||||||
cap: cap_
|
cap: cap_
|
||||||
|
@ -29,11 +29,11 @@ fn __new_array(mylen int, cap int, elm_size int) array {
|
||||||
|
|
||||||
// Private function, used by V (`nums := [1, 2, 3]`)
|
// Private function, used by V (`nums := [1, 2, 3]`)
|
||||||
fn new_array_from_c_array(len, cap, elm_size int, c_array voidptr) array {
|
fn new_array_from_c_array(len, cap, elm_size int, c_array voidptr) array {
|
||||||
cap_ := if cap == 0 { 1 } else { cap }
|
cap_ := if cap < len { len } else { cap }
|
||||||
|
|
||||||
arr := array{
|
arr := array{
|
||||||
len: len
|
len: len
|
||||||
cap: cap
|
cap: cap_
|
||||||
element_size: elm_size
|
element_size: elm_size
|
||||||
data: vcalloc(cap_ * elm_size)
|
data: vcalloc(cap_ * elm_size)
|
||||||
}
|
}
|
||||||
|
|
|
@ -142,10 +142,13 @@ pub fn v_calloc(n int) byteptr {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn vcalloc(n int) byteptr {
|
pub fn vcalloc(n int) byteptr {
|
||||||
if n <= 0 {
|
if n < 0 {
|
||||||
panic('calloc(<=0)')
|
panic('calloc(<=0)')
|
||||||
|
} else if n == 0 {
|
||||||
|
return byteptr(0)
|
||||||
|
} else {
|
||||||
|
return C.calloc(n, 1)
|
||||||
}
|
}
|
||||||
return C.calloc(n, 1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[unsafe_fn]
|
[unsafe_fn]
|
||||||
|
|
Loading…
Reference in New Issue