-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package trie | ||
|
||
import "encoding/binary" | ||
|
||
type vlcScope struct{} | ||
|
||
// vlc implements variable length coding. | ||
// | ||
// It's much simpler and a bit faster than RLP. | ||
// Trie nodes stored in database are encoded using VLC. | ||
var vlc vlcScope | ||
|
||
// AppendUint appends VLC-encoded i to buf and returns the extended buffer. | ||
func (vlcScope) AppendUint(buf []byte, i uint64) []byte { | ||
return binary.AppendUvarint(buf, i) | ||
} | ||
|
||
// AppendString appends RLP-encoded str to buf and returns the extended buffer. | ||
func (vlcScope) AppendString(buf, str []byte) []byte { | ||
buf = binary.AppendUvarint(buf, uint64(len(str))) | ||
return append(buf, str...) | ||
} | ||
|
||
// SplitString extracts a string and returns rest bytes. | ||
// It'll panic if errored. | ||
func (vlcScope) SplitString(buf []byte) (str []byte, rest []byte) { | ||
size, n := binary.Uvarint(buf) | ||
buf = buf[n:] | ||
return buf[:size], buf[size:] | ||
} | ||
|
||
// SplitUint extracts uint64 and returns rest bytes. | ||
// It'll panic if errored. | ||
func (vlcScope) SplitUint(buf []byte) (uint64, []byte) { | ||
v, n := binary.Uvarint(buf) | ||
return v, buf[n:] | ||
} |