Replace sort.Sort(ByOrder(...)) with slices.SortFunc
CodeQL / Analyze (go) (push) Has been cancelled
build / build (push) Has been cancelled
Test fzf on macOS / build (push) Has been cancelled

This commit is contained in:
Junegunn Choi
2026-04-12 10:43:47 +09:00
parent 1b0d448c6e
commit bd4a18d0a9
3 changed files with 22 additions and 20 deletions
+17 -17
View File
@@ -2,6 +2,7 @@ package fzf
import ( import (
"math" "math"
"slices"
"sort" "sort"
"unicode" "unicode"
@@ -30,7 +31,7 @@ type Result struct {
func buildResult(item *Item, offsets []Offset, score int) Result { func buildResult(item *Item, offsets []Offset, score int) Result {
if len(offsets) > 1 { if len(offsets) > 1 {
sort.Sort(ByOrder(offsets)) slices.SortFunc(offsets, compareOffsets)
} }
minBegin := math.MaxUint16 minBegin := math.MaxUint16
@@ -187,7 +188,7 @@ func (result *Result) colorOffsets(matchOffsets []Offset, nthOffsets []Offset, t
} }
} }
// sort.Sort(ByOrder(offsets)) // slices.SortFunc(offsets, compareOffsets)
// Merge offsets // Merge offsets
// ------------ ---- -- ---- // ------------ ---- -- ----
@@ -297,21 +298,20 @@ func (result *Result) colorOffsets(matchOffsets []Offset, nthOffsets []Offset, t
return colors return colors
} }
// ByOrder is for sorting substring offsets func compareOffsets(a, b Offset) int {
type ByOrder []Offset if a[0] < b[0] {
return -1
func (a ByOrder) Len() int { }
return len(a) if a[0] > b[0] {
} return 1
}
func (a ByOrder) Swap(i, j int) { if a[1] < b[1] {
a[i], a[j] = a[j], a[i] return -1
} }
if a[1] > b[1] {
func (a ByOrder) Less(i, j int) bool { return 1
ioff := a[i] }
joff := a[j] return 0
return (ioff[0] < joff[0]) || (ioff[0] == joff[0]) && (ioff[1] <= joff[1])
} }
// ByRelevance is for sorting Items // ByRelevance is for sorting Items
+2 -1
View File
@@ -3,6 +3,7 @@ package fzf
import ( import (
"math" "math"
"math/rand" "math/rand"
"slices"
"sort" "sort"
"testing" "testing"
@@ -19,7 +20,7 @@ func TestOffsetSort(t *testing.T) {
offsets := []Offset{ offsets := []Offset{
{3, 5}, {2, 7}, {3, 5}, {2, 7},
{1, 3}, {2, 9}} {1, 3}, {2, 9}}
sort.Sort(ByOrder(offsets)) slices.SortFunc(offsets, compareOffsets)
if offsets[0][0] != 1 || offsets[0][1] != 3 || if offsets[0][0] != 1 || offsets[0][1] != 3 ||
offsets[1][0] != 2 || offsets[1][1] != 7 || offsets[1][0] != 2 || offsets[1][1] != 7 ||
+3 -2
View File
@@ -13,6 +13,7 @@ import (
"os/exec" "os/exec"
"os/signal" "os/signal"
"regexp" "regexp"
"slices"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@@ -3751,7 +3752,7 @@ func (t *Terminal) printHighlighted(result Result, colBase tui.ColorPair, colMat
offset := Offset{int32(p), int32(p + w)} offset := Offset{int32(p), int32(p + w)}
charOffsets[idx] = offset charOffsets[idx] = offset
} }
sort.Sort(ByOrder(charOffsets)) slices.SortFunc(charOffsets, compareOffsets)
} }
// When postTask is nil, we're printing header lines. No need to care about nth. // When postTask is nil, we're printing header lines. No need to care about nth.
@@ -3788,7 +3789,7 @@ func (t *Terminal) printHighlighted(result Result, colBase tui.ColorPair, colMat
end := start + int32(length) end := start + int32(length)
nthOffsets[i] = Offset{int32(start), int32(end)} nthOffsets[i] = Offset{int32(start), int32(end)}
} }
sort.Sort(ByOrder(nthOffsets)) slices.SortFunc(nthOffsets, compareOffsets)
} }
} }
allOffsets := result.colorOffsets(charOffsets, nthOffsets, t.theme, colBase, colMatch, t.nthAttr, nthOverlay, hidden) allOffsets := result.colorOffsets(charOffsets, nthOffsets, t.theme, colBase, colMatch, t.nthAttr, nthOverlay, hidden)