-
Notifications
You must be signed in to change notification settings - Fork 67
/
slice.go
125 lines (117 loc) · 2.62 KB
/
slice.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package expr
import (
"errors"
"unicode/utf8"
"github.com/brimdata/zed"
"github.com/brimdata/zed/runtime/sam/expr/coerce"
"github.com/brimdata/zed/zcode"
)
type Slice struct {
zctx *zed.Context
elem Evaluator
from Evaluator
to Evaluator
}
func NewSlice(zctx *zed.Context, elem, from, to Evaluator) *Slice {
return &Slice{
zctx: zctx,
elem: elem,
from: from,
to: to,
}
}
var ErrSliceIndex = errors.New("slice index is not a number")
var ErrSliceIndexEmpty = errors.New("slice index is empty")
func (s *Slice) Eval(ectx Context, this zed.Value) zed.Value {
elem := s.elem.Eval(ectx, this)
if elem.IsError() {
return elem
}
var length int
switch zed.TypeUnder(elem.Type()).(type) {
case *zed.TypeOfBytes:
length = len(elem.Bytes())
case *zed.TypeOfString:
length = utf8.RuneCount(elem.Bytes())
case *zed.TypeArray, *zed.TypeSet:
n, err := elem.ContainerLength()
if err != nil {
panic(err)
}
length = n
default:
return s.zctx.WrapError("sliced value is not array, set, bytes, or string", elem)
}
if elem.IsNull() {
return elem
}
from, err := sliceIndex(ectx, this, s.from, length)
if err != nil && err != ErrSliceIndexEmpty {
return s.zctx.NewError(err)
}
to, err := sliceIndex(ectx, this, s.to, length)
if err != nil {
if err != ErrSliceIndexEmpty {
return s.zctx.NewError(err)
}
to = length
}
bytes := elem.Bytes()
switch zed.TypeUnder(elem.Type()).(type) {
case *zed.TypeOfBytes:
bytes = bytes[from:to]
case *zed.TypeOfString:
bytes = bytes[utf8PrefixLen(bytes, from):]
bytes = bytes[:utf8PrefixLen(bytes, to-from)]
case *zed.TypeArray, *zed.TypeSet:
it := bytes.Iter()
for k := 0; k < to && !it.Done(); k++ {
if k == from {
bytes = zcode.Bytes(it)
}
it.Next()
}
bytes = bytes[:len(bytes)-len(it)]
default:
panic(elem.Type())
}
return zed.NewValue(elem.Type(), bytes)
}
func sliceIndex(ectx Context, this zed.Value, slot Evaluator, length int) (int, error) {
if slot == nil {
//XXX
return 0, ErrSliceIndexEmpty
}
val := slot.Eval(ectx, this)
v, ok := coerce.ToInt(val)
if !ok {
return 0, ErrSliceIndex
}
index := int(v)
if index < 0 {
index += length
}
if index < 0 {
return 0, nil
}
if index > length {
return length, nil
}
return index, nil
}
// utf8PrefixLen returns the length in bytes of the first runeCount runes in b.
// It returns 0 if runeCount<0 and len(b) if runeCount>utf8.RuneCount(b).
func utf8PrefixLen(b []byte, runeCount int) int {
var i, runeCurrent int
for {
if runeCurrent >= runeCount {
return i
}
r, n := utf8.DecodeRune(b[i:])
if r == utf8.RuneError && n == 0 {
return i
}
i += n
runeCurrent++
}
}