mirror of https://github.com/tidwall/redcon.git
Added bulk number appending
This commit is contained in:
parent
47f86dd29a
commit
565ec452b4
36
append.go
36
append.go
|
@ -312,3 +312,39 @@ func AppendTile38(b []byte, data []byte) []byte {
|
|||
func AppendNull(b []byte) []byte {
|
||||
return append(b, '$', '-', '1', '\r', '\n')
|
||||
}
|
||||
|
||||
// AppendBulkFloat appends a float64, as bulk bytes.
|
||||
func AppendBulkFloat(b []byte, f float64) []byte {
|
||||
mark1 := len(b)
|
||||
b = strconv.AppendFloat(b, f, 'f', -1, 64)
|
||||
mark2 := len(b)
|
||||
b = AppendBulk(b, b[mark1:mark2])
|
||||
mark3 := len(b)
|
||||
copy(b[mark1:], b[mark2:])
|
||||
b = b[:mark1+(mark3-mark2)]
|
||||
return b
|
||||
}
|
||||
|
||||
// AppendBulkInt appends an int64, as bulk bytes.
|
||||
func AppendBulkInt(b []byte, x int64) []byte {
|
||||
mark1 := len(b)
|
||||
b = strconv.AppendInt(b, x, 10)
|
||||
mark2 := len(b)
|
||||
b = AppendBulk(b, b[mark1:mark2])
|
||||
mark3 := len(b)
|
||||
copy(b[mark1:], b[mark2:])
|
||||
b = b[:mark1+(mark3-mark2)]
|
||||
return b
|
||||
}
|
||||
|
||||
// AppendBulkUint appends an uint64, as bulk bytes.
|
||||
func AppendBulkUint(b []byte, x uint64) []byte {
|
||||
mark1 := len(b)
|
||||
b = strconv.AppendUint(b, x, 10)
|
||||
mark2 := len(b)
|
||||
b = AppendBulk(b, b[mark1:mark2])
|
||||
mark3 := len(b)
|
||||
copy(b[mark1:], b[mark2:])
|
||||
b = b[:mark1+(mark3-mark2)]
|
||||
return b
|
||||
}
|
||||
|
|
|
@ -92,3 +92,36 @@ func TestNextCommand(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendBulkFloat(t *testing.T) {
|
||||
var b []byte
|
||||
b = AppendString(b, "HELLO")
|
||||
b = AppendBulkFloat(b, 9.123192839)
|
||||
b = AppendString(b, "HELLO")
|
||||
exp := "+HELLO\r\n$11\r\n9.123192839\r\n+HELLO\r\n"
|
||||
if string(b) != exp {
|
||||
t.Fatalf("expected '%s', got '%s'", exp, b)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendBulkInt(t *testing.T) {
|
||||
var b []byte
|
||||
b = AppendString(b, "HELLO")
|
||||
b = AppendBulkInt(b, -9182739137)
|
||||
b = AppendString(b, "HELLO")
|
||||
exp := "+HELLO\r\n$11\r\n-9182739137\r\n+HELLO\r\n"
|
||||
if string(b) != exp {
|
||||
t.Fatalf("expected '%s', got '%s'", exp, b)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendBulkUint(t *testing.T) {
|
||||
var b []byte
|
||||
b = AppendString(b, "HELLO")
|
||||
b = AppendBulkInt(b, 91827391370)
|
||||
b = AppendString(b, "HELLO")
|
||||
exp := "+HELLO\r\n$11\r\n91827391370\r\n+HELLO\r\n"
|
||||
if string(b) != exp {
|
||||
t.Fatalf("expected '%s', got '%s'", exp, b)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue