bugfix: allow the slice operators to return empty ranges

This commit is contained in:
Zahary Karadjov 2018-06-23 16:25:33 +03:00
parent cf118e240b
commit f6dae4edbc
3 changed files with 13 additions and 4 deletions

View File

@ -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

View File

@ -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)

View File

@ -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])