diff --git a/vlib/builtin/array.v b/vlib/builtin/array.v index d920f7b454..d892a9ca5a 100644 --- a/vlib/builtin/array.v +++ b/vlib/builtin/array.v @@ -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 // set to the number of the elements in the slice. 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 if start > end { panic('array.slice: invalid slice index ($start > $end)')