assert() -> doAssert()
This commit is contained in:
parent
9960d8eea9
commit
7c422ae027
|
@ -13,8 +13,8 @@ template `@`(s, idx: untyped): untyped =
|
||||||
(when idx is BackwardsIndex: s.len - int(idx) else: int(idx))
|
(when idx is BackwardsIndex: s.len - int(idx) else: int(idx))
|
||||||
|
|
||||||
proc bits*(a: MutByteRange, start, len: int): BitRange =
|
proc bits*(a: MutByteRange, start, len: int): BitRange =
|
||||||
assert start <= len
|
doAssert start <= len
|
||||||
assert len <= 8 * a.len
|
doAssert len <= 8 * a.len
|
||||||
result.data = a
|
result.data = a
|
||||||
result.start = start
|
result.start = start
|
||||||
result.mLen = len
|
result.mLen = len
|
||||||
|
@ -143,12 +143,12 @@ iterator pairs*(x: BitRange): (int, bool) =
|
||||||
for i, v in enumerateBits(x): yield (i, v)
|
for i, v in enumerateBits(x): yield (i, v)
|
||||||
|
|
||||||
proc `[]`*(x: BitRange, idx: int): bool {.inline.} =
|
proc `[]`*(x: BitRange, idx: int): bool {.inline.} =
|
||||||
assert idx < x.len
|
doAssert idx < x.len
|
||||||
let p = x.start + idx
|
let p = x.start + idx
|
||||||
result = getAbsoluteBit(x.data, p)
|
result = getAbsoluteBit(x.data, p)
|
||||||
|
|
||||||
proc sliceNormalized(x: BitRange, ibegin, iend: int): BitRange =
|
proc sliceNormalized(x: BitRange, ibegin, iend: int): BitRange =
|
||||||
assert ibegin >= 0 and
|
doAssert ibegin >= 0 and
|
||||||
ibegin < x.len and
|
ibegin < x.len and
|
||||||
iend < x.len and
|
iend < x.len and
|
||||||
iend + 1 >= ibegin # the +1 here allows the result to be
|
iend + 1 >= ibegin # the +1 here allows the result to be
|
||||||
|
@ -168,14 +168,14 @@ proc `==`*(a, b: BitRange): bool =
|
||||||
true
|
true
|
||||||
|
|
||||||
proc `[]=`*(r: var BitRange, idx: Natural, val: bool) {.inline.} =
|
proc `[]=`*(r: var BitRange, idx: Natural, val: bool) {.inline.} =
|
||||||
assert idx < r.len
|
doAssert idx < r.len
|
||||||
let absIdx = r.start + idx
|
let absIdx = r.start + idx
|
||||||
setAbsoluteBit(r.data, absIdx, val)
|
setAbsoluteBit(r.data, absIdx, val)
|
||||||
|
|
||||||
proc setAbsoluteBit(x: BitRange, absIdx: int, val: bool) {.inline.} =
|
proc setAbsoluteBit(x: BitRange, absIdx: int, val: bool) {.inline.} =
|
||||||
## Assumes the destination bit is already zeroed.
|
## Assumes the destination bit is already zeroed.
|
||||||
## Works with absolute positions similar to `getAbsoluteBit`
|
## Works with absolute positions similar to `getAbsoluteBit`
|
||||||
assert absIdx < x.len
|
doAssert absIdx < x.len
|
||||||
let
|
let
|
||||||
byteToWrite = absIdx shr 3 # the same as absIdx / 8
|
byteToWrite = absIdx shr 3 # the same as absIdx / 8
|
||||||
bitToWrite = (absIdx and 0b111)
|
bitToWrite = (absIdx and 0b111)
|
||||||
|
@ -184,7 +184,7 @@ proc setAbsoluteBit(x: BitRange, absIdx: int, val: bool) {.inline.} =
|
||||||
raiseBit x.data[byteToWrite], bitToWrite
|
raiseBit x.data[byteToWrite], bitToWrite
|
||||||
|
|
||||||
proc pushFront*(x: var BitRange, val: bool) =
|
proc pushFront*(x: var BitRange, val: bool) =
|
||||||
assert x.start > 0
|
doAssert x.start > 0
|
||||||
dec x.start
|
dec x.start
|
||||||
x[0] = val
|
x[0] = val
|
||||||
inc x.mLen
|
inc x.mLen
|
||||||
|
@ -193,9 +193,9 @@ template neededBytes(nBits: int): int =
|
||||||
(nBits shr 3) + ord((nBits and 0b111) != 0)
|
(nBits shr 3) + ord((nBits and 0b111) != 0)
|
||||||
|
|
||||||
static:
|
static:
|
||||||
assert neededBytes(2) == 1
|
doAssert neededBytes(2) == 1
|
||||||
assert neededBytes(8) == 1
|
doAssert neededBytes(8) == 1
|
||||||
assert neededBytes(9) == 2
|
doAssert neededBytes(9) == 2
|
||||||
|
|
||||||
proc `&`*(a, b: BitRange): BitRange =
|
proc `&`*(a, b: BitRange): BitRange =
|
||||||
let totalLen = a.len + b.len
|
let totalLen = a.len + b.len
|
||||||
|
@ -212,7 +212,7 @@ proc `$`*(r: BitRange): string =
|
||||||
result.add(if b: '1' else: '0')
|
result.add(if b: '1' else: '0')
|
||||||
|
|
||||||
proc fromBits*(T: typedesc, r: BitRange, offset, num: Natural): T =
|
proc fromBits*(T: typedesc, r: BitRange, offset, num: Natural): T =
|
||||||
assert(num <= sizeof(T) * 8)
|
doAssert(num <= sizeof(T) * 8)
|
||||||
# XXX: Nim has a bug that a typedesc parameter cannot be used
|
# XXX: Nim has a bug that a typedesc parameter cannot be used
|
||||||
# in a type coercion, so we must define an alias here:
|
# in a type coercion, so we must define an alias here:
|
||||||
type TT = T
|
type TT = T
|
||||||
|
@ -225,6 +225,6 @@ proc parse*(T: typedesc[BitRange], s: string): BitRange =
|
||||||
case c
|
case c
|
||||||
of '0': discard
|
of '0': discard
|
||||||
of '1': raiseBit(bytes[i shr 3], i and 0b111)
|
of '1': raiseBit(bytes[i shr 3], i and 0b111)
|
||||||
else: assert false
|
else: doAssert false
|
||||||
result = bits(bytes, 0, s.len)
|
result = bits(bytes, 0, s.len)
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ proc toMemRange*(x: string): MemRange =
|
||||||
result.size = x.len
|
result.size = x.len
|
||||||
|
|
||||||
proc toMemRange*[T](x: openarray[T], fromIdx, toIdx: int): MemRange =
|
proc toMemRange*[T](x: openarray[T], fromIdx, toIdx: int): MemRange =
|
||||||
assert(fromIdx >= 0 and toIdx >= fromIdx and fromIdx < x.len and toIdx < x.len)
|
doAssert(fromIdx >= 0 and toIdx >= fromIdx and fromIdx < x.len and toIdx < x.len)
|
||||||
result.start = unsafeAddr x[fromIdx]
|
result.start = unsafeAddr x[fromIdx]
|
||||||
result.size = (toIdx - fromIdx + 1) * T.sizeof
|
result.size = (toIdx - fromIdx + 1) * T.sizeof
|
||||||
|
|
||||||
|
|
|
@ -74,14 +74,14 @@ proc high*(r: Range): int {.inline.} = r.len - 1
|
||||||
proc low*(r: Range): int {.inline.} = 0
|
proc low*(r: Range): int {.inline.} = 0
|
||||||
|
|
||||||
proc elemAt[T](r: MutRange[T], idx: int): var T {.inline.} =
|
proc elemAt[T](r: MutRange[T], idx: int): var T {.inline.} =
|
||||||
assert(idx < r.len)
|
doAssert(idx < r.len)
|
||||||
Range[T](r).start.shift(idx)[]
|
Range[T](r).start.shift(idx)[]
|
||||||
|
|
||||||
proc `[]=`*[T](r: MutRange[T], idx: int, v: T) {.inline.} = r.elemAt(idx) = v
|
proc `[]=`*[T](r: MutRange[T], idx: int, v: T) {.inline.} = r.elemAt(idx) = v
|
||||||
proc `[]`*[T](r: MutRange[T], i: int): var T = r.elemAt(i)
|
proc `[]`*[T](r: MutRange[T], i: int): var T = r.elemAt(i)
|
||||||
|
|
||||||
proc `[]`*[T](r: Range[T], idx: int): T {.inline.} =
|
proc `[]`*[T](r: Range[T], idx: int): T {.inline.} =
|
||||||
assert(idx < r.len)
|
doAssert(idx < r.len)
|
||||||
r.start.shift(idx)[]
|
r.start.shift(idx)[]
|
||||||
|
|
||||||
proc `==`*[T](a, b: Range[T]): bool =
|
proc `==`*[T](a, b: Range[T]): bool =
|
||||||
|
@ -122,7 +122,7 @@ 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
|
doAssert ibegin >= 0 and
|
||||||
ibegin < r.len and
|
ibegin < r.len and
|
||||||
iend < r.len and
|
iend < r.len and
|
||||||
iend + 1 >= ibegin # the +1 here allows the result to be
|
iend + 1 >= ibegin # the +1 here allows the result to be
|
||||||
|
|
Loading…
Reference in New Issue