mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-06 11:54:11 +00:00
iterators check seqs/strings are not resized during iteration
This commit is contained in:
@@ -1457,20 +1457,6 @@ iterator items*[IX, T](a: array[IX, T]): T {.inline.} =
|
||||
if i >= high(IX): break
|
||||
inc(i)
|
||||
|
||||
iterator items*[T](a: seq[T]): T {.inline.} =
|
||||
## iterates over each item of `a`.
|
||||
var i = 0
|
||||
while i < len(a):
|
||||
yield a[i]
|
||||
inc(i)
|
||||
|
||||
iterator items*(a: string): char {.inline.} =
|
||||
## iterates over each item of `a`.
|
||||
var i = 0
|
||||
while i < len(a):
|
||||
yield a[i]
|
||||
inc(i)
|
||||
|
||||
iterator items*[T](a: set[T]): T {.inline.} =
|
||||
## iterates over each element of `a`. `items` iterates only over the
|
||||
## elements that are really in the set (and not over the ones the set is
|
||||
@@ -2687,6 +2673,24 @@ template doAssert*(cond: bool, msg = "") =
|
||||
if not cond:
|
||||
raiseAssert(astToStr(cond) & ' ' & msg)
|
||||
|
||||
iterator items*[T](a: seq[T]): T {.inline.} =
|
||||
## iterates over each item of `a`.
|
||||
var i = 0
|
||||
let L = len(a)
|
||||
while i < L:
|
||||
yield a[i]
|
||||
inc(i)
|
||||
assert(len(a) == L, "seq modified while iterating over it")
|
||||
|
||||
iterator items*(a: string): char {.inline.} =
|
||||
## iterates over each item of `a`.
|
||||
var i = 0
|
||||
let L = len(a)
|
||||
while i < L:
|
||||
yield a[i]
|
||||
inc(i)
|
||||
assert(len(a) == L, "string modified while iterating over it")
|
||||
|
||||
when not defined(nimhygiene):
|
||||
{.pragma: inject.}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user