array: slice_clone()

pull/3142/head
Alexander Medvednikov 2019-12-17 01:29:40 +03:00
parent 562f24336d
commit ea781a557f
1 changed files with 21 additions and 0 deletions

View File

@ -214,6 +214,27 @@ fn (a array) slice2(start, _end int, end_max bool) array {
// the `end` element of the original array with the length and capacity // the `end` element of the original array with the length and capacity
// set to the number of the elements in the slice. // set to the number of the elements in the slice.
fn (a array) slice(start, _end int) array { fn (a array) slice(start, _end int) array {
mut end := _end
if start > end {
panic('array.slice: invalid slice index ($start > $end)')
}
if end > a.len {
panic('array.slice: slice bounds out of range ($end >= $a.len)')
}
if start < 0 {
panic('array.slice: slice bounds out of range ($start < 0)')
}
l := end - start
res := array {
element_size: a.element_size
data: a.data + start * a.element_size
len: l
cap: l
}
return res
}
fn (a array) slice_clone(start, _end int) array {
mut end := _end mut end := _end
if start > end { if start > end {
panic('array.slice: invalid slice index ($start > $end)') panic('array.slice: invalid slice index ($start > $end)')