mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
array.clone()
This commit is contained in:
parent
32754e3b4c
commit
15a42f1f35
@ -208,6 +208,17 @@ pub fn (a array) reverse() array {
|
||||
return arr
|
||||
}
|
||||
|
||||
pub fn (a array) clone() array {
|
||||
arr := array {
|
||||
len: a.len
|
||||
cap: a.cap
|
||||
element_size: a.element_size
|
||||
data: malloc(a.cap * a.element_size)
|
||||
}
|
||||
C.memcpy(arr.data, a.data, a.cap * a.element_size)
|
||||
return arr
|
||||
}
|
||||
|
||||
//pub fn (a []int) free() {
|
||||
pub fn (a array) free() {
|
||||
//if a.is_slice {
|
||||
|
@ -190,4 +190,12 @@ fn test_mut_slice() {
|
||||
assert n[2] == 777
|
||||
println(n)
|
||||
}
|
||||
|
||||
fn test_clone() {
|
||||
nums := [1, 2, 3, 4, 100]
|
||||
nums2 := nums.clone()
|
||||
assert nums2.len == 5
|
||||
assert nums2.str() == '[1, 2, 3, 4, 100]'
|
||||
assert nums.slice(1, 3).str() == '[2, 3]'
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user