Skip to content

Commit aa1a499

Browse files
cuinixzzzckck
authored andcommitted
refactor: use the built-in max/min to simplify the code (bnb-chain#3097)
1 parent 96345a2 commit aa1a499

File tree

3 files changed

+8
-32
lines changed

3 files changed

+8
-32
lines changed

common/bitutil/bitutil.go

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ func XORBytes(dst, a, b []byte) int {
2727
// fastXORBytes xors in bulk. It only works on architectures that support
2828
// unaligned read/writes.
2929
func fastXORBytes(dst, a, b []byte) int {
30-
n := len(a)
31-
if len(b) < n {
32-
n = len(b)
33-
}
30+
n := min(len(b), len(a))
3431
w := n / wordSize
3532
if w > 0 {
3633
dw := *(*[]uintptr)(unsafe.Pointer(&dst))
@@ -49,10 +46,7 @@ func fastXORBytes(dst, a, b []byte) int {
4946
// safeXORBytes xors one by one. It works on all architectures, independent if
5047
// it supports unaligned read/writes or not.
5148
func safeXORBytes(dst, a, b []byte) int {
52-
n := len(a)
53-
if len(b) < n {
54-
n = len(b)
55-
}
49+
n := min(len(b), len(a))
5650
for i := 0; i < n; i++ {
5751
dst[i] = a[i] ^ b[i]
5852
}
@@ -71,10 +65,7 @@ func ANDBytes(dst, a, b []byte) int {
7165
// fastANDBytes ands in bulk. It only works on architectures that support
7266
// unaligned read/writes.
7367
func fastANDBytes(dst, a, b []byte) int {
74-
n := len(a)
75-
if len(b) < n {
76-
n = len(b)
77-
}
68+
n := min(len(b), len(a))
7869
w := n / wordSize
7970
if w > 0 {
8071
dw := *(*[]uintptr)(unsafe.Pointer(&dst))
@@ -93,10 +84,7 @@ func fastANDBytes(dst, a, b []byte) int {
9384
// safeANDBytes ands one by one. It works on all architectures, independent if
9485
// it supports unaligned read/writes or not.
9586
func safeANDBytes(dst, a, b []byte) int {
96-
n := len(a)
97-
if len(b) < n {
98-
n = len(b)
99-
}
87+
n := min(len(b), len(a))
10088
for i := 0; i < n; i++ {
10189
dst[i] = a[i] & b[i]
10290
}
@@ -115,10 +103,7 @@ func ORBytes(dst, a, b []byte) int {
115103
// fastORBytes ors in bulk. It only works on architectures that support
116104
// unaligned read/writes.
117105
func fastORBytes(dst, a, b []byte) int {
118-
n := len(a)
119-
if len(b) < n {
120-
n = len(b)
121-
}
106+
n := min(len(b), len(a))
122107
w := n / wordSize
123108
if w > 0 {
124109
dw := *(*[]uintptr)(unsafe.Pointer(&dst))
@@ -137,10 +122,7 @@ func fastORBytes(dst, a, b []byte) int {
137122
// safeORBytes ors one by one. It works on all architectures, independent if
138123
// it supports unaligned read/writes or not.
139124
func safeORBytes(dst, a, b []byte) int {
140-
n := len(a)
141-
if len(b) < n {
142-
n = len(b)
143-
}
125+
n := min(len(b), len(a))
144126
for i := 0; i < n; i++ {
145127
dst[i] = a[i] | b[i]
146128
}

common/hexutil/hexutil.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,7 @@ func DecodeBig(input string) (*big.Int, error) {
147147
words := make([]big.Word, len(raw)/bigWordNibbles+1)
148148
end := len(raw)
149149
for i := range words {
150-
start := end - bigWordNibbles
151-
if start < 0 {
152-
start = 0
153-
}
150+
start := max(end-bigWordNibbles, 0)
154151
for ri := start; ri < end; ri++ {
155152
nib := decodeNibble(raw[ri])
156153
if nib == badNibble {

common/hexutil/json.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,7 @@ func (b *Big) UnmarshalText(input []byte) error {
179179
words := make([]big.Word, len(raw)/bigWordNibbles+1)
180180
end := len(raw)
181181
for i := range words {
182-
start := end - bigWordNibbles
183-
if start < 0 {
184-
start = 0
185-
}
182+
start := max(end-bigWordNibbles, 0)
186183
for ri := start; ri < end; ri++ {
187184
nib := decodeNibble(raw[ri])
188185
if nib == badNibble {

0 commit comments

Comments
 (0)