skip bounds checking in danger mode

This commit is contained in:
Joseph Montanaro 2021-07-29 19:34:56 -07:00
parent 7301597789
commit 29e3e70712

View File

@ -26,26 +26,30 @@ proc `$`*(s: FixedSeq): string =
proc `[]`*(s: FixedSeq, i: Natural): FixedSeq.Contents = proc `[]`*(s: FixedSeq, i: Natural): FixedSeq.Contents =
if i > s.last: when not defined(danger):
raise newException(IndexDefect, "index " & $i & " is out of bounds.") if i > s.last:
raise newException(IndexDefect, "index " & $i & " is out of bounds.")
s.data[i] s.data[i]
proc `[]`*(s: var FixedSeq, i: Natural): var FixedSeq.Contents = proc `[]`*(s: var FixedSeq, i: Natural): var FixedSeq.Contents =
if i > s.last: when not defined(danger):
raise newException(IndexDefect, "index " & $i & " is out of bounds.") if i > s.last:
raise newException(IndexDefect, "index " & $i & " is out of bounds.")
s.data[i] s.data[i]
proc `[]`*(s: FixedSeq, i: BackwardsIndex): auto = proc `[]`*(s: FixedSeq, i: BackwardsIndex): auto =
if s.last == -1: when not defined(danger):
raise newException(IndexDefect, "index out of bounds, the container is empty.") # matching stdlib again if s.last == -1:
raise newException(IndexDefect, "index out of bounds, the container is empty.") # matching stdlib again
s.data[s.last - typeof(s.last)(i) + 1] s.data[s.last - typeof(s.last)(i) + 1]
proc `[]=`*(s: var FixedSeq, i: Natural, v: FixedSeq.Contents) = proc `[]=`*(s: var FixedSeq, i: Natural, v: FixedSeq.Contents) =
if i > s.last: when not defined(danger):
raise newException(IndexDefect, "index " & $i & " is out of bounds.") if i > s.last:
raise newException(IndexDefect, "index " & $i & " is out of bounds.")
s.data[i] = v s.data[i] = v
@ -94,8 +98,9 @@ proc insert*(s: var FixedSeq, v: FixedSeq.Contents, idx: Natural = 0) =
proc delete*(s: var FixedSeq, idx: Natural) = proc delete*(s: var FixedSeq, idx: Natural) =
if idx > s.last: when not defined(danger):
raise newException(IndexDefect, "index " & $idx & " is out of bounds.") if idx > s.last:
raise newException(IndexDefect, "index " & $idx & " is out of bounds.")
s.data[idx] = -1 s.data[idx] = -1
dec s.last dec s.last
for i in typeof(s.last)(idx) .. s.last: for i in typeof(s.last)(idx) .. s.last: