forked from go-faster/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dec_arr_iter.go
63 lines (57 loc) · 1.13 KB
/
dec_arr_iter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package jx
import (
"github.com/go-faster/errors"
)
// ArrIter is decoding array iterator.
type ArrIter struct {
d *Decoder
err error
closed bool
comma bool
}
// ArrIter creates new array iterator.
func (d *Decoder) ArrIter() (ArrIter, error) {
if err := d.consume('['); err != nil {
return ArrIter{}, errors.Wrap(err, `"[" expected`)
}
if err := d.incDepth(); err != nil {
return ArrIter{}, err
}
if _, err := d.more(); err != nil {
return ArrIter{}, err
}
d.unread()
return ArrIter{d: d}, nil
}
// Next consumes element and returns false, if there is no elements anymore.
func (i *ArrIter) Next() bool {
if i.closed || i.err != nil {
return false
}
dec := i.d
c, err := dec.more()
if err != nil {
i.err = err
return false
}
if c == ']' {
i.closed = true
i.err = dec.decDepth()
return false
}
if i.comma {
if c != ',' {
err := badToken(c, dec.offset()-1)
i.err = errors.Wrap(err, `"," expected`)
return false
}
} else {
dec.unread()
}
i.comma = true
return true
}
// Err returns the error, if any, that was encountered during iteration.
func (i *ArrIter) Err() error {
return i.err
}