Skip to content

Commit f7112cc

Browse files
authored
rlp: add SplitUint64 (#21563)
This can be useful when working with raw RLP data.
1 parent 71c37d8 commit f7112cc

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

‎rlp/raw.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,32 @@ func SplitString(b []byte) (content, rest []byte, err error) {
5757
return content, rest, nil
5858
}
5959

60+
// SplitUint64 decodes an integer at the beginning of b.
61+
// It also returns the remaining data after the integer in 'rest'.
62+
func SplitUint64(b []byte) (x uint64, rest []byte, err error) {
63+
content, rest, err := SplitString(b)
64+
if err != nil {
65+
return 0, b, err
66+
}
67+
switch {
68+
case len(content) == 0:
69+
return 0, rest, nil
70+
case len(content) == 1:
71+
if content[0] == 0 {
72+
return 0, b, ErrCanonInt
73+
}
74+
return uint64(content[0]), rest, nil
75+
case len(content) > 8:
76+
return 0, b, errUintOverflow
77+
default:
78+
x, err = readSize(content, byte(len(content)))
79+
if err != nil {
80+
return 0, b, ErrCanonInt
81+
}
82+
return x, rest, nil
83+
}
84+
}
85+
6086
// SplitList splits b into the content of a list and any remaining
6187
// bytes after the list.
6288
func SplitList(b []byte) (content, rest []byte, err error) {

‎rlp/raw_test.go‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,59 @@ func TestSplitTypes(t *testing.T) {
7171
}
7272
}
7373

74+
func TestSplitUint64(t *testing.T) {
75+
tests := []struct {
76+
input string
77+
val uint64
78+
rest string
79+
err error
80+
}{
81+
{"01", 1, "", nil},
82+
{"7FFF", 0x7F, "FF", nil},
83+
{"80FF", 0, "FF", nil},
84+
{"81FAFF", 0xFA, "FF", nil},
85+
{"82FAFAFF", 0xFAFA, "FF", nil},
86+
{"83FAFAFAFF", 0xFAFAFA, "FF", nil},
87+
{"84FAFAFAFAFF", 0xFAFAFAFA, "FF", nil},
88+
{"85FAFAFAFAFAFF", 0xFAFAFAFAFA, "FF", nil},
89+
{"86FAFAFAFAFAFAFF", 0xFAFAFAFAFAFA, "FF", nil},
90+
{"87FAFAFAFAFAFAFAFF", 0xFAFAFAFAFAFAFA, "FF", nil},
91+
{"88FAFAFAFAFAFAFAFAFF", 0xFAFAFAFAFAFAFAFA, "FF", nil},
92+
93+
// errors
94+
{"", 0, "", io.ErrUnexpectedEOF},
95+
{"00", 0, "00", ErrCanonInt},
96+
{"81", 0, "81", ErrValueTooLarge},
97+
{"8100", 0, "8100", ErrCanonSize},
98+
{"8200FF", 0, "8200FF", ErrCanonInt},
99+
{"8103FF", 0, "8103FF", ErrCanonSize},
100+
{"89FAFAFAFAFAFAFAFAFAFF", 0, "89FAFAFAFAFAFAFAFAFAFF", errUintOverflow},
101+
}
102+
103+
for i, test := range tests {
104+
val, rest, err := SplitUint64(unhex(test.input))
105+
if val != test.val {
106+
t.Errorf("test %d: val mismatch: got %x, want %x (input %q)", i, val, test.val, test.input)
107+
}
108+
if !bytes.Equal(rest, unhex(test.rest)) {
109+
t.Errorf("test %d: rest mismatch: got %x, want %s (input %q)", i, rest, test.rest, test.input)
110+
}
111+
if err != test.err {
112+
t.Errorf("test %d: error mismatch: got %q, want %q", i, err, test.err)
113+
}
114+
}
115+
}
116+
74117
func TestSplit(t *testing.T) {
75118
tests := []struct {
76119
input string
77120
kind Kind
78121
val, rest string
79122
err error
80123
}{
124+
{input: "00FFFF", kind: Byte, val: "00", rest: "FFFF"},
81125
{input: "01FFFF", kind: Byte, val: "01", rest: "FFFF"},
126+
{input: "7FFFFF", kind: Byte, val: "7F", rest: "FFFF"},
82127
{input: "80FFFF", kind: String, val: "", rest: "FFFF"},
83128
{input: "C3010203", kind: List, val: "010203"},
84129

0 commit comments

Comments
 (0)