chore: use min()/max() builtins (Go 1.21+)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
ff530d9898
commit
d60e87dac8
4 changed files with 20 additions and 73 deletions
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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++
|
||||
|
|
|
|||
|
|
@ -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, `<text x="%d" y="%d" fill="var(--muted)" font-size="11" text-anchor="end" dominant-baseline="middle">%s</text>`,
|
||||
labelW-8, y+barH/2, template.HTMLEscapeString(d.Domain))
|
||||
fmt.Fprintf(&b, `<rect x="%d" y="%d" width="%d" height="%d" fill="var(--accent)" rx="2" opacity="0.8"/>`,
|
||||
|
|
@ -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, `<rect x="%d" y="%d" width="%d" height="%d" fill="var(--green)" rx="2" opacity="0.7"/>`,
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue