-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlowlevel.go
53 lines (48 loc) · 1.27 KB
/
lowlevel.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
package fastmsgpack
import (
"github.com/hexon/fastmsgpack/internal"
)
// Size returns the number of bytes the first entry in the given msgpack data is.
func Size(data []byte) (int, error) {
return internal.ValueLength(data)
}
// SplitArray splits a msgpack array into the msgpack chunks of its components.
// The returned slices point into the given data.
func SplitArray(data []byte) ([][]byte, error) {
d := NewDecoder(data)
elements, err := d.DecodeArrayLen()
if err != nil {
return nil, err
}
ret := make([][]byte, elements)
for i := 0; elements > i; i++ {
v, err := d.DecodeRaw()
if err != nil {
return nil, err
}
ret[i] = v
}
return ret, nil
}
// SplitMap splits a msgpack map into string-keys and the msgpack-values. It does not decode the values.
// The returned slices point into the given data.
func SplitMap(data []byte, dict *Dict) ([]string, [][]byte, error) {
d := NewDecoder(data)
elements, err := d.DecodeArrayLen()
if err != nil {
return nil, nil, err
}
keys := make([]string, elements)
values := make([][]byte, elements)
for i := 0; elements > i; i++ {
keys[i], err = d.DecodeString()
if err != nil {
return nil, nil, err
}
values[i], err = d.DecodeRaw()
if err != nil {
return nil, nil, err
}
}
return keys, values, nil
}