-
Notifications
You must be signed in to change notification settings - Fork 0
/
any_all.go
63 lines (48 loc) · 1.16 KB
/
any_all.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 jq
import (
"nikand.dev/go/cbor"
)
type (
Any struct {
arr []Off
}
All struct {
arr []Off
}
)
func NewAny() *Any { return &Any{} }
func NewAll() *All { return &All{} }
func (f *Any) ApplyTo(b *Buffer, off Off, next bool) (res Off, more bool, err error) {
if next {
return None, false, nil
}
res, f.arr, err = anyAllApplyTo(b, off, 0, f.arr[:0])
return res, false, err
}
func (f *All) ApplyTo(b *Buffer, off Off, next bool) (res Off, more bool, err error) {
if next {
return None, false, nil
}
res, f.arr, err = anyAllApplyTo(b, off, 1, f.arr[:0])
return res, false, err
}
func anyAllApplyTo(b *Buffer, off Off, flip Off, arr0 []Off) (res Off, arr []Off, err error) {
arr = arr0
tag := b.Reader().Tag(off)
if tag != cbor.Array && tag != cbor.Map {
return off, arr, NewTypeError(tag, cbor.Array, cbor.Map)
}
val := 0
if tag == cbor.Map {
val = 1
}
arr = b.Reader().ArrayMap(off, arr)
for j := 0; j < len(arr); j += 1 + val {
if (flip == 0) == IsTrue(b, arr[j+val]) {
return True ^ flip, arr, nil
}
}
return False ^ flip, arr, nil
}
func (Any) String() string { return "any" }
func (All) String() string { return "all" }