1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

array.clone()

This commit is contained in:
Alexander Medvednikov 2019-08-12 14:41:34 +03:00
parent 32754e3b4c
commit 15a42f1f35
2 changed files with 19 additions and 0 deletions

View File

@ -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 {

View File

@ -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]'
}