mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
fix
This commit is contained in:
parent
251e32948e
commit
09fb000e67
@ -189,6 +189,19 @@ pub fn (arr mut array) _push_many(val voidptr, size int) {
|
||||
arr.len += size
|
||||
}
|
||||
|
||||
pub fn (a array) reverse() array {
|
||||
arr := array {
|
||||
len: a.len
|
||||
cap: a.cap
|
||||
element_size: a.element_size
|
||||
data: malloc(a.cap * a.element_size)
|
||||
}
|
||||
for i := 0; i < a.len; i++ {
|
||||
C.memcpy(arr.data + i * arr.element_size, &a[a.len-1-i], arr.element_size)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
pub fn (a []int) str() string {
|
||||
mut res := '['
|
||||
for i := 0; i < a.len; i++ {
|
||||
|
@ -148,4 +148,10 @@ fn test_push_many() {
|
||||
assert a[5] == 6
|
||||
}
|
||||
|
||||
fn test_reverse() {
|
||||
mut a := [1, 2, 3, 4]
|
||||
mut b := ['test', 'array', 'reverse']
|
||||
|
||||
assert a.reverse() == [4, 3, 2, 1]
|
||||
assert b.reverse() == ['reverse', 'array', 'test']
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user