Skip to content

Commit c8f195e

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 79cb614 commit c8f195e

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ require (
1515
github.com/gdamore/encoding v1.0.0 // indirect
1616
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
1717
github.com/rivo/uniseg v0.4.3 // indirect
18+
golang.org/x/text v0.14.0 // indirect
1819
)

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
@@ -247,24 +245,35 @@ func (dir *dir) sort() {
247245
dir.files = filtered
248246
}
249247

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

262265
var lessfun func(i, j int) bool
263266

264267
switch dir.sortby {
265-
case nameSort, naturalSort:
268+
case naturalSort:
266269
lessfun = func(i, j int) bool {
267-
return coll.CompareString(dir.files[i].Name(), dir.files[j].Name()) == -1
270+
s1, s2 := normalizefun(dir.files[i].Name(), dir.files[j].Name())
271+
return naturalLess(s1, s2)
272+
}
273+
case nameSort:
274+
lessfun = func(i, j int) bool {
275+
s1, s2 := normalizefun(dir.files[i].Name(), dir.files[j].Name())
276+
return s1 < s2
268277
}
269278
case sizeSort:
270279
lessfun = func(i, j int) bool {
@@ -284,8 +293,16 @@ func (dir *dir) sort() {
284293
}
285294
case extSort:
286295
lessfun = func(i, j int) bool {
287-
cmp := coll.CompareString(dir.files[i].ext, dir.files[j].ext)
288-
return cmp == -1 || cmp == 0 && coll.CompareString(dir.files[i].Name(), dir.files[j].Name()) == -1
296+
ext1, ext2 := normalizefun(dir.files[i].ext, dir.files[j].ext)
297+
extcmp := strings.Compare(ext1, ext2)
298+
if extcmp == -1 {
299+
return true
300+
}
301+
if extcmp == 0 {
302+
name1, name2 := normalizefun(dir.files[i].Name(), dir.files[j].Name())
303+
return name1 < name2
304+
}
305+
return false
289306
}
290307
}
291308

0 commit comments

Comments
 (0)