Skip to content

Commit

Permalink
support negative slice index for arrayview and bytesview
Browse files Browse the repository at this point in the history
  • Loading branch information
Yu-zh authored and bobzhang committed Jan 11, 2025
1 parent 3892504 commit a1677e7
Show file tree
Hide file tree
Showing 5 changed files with 324 additions and 26 deletions.
10 changes: 6 additions & 4 deletions builtin/arrayview.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,11 @@ pub fn op_as_view[T](
let len = self.length()
let end = match end {
None => len
Some(end) => end
Some(end) => if end < 0 { len + end } else { end }
}
let start = if start < 0 { len + start } else { start }
guard start >= 0 && start <= end && end <= len else {
abort("View start index out of bounds")
abort("View index out of bounds")
}
ArrayView::{ buf: self.buffer(), start, len: end - start }
}
Expand Down Expand Up @@ -289,10 +290,11 @@ pub fn op_as_view[T](
let len = self.length()
let end = match end {
None => len
Some(end) => end
Some(end) => if end < 0 { len + end } else { end }
}
let start = if start < 0 { len + start } else { start }
guard start >= 0 && start <= end && end <= len else {
abort("View start index out of bounds")
abort("View index out of bounds")
}
ArrayView::{ buf: self.buf, start: self.start + start, len: end - start }
}
Expand Down
108 changes: 108 additions & 0 deletions builtin/arrayview_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,111 @@ test "arrayview_filter" {
assert_eq!(filtered.length(), 1)
assert_eq!(filtered[0], 4)
}

test "negative index1" {
let arr = [1, 2, 3]
let view = arr[-1:]
inspect!(view, content="[3]")
let view = arr[-2:]
inspect!(view, content="[2, 3]")
let view = arr[-3:]
inspect!(view, content="[1, 2, 3]")
let view = arr[:-1]
inspect!(view, content="[1, 2]")
let view = arr[:-2]
inspect!(view, content="[1]")
let view = arr[:-3]
inspect!(view, content="[]")
let view = arr[-3:-3]
inspect!(view, content="[]")
let view = arr[-3:-2]
inspect!(view, content="[1]")
let view = arr[-3:-1]
inspect!(view, content="[1, 2]")
let view = arr[-3:0]
inspect!(view, content="[]")
let view = arr[-3:1]
inspect!(view, content="[1]")
let view = arr[-3:2]
inspect!(view, content="[1, 2]")
let view = arr[-3:3]
inspect!(view, content="[1, 2, 3]")
}

test "negative index2" {
let arr = [0, 1, 2, 3, 4][1:4]
let view = arr[-1:]
inspect!(view, content="[3]")
let view = arr[-2:]
inspect!(view, content="[2, 3]")
let view = arr[-3:]
inspect!(view, content="[1, 2, 3]")
let view = arr[:-1]
inspect!(view, content="[1, 2]")
let view = arr[:-2]
inspect!(view, content="[1]")
let view = arr[:-3]
inspect!(view, content="[]")
let view = arr[-3:-3]
inspect!(view, content="[]")
let view = arr[-3:-2]
inspect!(view, content="[1]")
let view = arr[-3:-1]
inspect!(view, content="[1, 2]")
let view = arr[-3:0]
inspect!(view, content="[]")
let view = arr[-3:1]
inspect!(view, content="[1]")
let view = arr[-3:2]
inspect!(view, content="[1, 2]")
let view = arr[-3:3]
inspect!(view, content="[1, 2, 3]")
}

test "panic negative index1" {
let arr = [1, 2, 3]
let _ = arr[-4:]

}

test "panic negative index2" {
let arr = [1, 2, 3]
let _ = arr[:-4]

}

test "panic negative index3" {
let arr = [1, 2, 3]
let _ = arr[-1:-2]

}

test "panic negative index4" {
let arr = [1, 2, 3]
let _ = arr[-1:-3]

}

test "panic negative index5" {
let arr = [1, 2, 3][:]
let _ = arr[-4:]

}

test "panic negative index6" {
let arr = [1, 2, 3][:]
let _ = arr[:-4]

}

test "panic negative index7" {
let arr = [1, 2, 3][:]
let _ = arr[-1:-2]

}

