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

array: reverse empty arrays correctly

This commit is contained in:
Alexey 2020-02-29 22:44:02 +03:00 committed by GitHub
parent c1e095e587
commit cbffbf3438
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 0 deletions

View File

@ -328,6 +328,9 @@ pub fn (a mut array) push_many(val voidptr, size int) {
// array.reverse returns a new array with the elements of // array.reverse returns a new array with the elements of
// the original array in reverse order. // the original array in reverse order.
pub fn (a array) reverse() array { pub fn (a array) reverse() array {
if a.len < 2 {
return a
}
arr := array{ arr := array{
len: a.len len: a.len
cap: a.cap cap: a.cap

View File

@ -266,6 +266,10 @@ fn test_reverse() {
for i, _ in d { for i, _ in d {
assert d[i] == b[b.len - i - 1] assert d[i] == b[b.len - i - 1]
} }
e := []int
f := e.reverse()
assert f.len == 0
} }
const ( const (