rlp: no need to repeat called len method (#29936)

rlp: no need to repeat calling len
This commit is contained in:
Marquis Shanahan 2024-06-06 15:55:38 +08:00 committed by GitHub
parent 6c518fe606
commit cbbfa3eac0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -30,33 +30,33 @@ var rawValueType = reflect.TypeOf(RawValue{})
// StringSize returns the encoded size of a string. // StringSize returns the encoded size of a string.
func StringSize(s string) uint64 { func StringSize(s string) uint64 {
switch { switch n := len(s); n {
case len(s) == 0: case 0:
return 1 return 1
case len(s) == 1: case 1:
if s[0] <= 0x7f { if s[0] <= 0x7f {
return 1 return 1
} else { } else {
return 2 return 2
} }
default: default:
return uint64(headsize(uint64(len(s))) + len(s)) return uint64(headsize(uint64(n)) + n)
} }
} }
// BytesSize returns the encoded size of a byte slice. // BytesSize returns the encoded size of a byte slice.
func BytesSize(b []byte) uint64 { func BytesSize(b []byte) uint64 {
switch { switch n := len(b); n {
case len(b) == 0: case 0:
return 1 return 1
case len(b) == 1: case 1:
if b[0] <= 0x7f { if b[0] <= 0x7f {
return 1 return 1
} else { } else {
return 2 return 2
} }
default: default:
return uint64(headsize(uint64(len(b))) + len(b)) return uint64(headsize(uint64(n)) + n)
} }
} }
@ -105,18 +105,20 @@ func SplitUint64(b []byte) (x uint64, rest []byte, err error) {
if err != nil { if err != nil {
return 0, b, err return 0, b, err
} }
switch { switch n := len(content); n {
case len(content) == 0: case 0:
return 0, rest, nil return 0, rest, nil
case len(content) == 1: case 1:
if content[0] == 0 { if content[0] == 0 {
return 0, b, ErrCanonInt return 0, b, ErrCanonInt
} }
return uint64(content[0]), rest, nil return uint64(content[0]), rest, nil
case len(content) > 8:
return 0, b, errUintOverflow
default: default:
x, err = readSize(content, byte(len(content))) if n > 8 {
return 0, b, errUintOverflow
}
x, err = readSize(content, byte(n))
if err != nil { if err != nil {
return 0, b, ErrCanonInt return 0, b, ErrCanonInt
} }