-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappend.go
45 lines (35 loc) · 1006 Bytes
/
append.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
package resp
import "strconv"
func appendPredix(b []byte, c byte, n int) []byte {
b = append(b, c)
b = strconv.AppendInt(b, int64(n), 10)
return append(b, '\n')
}
func appendBulkLength(b []byte, n int) []byte {
return appendPredix(b, '$', n)
}
func appendArrayLength(b []byte, n int) []byte {
return appendPredix(b, '*', n)
}
func appendLineEnding(b []byte) []byte {
return append(b, '\n')
}
func AppendSimpleString(b, c []byte) []byte {
return appendLineEnding(append(append(b, '+'), c...))
}
func AppendError(b, c []byte) []byte {
return appendLineEnding(append(append(b, '-'), c...))
}
func AppendInteger(b []byte, n int) []byte {
return appendLineEnding(append(append(b, ':'), []byte(strconv.Itoa(n))...))
}
func AppendBulkString(b, c []byte) []byte {
return appendLineEnding(append(appendBulkLength(b, len(c)), c...))
}
func AppendArray(b []byte, c ...[]byte) []byte {
b = appendArrayLength(b, len(c))
for _, d := range c {
b = append(b, d...)
}
return appendLineEnding(b)
}