Removed slice appending for integers

This commit is contained in:
obscuren 2013-12-28 02:24:01 +01:00
parent d6460f3de1
commit 5a7eae705b
2 changed files with 9 additions and 3 deletions

4
rlp.go
View File

@ -45,7 +45,7 @@ func Decode(data []byte, pos int) (interface{}, int) {
slice := make([]interface{}, 0) slice := make([]interface{}, 0)
switch { switch {
case char < 24: case char < 24:
return append(slice, data[pos]), pos + 1 return data[pos], pos + 1
case char < 56: case char < 56:
b := int(data[pos]) - 23 b := int(data[pos]) - 23
return FromBin(data[pos+1 : pos+1+b]), pos + 1 + b return FromBin(data[pos+1 : pos+1+b]), pos + 1 + b
@ -72,7 +72,7 @@ func Decode(data []byte, pos int) (interface{}, int) {
return slice, pos return slice, pos
case char < 192: case char < 192:
b := int(data[pos]) - 183 b := int(data[pos]) - 183
//b2 := int(FromBin(data[pos+1 : pos+1+b])) (ref imprementation has an unused variable) //b2 := int(FromBin(data[pos+1 : pos+1+b])) (ref implementation has an unused variable)
pos = pos+1+b pos = pos+1+b
for i := 0; i < b; i++ { for i := 0; i < b; i++ {
var obj interface{} var obj interface{}

View File

@ -15,7 +15,6 @@ func TestEncode(t *testing.T) {
dec,_ := Decode(bytes, 0) dec,_ := Decode(bytes, 0)
fmt.Printf("raw: %v encoded: %q == %v\n", dec, str, "dog") fmt.Printf("raw: %v encoded: %q == %v\n", dec, str, "dog")
sliceRes := "\x83CdogCgodCcat" sliceRes := "\x83CdogCgodCcat"
strs := []string{"dog", "god", "cat"} strs := []string{"dog", "god", "cat"}
bytes = Encode(strs) bytes = Encode(strs)
@ -27,3 +26,10 @@ func TestEncode(t *testing.T) {
dec,_ = Decode(bytes, 0) dec,_ = Decode(bytes, 0)
fmt.Printf("raw: %v encoded: %q == %v\n", dec, slice, strs) fmt.Printf("raw: %v encoded: %q == %v\n", dec, slice, strs)
} }
func BenchmarkEncodeDecode(b *testing.B) {
for i := 0; i < b.N; i++ {
bytes := Encode([]string{"dog", "god", "cat"})
Decode(bytes, 0)
}
}