array: fix multiple array.repeat index error

pull/5362/head
yuyi 2020-06-12 17:42:26 +08:00 committed by GitHub
parent 827a9e221f
commit b4ad174b7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -119,7 +119,14 @@ pub fn (a array) repeat(count int) array {
cap: count * a.len
}
for i in 0..count {
C.memcpy(byteptr(arr.data) + i * a.len * a.element_size, byteptr(a.data), a.len * a.element_size)
if a.len > 0 && a.element_size == sizeof(array) {
ary := array{}
C.memcpy(&ary, a.data, sizeof(array))
ary_clone := ary.clone()
C.memcpy(byteptr(arr.data) + i * a.len * a.element_size, &ary_clone, a.len * a.element_size)
} else {
C.memcpy(byteptr(arr.data) + i * a.len * a.element_size, byteptr(a.data), a.len * a.element_size)
}
}
return arr
}

View File

@ -821,4 +821,8 @@ fn test_mutli_array_index() {
mut a := [][]int{len:2, init: []int{len:3, init:0}}
a[0][0] = 1
assert '$a' == '[[1, 0, 0], [0, 0, 0]]'
mut b := [[0].repeat(3)].repeat(2)
b[0][0] = 1
assert '$b' == '[[1, 0, 0], [0, 0, 0]]'
}