@@ -229,6 +229,16 @@ func readTelnetCommand(packet []byte, argsbuf [][]byte) (
229229 return false , args [:0 ], Telnet , packet , nil
230230}
231231
232+ // appendPrefix will append a "$3\r\n" style redis prefix for a message.
233+ func appendPrefix (b []byte , c byte , n int64 ) []byte {
234+ if n >= 0 && n <= 9 {
235+ return append (b , c , byte ('0' + n ), '\r' , '\n' )
236+ }
237+ b = append (b , c )
238+ b = strconv .AppendInt (b , n , 10 )
239+ return append (b , '\r' , '\n' )
240+ }
241+
232242// AppendUint appends a Redis protocol uint64 to the input bytes.
233243func AppendUint (b []byte , n uint64 ) []byte {
234244 b = append (b , ':' )
@@ -238,32 +248,24 @@ func AppendUint(b []byte, n uint64) []byte {
238248
239249// AppendInt appends a Redis protocol int64 to the input bytes.
240250func AppendInt (b []byte , n int64 ) []byte {
241- b = append (b , ':' )
242- b = strconv .AppendInt (b , n , 10 )
243- return append (b , '\r' , '\n' )
251+ return appendPrefix (b , ':' , n )
244252}
245253
246254// AppendArray appends a Redis protocol array to the input bytes.
247255func AppendArray (b []byte , n int ) []byte {
248- b = append (b , '*' )
249- b = strconv .AppendInt (b , int64 (n ), 10 )
250- return append (b , '\r' , '\n' )
256+ return appendPrefix (b , '*' , int64 (n ))
251257}
252258
253259// AppendBulk appends a Redis protocol bulk byte slice to the input bytes.
254260func AppendBulk (b []byte , bulk []byte ) []byte {
255- b = append (b , '$' )
256- b = strconv .AppendInt (b , int64 (len (bulk )), 10 )
257- b = append (b , '\r' , '\n' )
261+ b = appendPrefix (b , '$' , int64 (len (bulk )))
258262 b = append (b , bulk ... )
259263 return append (b , '\r' , '\n' )
260264}
261265
262266// AppendBulkString appends a Redis protocol bulk string to the input bytes.
263267func AppendBulkString (b []byte , bulk string ) []byte {
264- b = append (b , '$' )
265- b = strconv .AppendInt (b , int64 (len (bulk )), 10 )
266- b = append (b , '\r' , '\n' )
268+ b = appendPrefix (b , '$' , int64 (len (bulk )))
267269 b = append (b , bulk ... )
268270 return append (b , '\r' , '\n' )
269271}
0 commit comments