Skip to content

Commit 4fe482f

Browse files
knowmostcce
authored andcommitted
refactor: use the built-in max/min to simplify the code (algorand#6338)
Signed-off-by: knowmost <knowmost@outlook.com>
1 parent 1ba4c7a commit 4fe482f

File tree

5 files changed

+6
-20
lines changed

5 files changed

+6
-20
lines changed

crypto/statetrie/nibbles/nibbles.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ func ShiftLeft(nyb1 Nibbles, numNibbles int) Nibbles {
8383
// SharedPrefix returns a slice from nyb1 that contains the shared prefix
8484
// between nyb1 and nyb2
8585
func SharedPrefix(nyb1 Nibbles, nyb2 Nibbles) Nibbles {
86-
minLength := len(nyb1)
87-
if len(nyb2) < minLength {
88-
minLength = len(nyb2)
89-
}
86+
minLength := min(len(nyb2), len(nyb1))
9087
for i := 0; i < minLength; i++ {
9188
if nyb1[i] != nyb2[i] {
9289
return nyb1[:i]

network/limitlistener/rejectingLimitListener_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ func TestRejectingLimitListenerBasic(t *testing.T) {
2424
partitiontest.PartitionTest(t)
2525

2626
const limit = 5
27-
attempts := (maxOpenFiles() - limit) / 2
28-
if attempts > 256 { // maximum length of accept queue is 128 by default
29-
attempts = 256
30-
}
27+
// maximum length of accept queue is 128 by default
28+
attempts := min((maxOpenFiles()-limit)/2, 256)
3129

3230
l, err := net.Listen("tcp", "127.0.0.1:0")
3331
if err != nil {

tools/debug/carpenter/main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,7 @@ func outputTableFormat(out string, event logspec.Event, columns []string, colPos
305305
maxLen := len(out)
306306
for i := 0; i < rowCount; i++ {
307307
start := i * columnWidth
308-
end := start + columnWidth
309-
if end > maxLen {
310-
end = maxLen
311-
}
308+
end := min(start+columnWidth, maxLen)
312309
if start < len(out) {
313310
row := strings.TrimSpace(out[start:end])
314311
output := ""

util/bloom/bloom.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ func Optimal(numElements int, falsePositiveRate float64) (sizeBits int, numHashe
5050
m := -(n+0.5)*math.Log(p)/math.Pow(math.Log(2), 2) + 1
5151
k := -math.Log(p) / math.Log(2)
5252

53-
numHashes = uint32(math.Ceil(k))
54-
if numHashes > maxHashes {
55-
numHashes = maxHashes
56-
}
53+
numHashes = min(uint32(math.Ceil(k)), maxHashes)
5754

5855
return int(math.Ceil(m)), numHashes
5956
}

util/watchdogStreamReader.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,7 @@ func (r *watchdogStreamReader) Read(p []byte) (n int, err error) {
110110
}
111111
if len(r.stageBuffer) > 0 {
112112
// copy the data to the buffer p
113-
n = len(p)
114-
if n > len(r.stageBuffer) {
115-
n = len(r.stageBuffer)
116-
}
113+
n = min(len(p), len(r.stageBuffer))
117114
copy(p, r.stageBuffer)
118115
r.stageBuffer = r.stageBuffer[n:]
119116
}

0 commit comments

Comments
 (0)