forked from deepch/vdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec.go
69 lines (61 loc) · 974 Bytes
/
vec.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
package pio
func VecLen(vec [][]byte) (n int) {
for _, b := range vec {
n += len(b)
}
return
}
func VecSliceTo(in [][]byte, out [][]byte, s int, e int) (n int) {
if s < 0 {
s = 0
}
if e >= 0 && e < s {
panic("pio: VecSlice start > end")
}
i := 0
off := 0
for s > 0 && i < len(in) {
left := len(in[i])
read := s
if left < read {
read = left
}
left -= read
off += read
s -= read
e -= read
if left == 0 {
i++
off = 0
}
}
if s > 0 {
panic("pio: VecSlice start out of range")
}
for e != 0 && i < len(in) {
left := len(in[i])-off
read := left
if e > 0 && e < read {
read = e
}
out[n] = in[i][off:off+read]
n++
left -= read
e -= read
off += read
if left == 0 {
i++
off = 0
}
}
if e > 0 {
panic("pio: VecSlice end out of range")
}
return
}
func VecSlice(in [][]byte, s int, e int) (out [][]byte) {
out = make([][]byte, len(in))
n := VecSliceTo(in, out, s, e)
out = out[:n]
return
}