From 29e3e7071218293c952afc8d7b74030b98287ef9 Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Thu, 29 Jul 2021 19:34:56 -0700 Subject: [PATCH] skip bounds checking in danger mode --- fixedseq.nim | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/fixedseq.nim b/fixedseq.nim index 4bdc88c..879c5e3 100644 --- a/fixedseq.nim +++ b/fixedseq.nim @@ -26,26 +26,30 @@ proc `$`*(s: FixedSeq): string = proc `[]`*(s: FixedSeq, i: Natural): FixedSeq.Contents = - if i > s.last: - raise newException(IndexDefect, "index " & $i & " is out of bounds.") + when not defined(danger): + if i > s.last: + raise newException(IndexDefect, "index " & $i & " is out of bounds.") s.data[i] proc `[]`*(s: var FixedSeq, i: Natural): var FixedSeq.Contents = - if i > s.last: - raise newException(IndexDefect, "index " & $i & " is out of bounds.") + when not defined(danger): + if i > s.last: + raise newException(IndexDefect, "index " & $i & " is out of bounds.") s.data[i] proc `[]`*(s: FixedSeq, i: BackwardsIndex): auto = - if s.last == -1: - raise newException(IndexDefect, "index out of bounds, the container is empty.") # matching stdlib again + when not defined(danger): + 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] proc `[]=`*(s: var FixedSeq, i: Natural, v: FixedSeq.Contents) = - if i > s.last: - raise newException(IndexDefect, "index " & $i & " is out of bounds.") + when not defined(danger): + if i > s.last: + raise newException(IndexDefect, "index " & $i & " is out of bounds.") 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) = - if idx > s.last: - raise newException(IndexDefect, "index " & $idx & " is out of bounds.") + when not defined(danger): + if idx > s.last: + raise newException(IndexDefect, "index " & $idx & " is out of bounds.") s.data[idx] = -1 dec s.last for i in typeof(s.last)(idx) .. s.last: