Skip to content

Commit

Permalink
Revert misguided text/collate usage
Browse files Browse the repository at this point in the history
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
  • Loading branch information
q3cpma committed Aug 31, 2024
1 parent c152029 commit 5f6eae2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ require (
github.com/mattn/go-runewidth v0.0.15
golang.org/x/sys v0.20.0
golang.org/x/term v0.20.0
golang.org/x/text v0.14.0
)

require (
github.com/gdamore/encoding v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
golang.org/x/text v0.14.0 // indirect
)
47 changes: 32 additions & 15 deletions nav.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
"time"

"github.com/djherbis/times"
"golang.org/x/text/collate"
"golang.org/x/text/language"
)

type linkState byte
Expand Down Expand Up @@ -249,24 +247,35 @@ func (dir *dir) sort() {
dir.files = filtered
}

collopts := []collate.Option{}
if dir.ignorecase {
collopts = append(collopts, collate.IgnoreCase)
normalizefun := func(s1, s2 string) (string, string) {
return s1, s2
}
if dir.ignoredia {
collopts = append(collopts, collate.IgnoreDiacritics)
}
if dir.sortby == naturalSort {
collopts = append(collopts, collate.Numeric)
if dir.ignorecase && dir.ignoredia {
normalizefun = func(s1, s2 string) (string, string) {
return removeDiacritics(strings.ToLower(s1)), removeDiacritics(strings.ToLower(s2))
}
} else if dir.ignorecase {
normalizefun = func(s1, s2 string) (string, string) {
return strings.ToLower(s1), strings.ToLower(s2)
}
} else if dir.ignoredia {
normalizefun = func(s1, s2 string) (string, string) {
return removeDiacritics(s1), removeDiacritics(s2)
}
}
coll := collate.New(language.Und, collopts...)

var lessfun func(i, j int) bool

switch dir.sortby {
case nameSort, naturalSort:
case naturalSort:
lessfun = func(i, j int) bool {
return coll.CompareString(dir.files[i].Name(), dir.files[j].Name()) == -1
s1, s2 := normalizefun(dir.files[i].Name(), dir.files[j].Name())
return naturalLess(s1, s2)
}
case nameSort:
lessfun = func(i, j int) bool {
s1, s2 := normalizefun(dir.files[i].Name(), dir.files[j].Name())
return s1 < s2
}
case sizeSort:
lessfun = func(i, j int) bool {
Expand All @@ -286,8 +295,16 @@ func (dir *dir) sort() {
}
case extSort:
lessfun = func(i, j int) bool {
cmp := coll.CompareString(dir.files[i].ext, dir.files[j].ext)
return cmp == -1 || cmp == 0 && coll.CompareString(dir.files[i].Name(), dir.files[j].Name()) == -1
ext1, ext2 := normalizefun(dir.files[i].ext, dir.files[j].ext)
extcmp := strings.Compare(ext1, ext2)
if extcmp == -1 {
return true
}
if extcmp == 0 {
name1, name2 := normalizefun(dir.files[i].Name(), dir.files[j].Name())
return name1 < name2
}
return false
}
}

Expand Down

0 comments on commit 5f6eae2

Please sign in to comment.