array: add `array.trim()`
parent
6849a4e770
commit
a2838ae6f3
|
@ -152,6 +152,14 @@ pub fn (a mut array) clear() {
|
|||
a.len = 0
|
||||
}
|
||||
|
||||
// trims the array length to "index" without modifying the allocated data. If "index" is greater
|
||||
// than len nothing will be changed
|
||||
pub fn (a mut array) trim(index int) {
|
||||
if index < a.len {
|
||||
a.len = index
|
||||
}
|
||||
}
|
||||
|
||||
// Private function. Used to implement array[] operator
|
||||
fn (a array) get(i int) voidptr {
|
||||
$if !no_bounds_checking? {
|
||||
|
|
|
@ -592,3 +592,20 @@ fn test_clear() {
|
|||
arr.clear()
|
||||
assert arr.len == 0
|
||||
}
|
||||
|
||||
fn test_trim() {
|
||||
mut arr := [1,2,3,4,5,6,7,8,9]
|
||||
assert arr.len == 9
|
||||
|
||||
arr.trim(9)
|
||||
assert arr.len == 9
|
||||
assert arr.last() == 9
|
||||
|
||||
arr.trim(7)
|
||||
assert arr.len == 7
|
||||
assert arr.last() == 7
|
||||
|
||||
arr.trim(2)
|
||||
assert arr.len == 2
|
||||
assert arr.last() == 2
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue