int-iterator: fixed iterator's index value
This commit is contained in:
+23
-22
@@ -15,11 +15,13 @@ import (
|
||||
)
|
||||
|
||||
type IntIterator struct {
|
||||
count int64
|
||||
index int64
|
||||
start int64
|
||||
stop int64
|
||||
step int64
|
||||
count int64
|
||||
index int64
|
||||
next int64
|
||||
current any
|
||||
start int64
|
||||
stop int64
|
||||
step int64
|
||||
}
|
||||
|
||||
func NewIntIteratorFromInterval(p *interval.IntervalType) (it *IntIterator, err error) {
|
||||
@@ -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) {
|
||||
if it.start <= it.stop {
|
||||
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
|
||||
return it.current, nil
|
||||
}
|
||||
|
||||
func (it *IntIterator) Next() (item any, err error) {
|
||||
it.index += it.step
|
||||
if item, err = it.Current(); err != io.EOF {
|
||||
item = it.next
|
||||
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.next += it.step
|
||||
it.current = item
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -143,7 +142,9 @@ func (it *IntIterator) Count() int64 {
|
||||
}
|
||||
|
||||
func (it *IntIterator) Reset() error {
|
||||
it.index = it.start - it.step
|
||||
it.index = -1
|
||||
it.next = it.start
|
||||
it.current = nil
|
||||
it.count = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
+2
-1
@@ -45,9 +45,10 @@ func TestIteratorParser(t *testing.T) {
|
||||
/* 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)`},
|
||||
/* 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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user