Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 31 additions & 26 deletions lib/system/iterators.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ when not defined(nimNoLentIterators):
else:
template lent2(T): untyped = T

template unCheckedInc(x) =
{.push overflowChecks: off.}
inc(x)
{.pop.}

iterator items*[T: not char](a: openArray[T]): lent2 T {.inline.} =
## Iterates over each item of `a`.
var i = 0
while i < len(a):
yield a[i]
inc(i)
unCheckedInc(i)

iterator items*[T: char](a: openArray[T]): T {.inline.} =
## Iterates over each item of `a`.
Expand All @@ -23,14 +28,14 @@ iterator items*[T: char](a: openArray[T]): T {.inline.} =
var i = 0
while i < len(a):
yield a[i]
inc(i)
unCheckedInc(i)

iterator mitems*[T](a: var openArray[T]): var T {.inline.} =
## Iterates over each item of `a` so that you can modify the yielded value.
var i = 0
while i < len(a):
yield a[i]
inc(i)
unCheckedInc(i)

iterator items*[IX, T](a: array[IX, T]): T {.inline.} =
## Iterates over each item of `a`.
Expand All @@ -39,7 +44,7 @@ iterator items*[IX, T](a: array[IX, T]): T {.inline.} =
while true:
yield a[i]
if i >= high(IX): break
inc(i)
unCheckedInc(i)

iterator mitems*[IX, T](a: var array[IX, T]): var T {.inline.} =
## Iterates over each item of `a` so that you can modify the yielded value.
Expand All @@ -48,7 +53,7 @@ iterator mitems*[IX, T](a: var array[IX, T]): var T {.inline.} =
while true:
yield a[i]
if i >= high(IX): break
inc(i)
unCheckedInc(i)

iterator items*[T](a: set[T]): T {.inline.} =
## Iterates over each element of `a`. `items` iterates only over the
Expand All @@ -57,7 +62,7 @@ iterator items*[T](a: set[T]): T {.inline.} =
var i = low(T).int
while i <= high(T).int:
if T(i) in a: yield T(i)
inc(i)
unCheckedInc(i)

iterator items*(a: cstring): char {.inline.} =
## Iterates over each item of `a`.
Expand All @@ -76,7 +81,7 @@ iterator items*(a: cstring): char {.inline.} =
let n = len(a)
while i < n:
yield a[i]
inc(i)
unCheckedInc(i)
when defined(js): impl()
else:
when nimvm:
Expand All @@ -86,7 +91,7 @@ iterator items*(a: cstring): char {.inline.} =
var i = 0
while a[i] != '\0':
yield a[i]
inc(i)
unCheckedInc(i)

iterator mitems*(a: var cstring): var char {.inline.} =
## Iterates over each item of `a` so that you can modify the yielded value.
Expand All @@ -109,15 +114,15 @@ iterator mitems*(a: var cstring): var char {.inline.} =
let n = len(a)
while i < n:
yield a[i]
inc(i)
unCheckedInc(i)
when defined(js): impl()
else:
when nimvm: impl()
else:
var i = 0
while a[i] != '\0':
yield a[i]
inc(i)
unCheckedInc(i)

iterator items*[T: enum and Ordinal](E: typedesc[T]): T =
## Iterates over the values of `E`.
Expand All @@ -140,15 +145,15 @@ iterator pairs*[T](a: openArray[T]): tuple[key: int, val: T] {.inline.} =
var i = 0
while i < len(a):
yield (i, a[i])
inc(i)
unCheckedInc(i)

iterator mpairs*[T](a: var openArray[T]): tuple[key: int, val: var T]{.inline.} =
## Iterates over each item of `a`. Yields `(index, a[index])` pairs.
## `a[index]` can be modified.
var i = 0
while i < len(a):
yield (i, a[i])
inc(i)
unCheckedInc(i)

iterator pairs*[IX, T](a: array[IX, T]): tuple[key: IX, val: T] {.inline.} =
## Iterates over each item of `a`. Yields `(index, a[index])` pairs.
Expand All @@ -157,7 +162,7 @@ iterator pairs*[IX, T](a: array[IX, T]): tuple[key: IX, val: T] {.inline.} =
while true:
yield (i, a[i])
if i >= high(IX): break
inc(i)
unCheckedInc(i)

