Skip to content

Commit 1979e41

Browse files
authored
Merge pull request #2347 from sunnyraindy/develop
refactor: use the built-in max to simplify the code
2 parents 25d16dd + 2d13ff6 commit 1979e41

7 files changed

Lines changed: 14 additions & 57 deletions

File tree

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/fdlimit/fdlimit_unix.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ func Raise(max uint64) (uint64, error) {
3131
return 0, err
3232
}
3333
// Try to update the limit to the max allowance
34-
limit.Cur = limit.Max
35-
if limit.Cur > max {
36-
limit.Cur = max
37-
}
34+
limit.Cur = min(limit.Max, max)
3835
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
3936
return 0, err
4037
}

common/hexutil/hexutil.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import (
3838
"strconv"
3939
)
4040

41-
4241
// Errors
4342
var (
4443
ErrEmptyString = &decError{"empty hex string"}
@@ -147,10 +146,7 @@ func DecodeBig(input string) (*big.Int, error) {
147146
words := make([]big.Word, len(raw)/bigWordNibbles+1)
148147
end := len(raw)
149148
for i := range words {
150-
start := end - bigWordNibbles
151-
if start < 0 {
152-
start = 0
153-
}
149+
start := max(end-bigWordNibbles, 0)
154150
for ri := start; ri < end; ri++ {
155151
nib := decodeNibble(raw[ri])
156152
if nib == badNibble {

common/hexutil/json.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,7 @@ func (b *Big) UnmarshalText(input []byte) error {
176176
words := make([]big.Word, len(raw)/bigWordNibbles+1)
177177
end := len(raw)
178178
for i := range words {
179-
start := end - bigWordNibbles
180-
if start < 0 {
181-
start = 0
182-
}
179+
start := max(end-bigWordNibbles, 0)
183180
for ri := start; ri < end; ri++ {
184181
nib := decodeNibble(raw[ri])
185182
if nib == badNibble {

common/json/scanner_test.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,7 @@ func TestIndentErrors(t *testing.T) {
204204
func diff(t *testing.T, a, b []byte) {
205205
for i := 0; ; i++ {
206206
if i >= len(a) || i >= len(b) || a[i] != b[i] {
207-
j := i - 10
208-
if j < 0 {
209-
j = 0
210-
}
207+
j := max(i-10, 0)
211208
t.Errorf("diverge at %d: «%s» vs «%s»", i, trim(a[j:]), trim(b[j:]))
212209
return
213210
}
@@ -271,10 +268,7 @@ func genString(stddev float64) string {
271268
}
272269

273270
func genArray(n int) []interface{} {
274-
f := int(math.Abs(rand.NormFloat64()) * math.Min(10, float64(n/2)))
275-
if f > n {
276-
f = n
277-
}
271+
f := min(int(math.Abs(rand.NormFloat64())*math.Min(10, float64(n/2))), n)
278272
if f < 1 {
279273
f = 1
280274
}
@@ -286,10 +280,7 @@ func genArray(n int) []interface{} {
286280
}
287281

288282
func genMap(n int) map[string]interface{} {
289-
f := int(math.Abs(rand.NormFloat64()) * math.Min(10, float64(n/2)))
290-
if f > n {
291-
f = n
292-
}
283+
f := min(int(math.Abs(rand.NormFloat64())*math.Min(10, float64(n/2))), n)
293284
if n > 0 && f == 0 {
294285
f = 1
295286
}

p2p/netutil/net.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,7 @@ func (s *DistinctNetSet) key(ip net.IP) net.IP {
280280
if ip4 := ip.To4(); ip4 != nil {
281281
typ, ip = '4', ip4
282282
}
283-
bits := s.Subnet
284-
if bits > uint(len(ip)*8) {
285-
bits = uint(len(ip) * 8)
286-
}
283+
bits := min(s.Subnet, uint(len(ip)*8))
287284
// Encode the prefix into s.buf.
288285
nb := int(bits / 8)
289286
mask := ^byte(0xFF >> (bits % 8))

rlp/decode.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,7 @@ func decodeSliceElems(s *Stream, val reflect.Value, elemdec decoder) error {
289289
for ; ; i++ {
290290
// grow slice if necessary
291291
if i >= val.Cap() {
292-
newcap := val.Cap() + val.Cap()/2
293-
if newcap < 4 {
294-
newcap = 4
295-
}
292+
newcap := max(val.Cap()+val.Cap()/2, 4)
296293
newv := reflect.MakeSlice(val.Type(), val.Len(), newcap)
297294
reflect.Copy(newv, val)
298295
val.Set(newv)

0 commit comments

Comments
 (0)