Skip to content

Commit 2058221

Browse files
authored
Merge pull request #2061 from CortexFoundation/dev
rlp: no need to repeat called len method
2 parents 5262545 + 66b62ea commit 2058221

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

rlp/raw.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,33 @@ var rawValueType = reflect.TypeOf(RawValue{})
3030

3131
// StringSize returns the encoded size of a string.
3232
func StringSize(s string) uint64 {
33-
switch {
34-
case len(s) == 0:
33+
switch n := len(s); n {
34+
case 0:
3535
return 1
36-
case len(s) == 1:
36+
case 1:
3737
if s[0] <= 0x7f {
3838
return 1
3939
} else {
4040
return 2
4141
}
4242
default:
43-
return uint64(headsize(uint64(len(s))) + len(s))
43+
return uint64(headsize(uint64(n)) + n)
4444
}
4545
}
4646

4747
// BytesSize returns the encoded size of a byte slice.
4848
func BytesSize(b []byte) uint64 {
49-
switch {
50-
case len(b) == 0:
49+
switch n := len(b); n {
50+
case 0:
5151
return 1
52-
case len(b) == 1:
52+
case 1:
5353
if b[0] <= 0x7f {
5454
return 1
5555
} else {
5656
return 2
5757
}
5858
default:
59-
return uint64(headsize(uint64(len(b))) + len(b))
59+
return uint64(headsize(uint64(n)) + n)
6060
}
6161
}
6262

@@ -105,18 +105,20 @@ func SplitUint64(b []byte) (x uint64, rest []byte, err error) {
105105
if err != nil {
106106
return 0, b, err
107107
}
108-
switch {
109-
case len(content) == 0:
108+
switch n := len(content); n {
109+
case 0:
110110
return 0, rest, nil
111-
case len(content) == 1:
111+
case 1:
112112
if content[0] == 0 {
113113
return 0, b, ErrCanonInt
114114
}
115115
return uint64(content[0]), rest, nil
116-
case len(content) > 8:
117-
return 0, b, errUintOverflow
118116
default:
119-
x, err = readSize(content, byte(len(content)))
117+
if n > 8 {
118+
return 0, b, errUintOverflow
119+
}
120+
121+
x, err = readSize(content, byte(n))
120122
if err != nil {
121123
return 0, b, ErrCanonInt
122124
}

0 commit comments

Comments
 (0)