rlp: add SplitUint64 (#21563)

This can be useful when working with raw RLP data.
This commit is contained in:
Felix Lange
2020-09-14 19:23:01 +02:00
committed by GitHub
parent 71c37d82ad
commit f7112cc182
2 changed files with 71 additions and 0 deletions

View File

@@ -57,6 +57,32 @@ func SplitString(b []byte) (content, rest []byte, err error) {
return content, rest, nil
}
// SplitUint64 decodes an integer at the beginning of b.
// It also returns the remaining data after the integer in 'rest'.
func SplitUint64(b []byte) (x uint64, rest []byte, err error) {
content, rest, err := SplitString(b)
if err != nil {
return 0, b, err
}
switch {
case len(content) == 0:
return 0, rest, nil
case len(content) == 1:
if content[0] == 0 {
return 0, b, ErrCanonInt
}
return uint64(content[0]), rest, nil
case len(content) > 8:
return 0, b, errUintOverflow
default:
x, err = readSize(content, byte(len(content)))
if err != nil {
return 0, b, ErrCanonInt
}
return x, rest, nil
}
}
// SplitList splits b into the content of a list and any remaining
// bytes after the list.
func SplitList(b []byte) (content, rest []byte, err error) {