iterators check seqs/strings are not resized during iteration
This commit is contained in:
parent
2958adc47f
commit
b6d5f18c93
1 changed files with 18 additions and 14 deletions
|
|
@ -1457,20 +1457,6 @@ iterator items*[IX, T](a: array[IX, T]): T {.inline.} =
|
||||||
if i >= high(IX): break
|
if i >= high(IX): break
|
||||||
inc(i)
|
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.} =
|
iterator items*[T](a: set[T]): T {.inline.} =
|
||||||
## iterates over each element of `a`. `items` iterates only over the
|
## 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
|
## 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:
|
if not cond:
|
||||||
raiseAssert(astToStr(cond) & ' ' & msg)
|
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):
|
when not defined(nimhygiene):
|
||||||
{.pragma: inject.}
|
{.pragma: inject.}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue