aboutsummaryrefslogtreecommitdiffstats
path: root/trie/encoding.go
diff options
context:
space:
mode:
Diffstat (limited to 'trie/encoding.go')
-rw-r--r--trie/encoding.go67
1 files changed, 27 insertions, 40 deletions
diff --git a/trie/encoding.go b/trie/encoding.go
index 8c2d6a3f7..9c862d78f 100644
--- a/trie/encoding.go
+++ b/trie/encoding.go
@@ -1,28 +1,22 @@
// Copyright 2014 The go-ethereum Authors
-// This file is part of go-ethereum.
+// This file is part of the go-ethereum library.
//
-// go-ethereum is free software: you can redistribute it and/or modify
+// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
-// go-ethereum is distributed in the hope that it will be useful,
+// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
-// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
+// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package trie
-import (
- "bytes"
- "encoding/hex"
- "strings"
-)
-
-func CompactEncode(hexSlice []byte) string {
+func CompactEncode(hexSlice []byte) []byte {
terminator := 0
if hexSlice[len(hexSlice)-1] == 16 {
terminator = 1
@@ -40,15 +34,15 @@ func CompactEncode(hexSlice []byte) string {
hexSlice = append([]byte{flags, 0}, hexSlice...)
}
- var buff bytes.Buffer
- for i := 0; i < len(hexSlice); i += 2 {
- buff.WriteByte(byte(16*hexSlice[i] + hexSlice[i+1]))
+ l := len(hexSlice) / 2
+ var buf = make([]byte, l)
+ for i := 0; i < l; i++ {
+ buf[i] = 16*hexSlice[2*i] + hexSlice[2*i+1]
}
-
- return buff.String()
+ return buf
}
-func CompactDecode(str string) []byte {
+func CompactDecode(str []byte) []byte {
base := CompactHexDecode(str)
base = base[:len(base)-1]
if base[0] >= 2 {
@@ -63,30 +57,23 @@ func CompactDecode(str string) []byte {
return base
}
-func CompactHexDecode(str string) []byte {
- base := "0123456789abcdef"
- var hexSlice []byte
-
- enc := hex.EncodeToString([]byte(str))
- for _, v := range enc {
- hexSlice = append(hexSlice, byte(strings.IndexByte(base, byte(v))))
+func CompactHexDecode(str []byte) []byte {
+ l := len(str)*2 + 1
+ var nibbles = make([]byte, l)
+ for i, b := range str {
+ nibbles[i*2] = b / 16
+ nibbles[i*2+1] = b % 16
}
- hexSlice = append(hexSlice, 16)
-
- return hexSlice
+ nibbles[l-1] = 16
+ return nibbles
}
-func DecodeCompact(key []byte) string {
- const base = "0123456789abcdef"
- var str string
-
- for _, v := range key {
- if v < 16 {
- str += string(base[v])
- }
+func DecodeCompact(key []byte) []byte {
+ l := len(key) / 2
+ var res = make([]byte, l)
+ for i := 0; i < l; i++ {
+ v1, v0 := key[2*i], key[2*i+1]
+ res[i] = v1*16 + v0
}
-
- res, _ := hex.DecodeString(str)
-
- return string(res)
+ return res
}