From d60e87dac8904d8181500f8430790ca807189e25 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 24 Feb 2026 15:52:06 +0000 Subject: [PATCH] chore: use min()/max() builtins (Go 1.21+) Co-Authored-By: Claude Opus 4.6 --- pkg/help/search.go | 19 +++--------- pkg/io/sigil/crypto_sigil.go | 10 ++---- pkg/lab/handler/chart.go | 59 ++++++++---------------------------- pkg/lab/handler/web.go | 5 +-- 4 files changed, 20 insertions(+), 73 deletions(-) diff --git a/pkg/help/search.go b/pkg/help/search.go index c718a2b..bb2a24b 100644 --- a/pkg/help/search.go +++ b/pkg/help/search.go @@ -286,24 +286,15 @@ func extractSnippet(content string, res []*regexp.Regexp) string { if matchPos == -1 { // No match found, use start of content start = 0 - end = snippetLen - if end > runeLen { - end = runeLen - } + end = min(snippetLen, runeLen) } else { // Convert byte position to rune position matchRunePos := len([]rune(content[:matchPos])) // Extract snippet around match (rune-based) - start = matchRunePos - 50 - if start < 0 { - start = 0 - } + start = max(matchRunePos-50, 0) - end = start + snippetLen - if end > runeLen { - end = runeLen - } + end = min(start+snippetLen, runeLen) } snippet := string(runes[start:end]) @@ -371,9 +362,7 @@ func highlight(text string, res []*regexp.Regexp) string { curr := matches[0] for i := 1; i < len(matches); i++ { if matches[i].start <= curr.end { - if matches[i].end > curr.end { - curr.end = matches[i].end - } + curr.end = max(curr.end, matches[i].end) } else { merged = append(merged, curr) curr = matches[i] diff --git a/pkg/io/sigil/crypto_sigil.go b/pkg/io/sigil/crypto_sigil.go index 98c25cc..8bacd44 100644 --- a/pkg/io/sigil/crypto_sigil.go +++ b/pkg/io/sigil/crypto_sigil.go @@ -103,10 +103,7 @@ func (x *XORObfuscator) deriveKeyStream(entropy []byte, length int) []byte { h.Write(blockBytes[:]) block := h.Sum(nil) - copyLen := len(block) - if offset+copyLen > length { - copyLen = length - offset - } + copyLen := min(len(block), length-offset) copy(stream[offset:], block[:copyLen]) offset += copyLen blockNum++ @@ -222,10 +219,7 @@ func (s *ShuffleMaskObfuscator) deriveMask(entropy []byte, length int) []byte { h.Write(blockBytes[:]) block := h.Sum(nil) - copyLen := len(block) - if offset+copyLen > length { - copyLen = length - offset - } + copyLen := min(len(block), length-offset) copy(mask[offset:], block[:copyLen]) offset += copyLen blockNum++ diff --git a/pkg/lab/handler/chart.go b/pkg/lab/handler/chart.go index 5e179ab..abda97f 100644 --- a/pkg/lab/handler/chart.go +++ b/pkg/lab/handler/chart.go @@ -62,25 +62,15 @@ func LossChart(points []lab.LossPoint) template.HTML { yMin, yMax := allPts[0].Loss, allPts[0].Loss for _, p := range allPts { x := float64(p.Iteration) - if x < xMin { - xMin = x - } - if x > xMax { - xMax = x - } - if p.Loss < yMin { - yMin = p.Loss - } - if p.Loss > yMax { - yMax = p.Loss - } + xMin = min(xMin, x) + xMax = max(xMax, x) + yMin = min(yMin, p.Loss) + yMax = max(yMax, p.Loss) } // Add padding to Y range. yRange := yMax - yMin - if yRange < 0.1 { - yRange = 0.1 - } + yRange = max(yRange, 0.1) yMin = yMin - yRange*0.1 yMax = yMax + yRange*0.1 if xMax == xMin { @@ -104,13 +94,7 @@ func LossChart(points []lab.LossPoint) template.HTML { } // X axis labels. - nGridX := 6 - if int(xMax-xMin) < nGridX { - nGridX = int(xMax - xMin) - } - if nGridX < 1 { - nGridX = 1 - } + nGridX := max(min(6, int(xMax-xMin)), 1) for i := 0; i <= nGridX; i++ { xVal := xMin + float64(i)*(xMax-xMin)/float64(nGridX) x := scaleX(xVal) @@ -534,21 +518,14 @@ func DomainChart(stats []lab.DomainStat) template.HTML { if len(stats) == 0 { return "" } - limit := 25 - if len(stats) < limit { - limit = len(stats) - } + limit := min(25, len(stats)) items := stats[:limit] maxCount := 0 for _, d := range items { - if d.Count > maxCount { - maxCount = d.Count - } - } - if maxCount == 0 { - maxCount = 1 + maxCount = max(maxCount, d.Count) } + maxCount = max(maxCount, 1) barH := 18 gap := 4 @@ -563,10 +540,7 @@ func DomainChart(stats []lab.DomainStat) template.HTML { for i, d := range items { y := i*(barH+gap) + 5 - barW := int(float64(d.Count) / float64(maxCount) * float64(barAreaW)) - if barW < 2 { - barW = 2 - } + barW := max(int(float64(d.Count)/float64(maxCount)*float64(barAreaW)), 2) fmt.Fprintf(&b, `%s`, labelW-8, y+barH/2, template.HTMLEscapeString(d.Domain)) fmt.Fprintf(&b, ``, @@ -587,13 +561,9 @@ func VoiceChart(stats []lab.VoiceStat) template.HTML { maxCount := 0 for _, v := range stats { - if v.Count > maxCount { - maxCount = v.Count - } - } - if maxCount == 0 { - maxCount = 1 + maxCount = max(maxCount, v.Count) } + maxCount = max(maxCount, 1) barW := 50 gap := 8 @@ -609,10 +579,7 @@ func VoiceChart(stats []lab.VoiceStat) template.HTML { for i, v := range stats { x := i*(barW+gap) + gap + 5 - barH := int(float64(v.Count) / float64(maxCount) * float64(chartHeight)) - if barH < 2 { - barH = 2 - } + barH := max(int(float64(v.Count)/float64(maxCount)*float64(chartHeight)), 2) y := topPad + chartHeight - barH fmt.Fprintf(&b, ``, diff --git a/pkg/lab/handler/web.go b/pkg/lab/handler/web.go index 146c560..b4b9d3f 100644 --- a/pkg/lab/handler/web.go +++ b/pkg/lab/handler/web.go @@ -73,10 +73,7 @@ func NewWebHandler(s *lab.Store) *WebHandler { if cores <= 0 { return "0" } - pct := load / float64(cores) * 100 - if pct > 100 { - pct = 100 - } + pct := min(load/float64(cores)*100, 100) return fmt.Sprintf("%.0f", pct) }, "fmtGB": func(v float64) string {