bugfix: allow the slice operators to return empty ranges
This commit is contained in:
parent
cf118e240b
commit
f6dae4edbc
|
@ -137,8 +137,9 @@ proc `[]`*(x: BitRange, idx: int): bool {.inline.} =
|
||||||
proc sliceNormalized(x: BitRange, ibegin, iend: int): BitRange =
|
proc sliceNormalized(x: BitRange, ibegin, iend: int): BitRange =
|
||||||
assert ibegin >= 0 and
|
assert ibegin >= 0 and
|
||||||
ibegin < x.len and
|
ibegin < x.len and
|
||||||
iend >= ibegin and
|
iend < x.len and
|
||||||
iend < x.len
|
iend + 1 >= ibegin # the +1 here allows the result to be
|
||||||
|
# an empty range
|
||||||
|
|
||||||
result.data = x.data
|
result.data = x.data
|
||||||
result.start = x.start + ibegin
|
result.start = x.start + ibegin
|
||||||
|
|
|
@ -113,7 +113,12 @@ proc `$`*(r: Range): string =
|
||||||
result &= "]"
|
result &= "]"
|
||||||
|
|
||||||
proc sliceNormalized[T](r: Range[T], ibegin, iend: int): Range[T] =
|
proc sliceNormalized[T](r: Range[T], ibegin, iend: int): Range[T] =
|
||||||
assert(ibegin >= 0 and ibegin < r.len and iend >= ibegin and iend < r.len)
|
assert ibegin >= 0 and
|
||||||
|
ibegin < r.len and
|
||||||
|
iend < r.len and
|
||||||
|
iend + 1 >= ibegin # the +1 here allows the result to be
|
||||||
|
# an empty range
|
||||||
|
|
||||||
when rangesGCHoldEnabled:
|
when rangesGCHoldEnabled:
|
||||||
result.gcHold = r.gcHold
|
result.gcHold = r.gcHold
|
||||||
result.start = r.start.shift(ibegin)
|
result.start = r.start.shift(ibegin)
|
||||||
|
|
|
@ -19,6 +19,9 @@ suite "Typed ranges":
|
||||||
let b = toRange(@[1, 2, 3])
|
let b = toRange(@[1, 2, 3])
|
||||||
a[1 .. 3] = b
|
a[1 .. 3] = b
|
||||||
check a.toSeq == @[0, 1, 2, 3, 0]
|
check a.toSeq == @[0, 1, 2, 3, 0]
|
||||||
|
check:
|
||||||
|
a[2 .. 2].len == 1
|
||||||
|
a[1 ..< 1].len == 0
|
||||||
|
|
||||||
test "equality operator":
|
test "equality operator":
|
||||||
var x = toRange(@[0, 1, 2, 3, 4, 5])
|
var x = toRange(@[0, 1, 2, 3, 4, 5])
|
||||||
|
|
Loading…
Reference in New Issue