Skip to content

Commit

Permalink
tweak with guard (moonbitlang#974)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobzhang authored Sep 11, 2024
1 parent fcc145d commit 0ac328a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
5 changes: 3 additions & 2 deletions builtin/arraycore.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ pub fn length[T](self : Array[T]) -> Int {

fn set_length[T](self : Array[T], new_len : Int) -> Unit {
// assert new_len >= 0 && new_len <= self.buf.len
if new_len < self.len {
for i = new_len; i < self.len; i = i + 1 {
let len = self.len
if new_len < len {
for i in new_len..<len {
self.buf.set_null(i)
}
}
Expand Down
15 changes: 4 additions & 11 deletions builtin/arrayview.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,14 @@ pub fn op_as_view[T](
~start : Int,
~end? : Int
) -> ArrayView[T] {
let len = self.length()
let end = match end {
None => self.length()
Some(end) =>
if end > self.length() {
abort("View end index out of bounds")
} else {
end
}
None => len
Some(end) => end
}
if start < 0 {
guard start >= 0 && start <= end && end <= len else {
abort("View start index out of bounds")
}
if start > end {
abort("View start index greater than end index")
}
ArrayView::{ buf: self.buffer(), start, len: end - start }
}

Expand Down

0 comments on commit 0ac328a

Please sign in to comment.