test "panic negative index8" {
let arr = [1, 2, 3][:]
let _ = arr[-1:-3]

}
6 changes: 4 additions & 2 deletions builtin/bytesview.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ pub fn op_as_view(self : Bytes, start~ : Int = 0, end? : Int) -> BytesView {
let len = self.length()
let end = match end {
None => len
Some(end) => end
Some(end) => if end < 0 { len + end } else { end }
}
let start = if start < 0 { len + start } else { start }
guard start >= 0 && start <= end && end <= len else {
abort("Invalid index for BytesView")
}
Expand All @@ -94,8 +95,9 @@ pub fn op_as_view(self : BytesView, start~ : Int = 0, end? : Int) -> BytesView {
let len = self.length()
let end = match end {
None => len
Some(end) => end
Some(end) => if end < 0 { len + end } else { end }
}
let start = if start < 0 { len + start } else { start }
guard start >= 0 && start <= end && end <= len else {
abort("Invalid index for BytesView")
}
Expand Down
206 changes: 204 additions & 2 deletions builtin/bytesview_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ test "basic3" {

test "panic invalid index1" {
let bs = b"\x00\x01\x02\x03\x04\x05"
let _ = bs[-1:]
let _ = bs[-7:]

}

Expand All @@ -64,7 +64,7 @@ test "panic invalid index3" {

test "panic invalid index4" {
let bs = b"\x00\x01\x02\x03\x04\x05"[:]
let _ = bs[-1:]
let _ = bs[:-7]

}

Expand All @@ -88,3 +88,205 @@ test "iter" {
content="[b'\\x00', b'\\x01', b'\\x02', b'\\x03', b'\\x04', b'\\x05']",
)
}

test "negative index1" {
let bs = b"\x01\x02\x03"
let bv = bs[-1:]
inspect!(
bv,
content=
#|b"\x03"
,
)
let bv = bs[-2:]
inspect!(
bv,
content=
#|b"\x02\x03"
,
)
let bv = bs[-3:]
inspect!(
bv,
content=
#|b"\x01\x02\x03"
,
)
let bv = bs[:-1]
inspect!(
bv,
content=
#|b"\x01\x02"
,
)
let bv = bs[:-2]
inspect!(
bv,
content=
#|b"\x01"
,
)
let bv = bs[:-3]
inspect!(
bv,
content=
#|b""
,
)
let bv = bs[-3:-3]
inspect!(
bv,
content=
#|b""
,
)
let bv = bs[-3:-2]
inspect!(
bv,
content=
#|b"\x01"
,
)
let bv = bs[-3:-1]
inspect!(
bv,
content=
#|b"\x01\x02"
,
)
let bv = bs[-3:0]
inspect!(
bv,
content=
#|b""
,
)
let bv = bs[-3:1]
inspect!(
bv,
content=
#|b"\x01"
,
)
let bv = bs[-3:2]
inspect!(
bv,
content=
#|b"\x01\x02"
,
)
let bv = bs[-3:3]
inspect!(
bv,
content=
#|b"\x01\x02\x03"
,
)
}

test "negative index2" {
let bs = b"\x00\x01\x02\x03\x04"[1:4]
let bv = bs[-1:]
inspect!(
bv,
content=
#|b"\x03"
,
)
let bv = bs[-2:]
inspect!(
bv,
content=
#|b"\x02\x03"
,
)
let bv = bs[-3:]
inspect!(
bv,
content=
#|b"\x01\x02\x03"
,
)
let bv = bs[:-1]
inspect!(
bv,
content=
#|b"\x01\x02"
,
)
let bv = bs[:-2]
inspect!(
bv,
content=
#|b"\x01"
,
)
let bv = bs[:-3]
inspect!(
bv,
content=
#|b""
,
)
let bv = bs[-3:-3]
inspect!(
bv,
content=
#|b""
,
)
let bv = bs[-3:-2]
inspect!(
bv,
content=
#|b"\x01"
,
)
let bv = bs[-3:-1]
inspect!(
bv,
content=
#|b"\x01\x02"
,
)
let bv = bs[-3:0]
inspect!(
bv,
content=
#|b""
,
)
let bv = bs[-3:1]
inspect!(
bv,
content=
#|b"\x01"
,
)
let bv = bs[-3:2]
inspect!(
bv,
content=
#|b"\x01\x02"
,
)
let bv = bs[-3:3]
inspect!(
bv,
content=
#|b"\x01\x02\x03"
,
)
}

test "panic negative index1" {
let bs = b"\x01\x02\x03"
let _ = bs[-1:-2]

}

test "panic negative index2" {
let bs = b"\x01\x02\x03"[:]
let _ = bs[-1:-2]

}
Loading

0 comments on commit a1677e7

Please sign in to comment.