@@ -70,7 +70,7 @@ func (e flatenc) EncodeRLP(out io.Writer) error {
7070 newhead := eb .lheads [prevnheads ]
7171 copy (eb .lheads [prevnheads :], eb .lheads [prevnheads + 1 :])
7272 eb .lheads = eb .lheads [:len (eb .lheads )- 1 ]
73- eb .lhsize -= newhead .tagsize ( )
73+ eb .lhsize -= headsize ( uint64 ( newhead .size ) )
7474 return nil
7575}
7676
@@ -155,21 +155,29 @@ type listhead struct {
155155// encode writes head to the given buffer, which must be at least
156156// 9 bytes long. It returns the encoded bytes.
157157func (head * listhead ) encode (buf []byte ) []byte {
158- if head .size < 56 {
159- buf [0 ] = 0xC0 + byte (head .size )
160- return buf [:1 ]
161- } else {
162- sizesize := putint (buf [1 :], uint64 (head .size ))
163- buf [0 ] = 0xF7 + byte (sizesize )
164- return buf [:sizesize + 1 ]
158+ return buf [:puthead (buf , 0xC0 , 0xF7 , uint64 (head .size ))]
159+ }
160+
161+ // headsize returns the size of a list or string header
162+ // for a value of the given size.
163+ func headsize (size uint64 ) int {
164+ if size < 56 {
165+ return 1
165166 }
167+ return 1 + intsize (size )
166168}
167169
168- func (head * listhead ) tagsize () int {
169- if head .size < 56 {
170+ // puthead writes a list or string header to buf.
171+ // buf must be at least 9 bytes long.
172+ func puthead (buf []byte , smalltag , largetag byte , size uint64 ) int {
173+ if size < 56 {
174+ buf [0 ] = smalltag + byte (size )
170175 return 1
176+ } else {
177+ sizesize := putint (buf [1 :], size )
178+ buf [0 ] = largetag + byte (sizesize )
179+ return sizesize + 1
171180 }
172- return 1 + intsize (uint64 (head .size ))
173181}
174182
175183func newencbuf () * encbuf {
0 commit comments