diff options
author | Araq <rumpf_a@web.de> | 2014-03-29 13:50:07 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2014-03-29 13:50:07 +0100 |
commit | b6d5f18c9345c0000bf4f87dc455b3aa3946811d (patch) | |
tree | d1e32fd49f87e6c9cc5b50ae7e237785d0e6dd23 | |
parent | 2958adc47f201bde20deeae1f701029d3dd026ff (diff) | |
download | Nim-b6d5f18c9345c0000bf4f87dc455b3aa3946811d.tar.gz |
iterators check seqs/strings are not resized during iteration
-rw-r--r-- | lib/system.nim | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/lib/system.nim b/lib/system.nim index be57ba192..047b998c3 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -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.} |