Skip to content

Commit 5f6eae2

Browse files
committed
Revert misguided text/collate usage
And add the following optimizations: * Hoist the branching out of normalize() * In extSort, normalize the names only when extensions are equal * In extSort, use strings.Compare that does a proper 3-way comparison in Go 1.23
1 parent c152029 commit 5f6eae2

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ require (
88
github.com/mattn/go-runewidth v0.0.15
99
golang.org/x/sys v0.20.0
1010
golang.org/x/term v0.20.0
11-
golang.org/x/text v0.14.0
1211
)
1312

1413
require (
1514
github.com/gdamore/encoding v1.0.0 // indirect
1615
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
1716
github.com/rivo/uniseg v0.4.3 // indirect
17+
golang.org/x/text v0.14.0 // indirect
1818
)

nav.go

+32-15
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
"time"
1717

1818
"github.com/djherbis/times"
19-
"golang.org/x/text/collate"
20-
"golang.org/x/text/language"
2119
)
2220

2321
type linkState byte
@@ -249,24 +247,35 @@ func (dir *dir) sort() {
249247
dir.files = filtered
250248
}
251249

252-
collopts := []collate.Option{}
253-
if dir.ignorecase {
254-
collopts = append(collopts, collate.IgnoreCase)
250+
normalizefun := func(s1, s2 string) (string, string) {
251+
return s1, s2
255252
}
256-
if dir.ignoredia {
257-
collopts = append(collopts, collate.IgnoreDiacritics)
258-
}
259-
if dir.sortby == naturalSort {
260-
collopts = append(collopts, collate.Numeric)
253+
if dir.ignorecase && dir.ignoredia {
254+
normalizefun = func(s1, s2 string) (string, string) {
255+
return removeDiacritics(strings.ToLower(s1)), removeDiacritics(strings.ToLower(s2))
256+
}
257+
} else if dir.ignorecase {
258+
normalizefun = func(s1, s2 string) (string, string) {
259+
return strings.ToLower(s1), strings.ToLower(s2)
260+
}
261+
} else if dir.ignoredia {
262+
normalizefun = func(s1, s2 string) (string, string) {
263+
return removeDiacritics(s1), removeDiacritics(s2)
264+
}
261265
}
262-
coll := collate.New(language.Und, collopts...)
263266

264267
var lessfun func(i, j int) bool
265268

266269
switch dir.sortby {
267-
case nameSort, naturalSort:
270+
case naturalSort:
268271
lessfun = func(i, j int) bool {
269-
return coll.CompareString(dir.files[i].Name(), dir.files[j].Name()) == -1
272+
s1, s2 := normalizefun(dir.files[i].Name(), dir.files[j].Name())
273+
return naturalLess(s1, s2)
274+
}
275+
case nameSort:
276+
lessfun = func(i, j int) bool {
277+
s1, s2 := normalizefun(dir.files[i].Name(), dir.files[j].Name())
278+
return s1 < s2
270279
}
271280
case sizeSort:
272281
lessfun = func(i, j int) bool {
@@ -286,8 +295,16 @@ func (dir *dir) sort() {
286295
}
287296
case extSort:
288297
lessfun = func(i, j int) bool {
289-
cmp := coll.CompareString(dir.files[i].ext, dir.files[j].ext)
290-
return cmp == -1 || cmp == 0 && coll.CompareString(dir.files[i].Name(), dir.files[j].Name()) == -1
298+
ext1, ext2 := normalizefun(dir.files[i].ext, dir.files[j].ext)
299+
extcmp := strings.Compare(ext1, ext2)
300+
if extcmp == -1 {
301+
return true
302+
}
303+
if extcmp == 0 {
304+
name1, name2 := normalizefun(dir.files[i].Name(), dir.files[j].Name())
305+
return name1 < name2
306+
}
307+
return false
291308
}
292309
}
293310

0 commit comments

Comments
 (0)