9 type WriteBuffer struct {
13 func NewWriteBuffer() *WriteBuffer {
15 b: make([]byte, 0, 4096),
19 func (w *WriteBuffer) Len() int { return len(w.b) }
20 func (w *WriteBuffer) Bytes() []byte { return w.b }
21 func (w *WriteBuffer) Reset() { w.b = w.b[:0] }
23 func (w *WriteBuffer) Append(args []interface{}) error {
24 w.b = append(w.b, ArrayReply)
25 w.b = strconv.AppendUint(w.b, uint64(len(args)), 10)
26 w.b = append(w.b, '\r', '\n')
28 for _, arg := range args {
29 if err := w.append(arg); err != nil {
36 func (w *WriteBuffer) append(val interface{}) error {
37 switch v := val.(type) {
45 w.AppendString(formatInt(int64(v)))
47 w.AppendString(formatInt(int64(v)))
49 w.AppendString(formatInt(int64(v)))
51 w.AppendString(formatInt(int64(v)))
53 w.AppendString(formatInt(v))
55 w.AppendString(formatUint(uint64(v)))
57 w.AppendString(formatUint(uint64(v)))
59 w.AppendString(formatUint(uint64(v)))
61 w.AppendString(formatUint(uint64(v)))
63 w.AppendString(formatUint(v))
65 w.AppendString(formatFloat(float64(v)))
67 w.AppendString(formatFloat(v))
74 case encoding.BinaryMarshaler:
75 b, err := v.MarshalBinary()
82 "redis: can't marshal %T (consider implementing encoding.BinaryMarshaler)", val)
87 func (w *WriteBuffer) AppendString(s string) {
88 w.b = append(w.b, StringReply)
89 w.b = strconv.AppendUint(w.b, uint64(len(s)), 10)
90 w.b = append(w.b, '\r', '\n')
91 w.b = append(w.b, s...)
92 w.b = append(w.b, '\r', '\n')
95 func (w *WriteBuffer) AppendBytes(p []byte) {
96 w.b = append(w.b, StringReply)
97 w.b = strconv.AppendUint(w.b, uint64(len(p)), 10)
98 w.b = append(w.b, '\r', '\n')
99 w.b = append(w.b, p...)
100 w.b = append(w.b, '\r', '\n')
103 func formatInt(n int64) string {
104 return strconv.FormatInt(n, 10)
107 func formatUint(u uint64) string {
108 return strconv.FormatUint(u, 10)
111 func formatFloat(f float64) string {
112 return strconv.FormatFloat(f, 'f', -1, 64)