-
Notifications
You must be signed in to change notification settings - Fork 85
/
parse.go
54 lines (50 loc) · 911 Bytes
/
parse.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
package beanstalk
import (
"bytes"
"strconv"
"strings"
)
func parseDict(dat []byte) map[string]string {
if dat == nil {
return nil
}
d := make(map[string]string)
if bytes.HasPrefix(dat, yamlHead) {
dat = dat[4:]
}
for _, s := range bytes.Split(dat, nl) {
kv := bytes.SplitN(s, colonSpace, 2)
if len(kv) != 2 {
continue
}
d[string(kv[0])] = string(kv[1])
}
return d
}
func parseList(dat []byte) []string {
if dat == nil {
return nil
}
l := []string{}
if bytes.HasPrefix(dat, yamlHead) {
dat = dat[4:]
}
for _, s := range bytes.Split(dat, nl) {
if !bytes.HasPrefix(s, minusSpace) {
continue
}
l = append(l, string(s[2:]))
}
return l
}
func parseSize(s string) (string, int, error) {
i := strings.LastIndex(s, " ")
if i == -1 {
return "", 0, findRespError(s)
}
n, err := strconv.Atoi(s[i+1:])
if err != nil {
return "", 0, err
}
return s[:i], n, nil
}