iterators: several small fixes (#11162)
* fix pairs, mpairs, mitems for cstring for JS backend * add mutation check for pairs and mpairs for strings and seqs * change the assertion message - modifying (changing elements) is not a problem, changing the length is
This commit is contained in:
parent
d2e1936cf2
commit
ce152ed91f
3 changed files with 53 additions and 26 deletions
|
|
@ -55,20 +55,27 @@ iterator items*(a: cstring): char {.inline.} =
|
||||||
|
|
||||||
iterator mitems*(a: var cstring): var char {.inline.} =
|
iterator mitems*(a: var cstring): var char {.inline.} =
|
||||||
## Iterates over each item of `a` so that you can modify the yielded value.
|
## Iterates over each item of `a` so that you can modify the yielded value.
|
||||||
var i = 0
|
when defined(js):
|
||||||
while a[i] != '\0':
|
var i = 0
|
||||||
yield a[i]
|
var L = len(a)
|
||||||
inc(i)
|
while i < L:
|
||||||
|
yield a[i]
|
||||||
|
inc(i)
|
||||||
|
else:
|
||||||
|
var i = 0
|
||||||
|
while a[i] != '\0':
|
||||||
|
yield a[i]
|
||||||
|
inc(i)
|
||||||
|
|
||||||
iterator items*(E: typedesc[enum]): E =
|
iterator items*(E: typedesc[enum]): E =
|
||||||
## Iterates over the values of the enum ``E``.
|
## Iterates over the values of the enum ``E``.
|
||||||
for v in low(E)..high(E):
|
for v in low(E) .. high(E):
|
||||||
yield v
|
yield v
|
||||||
|
|
||||||
iterator items*[T](s: HSlice[T, T]): T =
|
iterator items*[T](s: HSlice[T, T]): T =
|
||||||
## Iterates over the slice `s`, yielding each value between `s.a` and `s.b`
|
## Iterates over the slice `s`, yielding each value between `s.a` and `s.b`
|
||||||
## (inclusively).
|
## (inclusively).
|
||||||
for x in s.a..s.b:
|
for x in s.a .. s.b:
|
||||||
yield x
|
yield x
|
||||||
|
|
||||||
iterator pairs*[T](a: openArray[T]): tuple[key: int, val: T] {.inline.} =
|
iterator pairs*[T](a: openArray[T]): tuple[key: int, val: T] {.inline.} =
|
||||||
|
|
@ -108,47 +115,69 @@ iterator mpairs*[IX, T](a:var array[IX, T]):tuple[key:IX,val:var T] {.inline.} =
|
||||||
iterator pairs*[T](a: seq[T]): tuple[key: int, val: T] {.inline.} =
|
iterator pairs*[T](a: seq[T]): tuple[key: int, val: T] {.inline.} =
|
||||||
## Iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
|
## Iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
|
||||||
var i = 0
|
var i = 0
|
||||||
while i < len(a):
|
let L = len(a)
|
||||||
|
while i < L:
|
||||||
yield (i, a[i])
|
yield (i, a[i])
|
||||||
inc(i)
|
inc(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.} =
|
iterator mpairs*[T](a: var seq[T]): tuple[key: int, val: var T] {.inline.} =
|
||||||
## Iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
|
## Iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
|
||||||
## ``a[index]`` can be modified.
|
## ``a[index]`` can be modified.
|
||||||
var i = 0
|
var i = 0
|
||||||
while i < len(a):
|
let L = len(a)
|
||||||
|
while i < L:
|
||||||
yield (i, a[i])
|
yield (i, a[i])
|
||||||
inc(i)
|
inc(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.} =
|
iterator pairs*(a: string): tuple[key: int, val: char] {.inline.} =
|
||||||
## Iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
|
## Iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
|
||||||
var i = 0
|
var i = 0
|
||||||
while i < len(a):
|
let L = len(a)
|
||||||
|
while i < L:
|
||||||
yield (i, a[i])
|
yield (i, a[i])
|
||||||
inc(i)
|
inc(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.} =
|
iterator mpairs*(a: var string): tuple[key: int, val: var char] {.inline.} =
|
||||||
## Iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
|
## Iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
|
||||||
## ``a[index]`` can be modified.
|
## ``a[index]`` can be modified.
|
||||||
var i = 0
|
var i = 0
|
||||||
while i < len(a):
|
let L = len(a)
|
||||||
|
while i < L:
|
||||||
yield (i, a[i])
|
yield (i, a[i])
|
||||||
inc(i)
|
inc(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.} =
|
iterator pairs*(a: cstring): tuple[key: int, val: char] {.inline.} =
|
||||||
## Iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
|
## Iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
|
||||||
var i = 0
|
when defined(js):
|
||||||
while a[i] != '\0':
|
var i = 0
|
||||||
yield (i, a[i])
|
var L = len(a)
|
||||||
inc(i)
|
while i < L:
|
||||||
|
yield (i, a[i])
|
||||||
|
inc(i)
|
||||||
|
else:
|
||||||
|
var i = 0
|
||||||
|
while a[i] != '\0':
|
||||||
|
yield (i, a[i])
|
||||||
|
inc(i)
|
||||||
|
|
||||||
iterator mpairs*(a: var cstring): tuple[key: int, val: var char] {.inline.} =
|
iterator mpairs*(a: var cstring): tuple[key: int, val: var char] {.inline.} =
|
||||||
## Iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
|
## Iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
|
||||||
## ``a[index]`` can be modified.
|
## ``a[index]`` can be modified.
|
||||||
var i = 0
|
when defined(js):
|
||||||
while a[i] != '\0':
|
var i = 0
|
||||||
yield (i, a[i])
|
var L = len(a)
|
||||||
inc(i)
|
while i < L:
|
||||||
|
yield (i, a[i])
|
||||||
|
inc(i)
|
||||||
|
else:
|
||||||
|
var i = 0
|
||||||
|
while a[i] != '\0':
|
||||||
|
yield (i, a[i])
|
||||||
|
inc(i)
|
||||||
|
|
||||||
|
|
||||||
iterator items*[T](a: seq[T]): T {.inline.} =
|
iterator items*[T](a: seq[T]): T {.inline.} =
|
||||||
|
|
@ -158,7 +187,7 @@ iterator items*[T](a: seq[T]): T {.inline.} =
|
||||||
while i < L:
|
while i < L:
|
||||||
yield a[i]
|
yield a[i]
|
||||||
inc(i)
|
inc(i)
|
||||||
assert(len(a) == L, "seq modified while iterating over it")
|
assert(len(a) == L, "the length of the seq changed while iterating over it")
|
||||||
|
|
||||||
iterator mitems*[T](a: var seq[T]): var T {.inline.} =
|
iterator mitems*[T](a: var seq[T]): var T {.inline.} =
|
||||||
## Iterates over each item of `a` so that you can modify the yielded value.
|
## Iterates over each item of `a` so that you can modify the yielded value.
|
||||||
|
|
@ -167,7 +196,7 @@ iterator mitems*[T](a: var seq[T]): var T {.inline.} =
|
||||||
while i < L:
|
while i < L:
|
||||||
yield a[i]
|
yield a[i]
|
||||||
inc(i)
|
inc(i)
|
||||||
assert(len(a) == L, "seq modified while iterating over it")
|
assert(len(a) == L, "the length of the seq changed while iterating over it")
|
||||||
|
|
||||||
iterator items*(a: string): char {.inline.} =
|
iterator items*(a: string): char {.inline.} =
|
||||||
## Iterates over each item of `a`.
|
## Iterates over each item of `a`.
|
||||||
|
|
@ -176,7 +205,7 @@ iterator items*(a: string): char {.inline.} =
|
||||||
while i < L:
|
while i < L:
|
||||||
yield a[i]
|
yield a[i]
|
||||||
inc(i)
|
inc(i)
|
||||||
assert(len(a) == L, "string modified while iterating over it")
|
assert(len(a) == L, "the length of the string changed while iterating over it")
|
||||||
|
|
||||||
iterator mitems*(a: var string): var char {.inline.} =
|
iterator mitems*(a: var string): var char {.inline.} =
|
||||||
## Iterates over each item of `a` so that you can modify the yielded value.
|
## Iterates over each item of `a` so that you can modify the yielded value.
|
||||||
|
|
@ -185,7 +214,7 @@ iterator mitems*(a: var string): var char {.inline.} =
|
||||||
while i < L:
|
while i < L:
|
||||||
yield a[i]
|
yield a[i]
|
||||||
inc(i)
|
inc(i)
|
||||||
assert(len(a) == L, "string modified while iterating over it")
|
assert(len(a) == L, "the length of the string changed while iterating over it")
|
||||||
|
|
||||||
|
|
||||||
iterator fields*[T: tuple|object](x: T): RootObj {.
|
iterator fields*[T: tuple|object](x: T): RootObj {.
|
||||||
|
|
@ -244,5 +273,3 @@ iterator fieldPairs*[S: tuple|object, T: tuple|object](x: S, y: T): tuple[
|
||||||
## **Warning**: This really transforms the 'for' and unrolls the loop.
|
## **Warning**: This really transforms the 'for' and unrolls the loop.
|
||||||
## The current implementation also has a bug that affects symbol binding
|
## The current implementation also has a bug that affects symbol binding
|
||||||
## in the loop body.
|
## in the loop body.
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ iterator myitems*[T](a: var seq[T]): var T {.inline.} =
|
||||||
while i < L:
|
while i < L:
|
||||||
yield a[i]
|
yield a[i]
|
||||||
inc(i)
|
inc(i)
|
||||||
assert(len(a) == L, "seq modified while iterating over it")
|
assert(len(a) == L, "the length of the seq changed while iterating over it")
|
||||||
|
|
||||||
# Works fine
|
# Works fine
|
||||||
var xs = @[1,2,3]
|
var xs = @[1,2,3]
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ iterator unitems*[T](a: seq[owned T]): T {.inline.} =
|
||||||
while i < L:
|
while i < L:
|
||||||
yield a[i]
|
yield a[i]
|
||||||
inc(i)
|
inc(i)
|
||||||
assert(len(a) == L, "seq modified while iterating over it")
|
assert(len(a) == L, "the length of the seq changed while iterating over it")
|
||||||
|
|
||||||
proc newWindow(): owned Window =
|
proc newWindow(): owned Window =
|
||||||
proc windraw(self: Widget) =
|
proc windraw(self: Widget) =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue