Skip to content

Commit 94c4371

Browse files
authored
Merge pull request #2677 from CortexFoundation/dev
rlp: add AppendList method to RawList
2 parents 68020d5 + 4278259 commit 94c4371

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

rlp/raw.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,18 @@ func (r *RawList[T]) AppendRaw(b []byte) error {
168168
return nil
169169
}
170170

171+
// AppendList appends all items from another RawList to this list.
172+
func (r *RawList[T]) AppendList(other *RawList[T]) {
173+
if other.enc == nil || other.length == 0 {
174+
return
175+
}
176+
if r.enc == nil {
177+
r.enc = make([]byte, 9)
178+
}
179+
r.enc = append(r.enc, other.Content()...)
180+
r.length += other.length
181+
}
182+
171183
// StringSize returns the encoded size of a string.
172184
func StringSize(s string) uint64 {
173185
switch n := len(s); n {

rlp/raw_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,54 @@ func TestRawListAppendRaw(t *testing.T) {
246246
t.Fatalf("wrong Len %d after invalid appends, want 2", rl.Len())
247247
}
248248
}
249+
func TestRawListAppendList(t *testing.T) {
250+
var rl1 RawList[uint64]
251+
if err := rl1.Append(uint64(1)); err != nil {
252+
t.Fatal("append 1 failed:", err)
253+
}
254+
if err := rl1.Append(uint64(2)); err != nil {
255+
t.Fatal("append 2 failed:", err)
256+
}
257+
258+
var rl2 RawList[uint64]
259+
if err := rl2.Append(uint64(3)); err != nil {
260+
t.Fatal("append 3 failed:", err)
261+
}
262+
if err := rl2.Append(uint64(4)); err != nil {
263+
t.Fatal("append 4 failed:", err)
264+
}
265+
266+
rl1.AppendList(&rl2)
267+
268+
if rl1.Len() != 4 {
269+
t.Fatalf("wrong Len %d, want 4", rl1.Len())
270+
}
271+
if rl1.Size() != 5 {
272+
t.Fatalf("wrong Size %d, want 5", rl1.Size())
273+
}
274+
275+
items, err := rl1.Items()
276+
if err != nil {
277+
t.Fatal("Items failed:", err)
278+
}
279+
if !reflect.DeepEqual(items, []uint64{1, 2, 3, 4}) {
280+
t.Fatalf("wrong items: %v", items)
281+
}
282+
283+
var empty RawList[uint64]
284+
prevLen := rl1.Len()
285+
rl1.AppendList(&empty)
286+
287+
if rl1.Len() != prevLen {
288+
t.Fatalf("appending empty list changed Len: got %d, want %d", rl1.Len(), prevLen)
289+
}
290+
291+
empty.AppendList(&rl1)
292+
293+
if empty.Len() != 4 {
294+
t.Fatalf("wrong Len %d, want 4", empty.Len())
295+
}
296+
}
249297

250298
func TestRawListDecodeInvalid(t *testing.T) {
251299
tests := []struct {

0 commit comments

Comments
 (0)