iterator mpairs*[IX, T](a: var array[IX, T]): tuple[key: IX, val: var T] {.inline.} =
## Iterates over each item of `a`. Yields `(index, a[index])` pairs.
Expand All @@ -167,15 +172,15 @@ iterator mpairs*[IX, T](a: var array[IX, T]): tuple[key: IX, val: var T] {.inlin
while true:
yield (i, a[i])
if i >= high(IX): break
inc(i)
unCheckedInc(i)

iterator pairs*[T](a: seq[T]): tuple[key: int, val: T] {.inline.} =
## Iterates over each item of `a`. Yields `(index, a[index])` pairs.
var i = 0
let L = len(a)
while i < L:
yield (i, a[i])
inc(i)
unCheckedInc(i)
assert(len(a) == L, "the length of the seq changed while iterating over it")

iterator mpairs*[T](a: var seq[T]): tuple[key: int, val: var T] {.inline.} =
Expand All @@ -185,7 +190,7 @@ iterator mpairs*[T](a: var seq[T]): tuple[key: int, val: var T] {.inline.} =
let L = len(a)
while i < L:
yield (i, a[i])
inc(i)
unCheckedInc(i)
assert(len(a) == L, "the length of the seq changed while iterating over it")

iterator pairs*(a: string): tuple[key: int, val: char] {.inline.} =
Expand All @@ -194,7 +199,7 @@ iterator pairs*(a: string): tuple[key: int, val: char] {.inline.} =
let L = len(a)
while i < L:
yield (i, a[i])
inc(i)
unCheckedInc(i)
assert(len(a) == L, "the length of the string changed while iterating over it")

iterator mpairs*(a: var string): tuple[key: int, val: var char] {.inline.} =
Expand All @@ -204,7 +209,7 @@ iterator mpairs*(a: var string): tuple[key: int, val: var char] {.inline.} =
let L = len(a)
while i < L:
yield (i, a[i])
inc(i)
unCheckedInc(i)
assert(len(a) == L, "the length of the string changed while iterating over it")

iterator pairs*(a: cstring): tuple[key: int, val: char] {.inline.} =
Expand All @@ -214,12 +219,12 @@ iterator pairs*(a: cstring): tuple[key: int, val: char] {.inline.} =
var L = len(a)
while i < L:
yield (i, a[i])
inc(i)
unCheckedInc(i)
else:
var i = 0
while a[i] != '\0':
yield (i, a[i])
inc(i)
unCheckedInc(i)

iterator mpairs*(a: var cstring): tuple[key: int, val: var char] {.inline.} =
## Iterates over each item of `a`. Yields `(index, a[index])` pairs.
Expand All @@ -229,20 +234,20 @@ iterator mpairs*(a: var cstring): tuple[key: int, val: var char] {.inline.} =
var L = len(a)
while i < L:
yield (i, a[i])
inc(i)
unCheckedInc(i)
else:
var i = 0
while a[i] != '\0':
yield (i, a[i])
inc(i)
unCheckedInc(i)

iterator items*[T](a: seq[T]): lent2 T {.inline.} =
## Iterates over each item of `a`.
var i = 0
let L = len(a)
while i < L:
yield a[i]
inc(i)
unCheckedInc(i)
assert(len(a) == L, "the length of the seq changed while iterating over it")

iterator mitems*[T](a: var seq[T]): var T {.inline.} =
Expand All @@ -251,7 +256,7 @@ iterator mitems*[T](a: var seq[T]): var T {.inline.} =
let L = len(a)
while i < L:
yield a[i]
inc(i)
unCheckedInc(i)
assert(len(a) == L, "the length of the seq changed while iterating over it")

iterator items*(a: string): char {.inline.} =
Expand All @@ -260,7 +265,7 @@ iterator items*(a: string): char {.inline.} =
let L = len(a)
while i < L:
yield a[i]
inc(i)
unCheckedInc(i)
assert(len(a) == L, "the length of the string changed while iterating over it")

iterator mitems*(a: var string): var char {.inline.} =
Expand All @@ -269,7 +274,7 @@ iterator mitems*(a: var string): var char {.inline.} =
let L = len(a)
while i < L:
yield a[i]
inc(i)
unCheckedInc(i)
assert(len(a) == L, "the length of the string changed while iterating over it")


Expand Down
4 changes: 4 additions & 0 deletions tests/arc/topt_no_cursor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ try:
`=copy`(lan_ip, splitted[1])
echo [lan_ip]
echo [splitted[1]]
{.push, overflowChecks: false.}
inc(i, 1)
{.pop.}
finally:
`=destroy`(splitted)
finally:
Expand All @@ -113,7 +115,9 @@ block :tmp:
addInterfaceDecl(c):
:tmpD = `=dup`(sym)
:tmpD
{.push, overflowChecks: false.}
inc(i, 1)
{.pop.}
`=destroy`(shadowScope)
-- end of expandArc ------------------------
--expandArc: check
Expand Down