int-iterator: fixed iterator's index value
This commit is contained in:
+18
-17
@@ -17,6 +17,8 @@ import (
|
|||||||
type IntIterator struct {
|
type IntIterator struct {
|
||||||
count int64
|
count int64
|
||||||
index int64
|
index int64
|
||||||
|
next int64
|
||||||
|
current any
|
||||||
start int64
|
start int64
|
||||||
stop int64
|
stop int64
|
||||||
step int64
|
step int64
|
||||||
@@ -110,26 +112,23 @@ func (it *IntIterator) CallOperation(name string, args map[string]any) (v any, e
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (it *IntIterator) Current() (item any, err error) {
|
func (it *IntIterator) Current() (item any, err error) {
|
||||||
if it.start <= it.stop {
|
return it.current, nil
|
||||||
if it.index >= it.start && it.index < it.stop {
|
|
||||||
item = it.index
|
|
||||||
} else {
|
|
||||||
err = io.EOF
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if it.index > it.stop && it.index <= it.start {
|
|
||||||
item = it.index
|
|
||||||
} else {
|
|
||||||
err = io.EOF
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it *IntIterator) Next() (item any, err error) {
|
func (it *IntIterator) Next() (item any, err error) {
|
||||||
it.index += it.step
|
item = it.next
|
||||||
if item, err = it.Current(); err != io.EOF {
|
if it.step > 0 {
|
||||||
|
if it.next+it.step > it.stop {
|
||||||
|
err = io.EOF
|
||||||
|
}
|
||||||
|
} else if it.next+it.step < it.stop {
|
||||||
|
err = io.EOF
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
it.index++
|
||||||
it.count++
|
it.count++
|
||||||
|
it.next += it.step
|
||||||
|
it.current = item
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -143,7 +142,9 @@ func (it *IntIterator) Count() int64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (it *IntIterator) Reset() error {
|
func (it *IntIterator) Reset() error {
|
||||||
it.index = it.start - it.step
|
it.index = -1
|
||||||
|
it.next = it.start
|
||||||
|
it.current = nil
|
||||||
it.count = 0
|
it.count = 0
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -45,9 +45,10 @@ func TestIteratorParser(t *testing.T) {
|
|||||||
/* 26 */ {`it=$(1..5..2); it++`, int64(1), nil},
|
/* 26 */ {`it=$(1..5..2); it++`, int64(1), nil},
|
||||||
/* 27 */ {`it=$(1.5..5..2); it++`, nil, `[1:13] interval expression expected integer, got float (1.5)`},
|
/* 27 */ {`it=$(1.5..5..2); it++`, nil, `[1:13] interval expression expected integer, got float (1.5)`},
|
||||||
/* 28 */ {`it=$(.."z"); it++`, nil, `[1:7] interval expression expected integer, got string (z)`},
|
/* 28 */ {`it=$(.."z"); it++`, nil, `[1:7] interval expression expected integer, got string (z)`},
|
||||||
|
/* 29 */ {`it=$(3..1); it++; it.index`, int64(0), nil},
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunTestSuiteSpec(t, section, inputs, 27)
|
// RunTestSuiteSpec(t, section, inputs, 25)
|
||||||
RunTestSuite(t, section, inputs)
|
RunTestSuite(t, section, inputs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user