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

builtin: implement array.pop()

This commit is contained in:
Delyan Angelov
2020-07-14 19:55:44 +03:00
parent de0b96f52c
commit cf7d03bda6
4 changed files with 50 additions and 4 deletions

View File

@ -256,6 +256,22 @@ pub fn (a array) last() voidptr {
}
}
// array.pop returns the last element of the array, and removes it
pub fn (mut a array) pop() voidptr {
// in a sense, this is the opposite of `a << x`
$if !no_bounds_checking? {
if a.len == 0 {
panic('array.pop: array is empty')
}
}
new_len := a.len - 1
last_elem := unsafe { byteptr(a.data) + (new_len) * a.element_size }
a.len = new_len
// NB: a.cap is not changed here *on purpose*, so that
// further << ops on that array will be more efficient.
return memdup(last_elem, a.element_size)
}
// array.slice returns an array using the same buffer as original array
// but starting from the `start` element and ending with the element before
// the `end` element of the original array with the length and capacity