commit 6e0cc7f2a9e45569c000b36817ddbdebe8f4ef7a
parent 2569d0a7551eeb1174be2b00983a50e1bd5caa59
Author: Jack Mordaunt <jackmordaunt.dev@gmail.com>
Date: Fri, 18 Sep 2026 16:09:35 -0400
icns: sort with the slices package
sort.Slice reaches for reflection and takes indices, so the comparisons read
around the slice rather than over its elements. The typed equivalents say the
same thing about the values themselves.
Diffstat:
2 files changed, 14 insertions(+), 17 deletions(-)
diff --git a/icns.go b/icns.go
@@ -1,11 +1,12 @@
package icns
import (
+ "cmp"
"errors"
"fmt"
"image"
"io"
- "sort"
+ "slices"
"sync"
"golang.org/x/image/draw"
@@ -93,15 +94,14 @@ func NewIconSetFrom(images map[Slot]image.Image, interp InterpolationFunction) (
}
// The largest artwork stands in for the slots left empty. Ties are broken
// by slot so the choice does not depend on map ordering.
- sort.Slice(slots, func(ii, jj int) bool {
- left, right := biggestSide(images[slots[ii]]), biggestSide(images[slots[jj]])
- if left != right {
- return left > right
+ slices.SortFunc(slots, func(a, b Slot) int {
+ if order := cmp.Compare(biggestSide(images[b]), biggestSide(images[a])); order != 0 {
+ return order
}
- if slots[ii].Points != slots[jj].Points {
- return slots[ii].Points > slots[jj].Points
+ if order := cmp.Compare(b.Points, a.Points); order != 0 {
+ return order
}
- return slots[ii].Scale > slots[jj].Scale
+ return cmp.Compare(b.Scale, a.Scale)
})
return newIconSet(images, images[slots[0]], interp)
}
diff --git a/reader.go b/reader.go
@@ -2,12 +2,12 @@ package icns
import (
"bytes"
+ "cmp"
"encoding/binary"
"fmt"
"image"
"io"
"slices"
- "sort"
)
var jpeg2000header = []byte{0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20}
@@ -26,8 +26,8 @@ func NewDecoder(r io.Reader) (*Decoder, error) {
return nil, err
}
// Largest first, keeping file order between icons of equal size.
- sort.SliceStable(entries, func(ii, jj int) bool {
- return entries[ii].Size > entries[jj].Size
+ slices.SortStableFunc(entries, func(a, b Entry) int {
+ return cmp.Compare(b.Size, a.Size)
})
return &Decoder{entries: entries}, nil
}
@@ -123,12 +123,9 @@ func DecodeAll(r io.Reader) (images []image.Image, err error) {
}
// An element may hold an image of a size other than the one its type
// names, so order by what was actually decoded.
- sort.SliceStable(images, func(ii, jj int) bool {
- var (
- left = images[ii].Bounds().Size()
- right = images[jj].Bounds().Size()
- )
- return (left.X + left.Y) > (right.X + right.Y)
+ slices.SortStableFunc(images, func(a, b image.Image) int {
+ left, right := a.Bounds().Size(), b.Bounds().Size()
+ return cmp.Compare(right.X+right.Y, left.X+left.Y)
})
return images, nil
}