>> write path sorter
> correct error in test > rename to sortedPaths
This commit is contained in:
parent
f517f61c55
commit
52c7f8bce6
2 changed files with 24 additions and 3 deletions
21
main.go
21
main.go
|
|
@ -23,6 +23,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
fpath "path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
|
@ -70,6 +71,26 @@ var FAT_RUNESET = runeset{
|
|||
}
|
||||
|
||||
/// MAIN FUNCTIONS
|
||||
/**
|
||||
* Sort a list of paths so that all files are listed before the directories
|
||||
* that contain them. Files in the same directory are listed in alphanumeric
|
||||
* order.
|
||||
*/
|
||||
func sortedPaths(paths []string) []string {
|
||||
sorter := func(a, b string) int {
|
||||
sep := string(fpath.Separator)
|
||||
aNumComponents := len(strings.Split(a, sep))
|
||||
bNumComponents := len(strings.Split(b, sep))
|
||||
if aNumComponents == bNumComponents {
|
||||
return strings.Compare(fpath.Base(a), fpath.Base(b))
|
||||
}
|
||||
return bNumComponents - aNumComponents
|
||||
}
|
||||
result := slices.Clone(paths)
|
||||
slices.SortFunc(result, sorter)
|
||||
return result
|
||||
}
|
||||
|
||||
func isFatValid(r rune) bool {
|
||||
for _, runeRange := range FAT_RUNESET {
|
||||
if runeRange[0] <= r && r <= runeRange[1] {
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ func TestRestrictRuneset(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestSortPaths(t *testing.T) {
|
||||
func TestSortedPaths(t *testing.T) {
|
||||
paths := []string{
|
||||
"./testdata/Tawnee: The Game",
|
||||
"./testdata/birds:/pengiuns?",
|
||||
|
|
@ -83,10 +83,10 @@ func TestSortPaths(t *testing.T) {
|
|||
"./testdata/birds:/pengiuns?/emperor",
|
||||
"./testdata/birds:/pengiuns?/pingu?",
|
||||
"./testdata/birds:/pengiuns?",
|
||||
"./testdata/birds:",
|
||||
"./testdata/Tawnee: The Game",
|
||||
"./testdata/birds:",
|
||||
}
|
||||
have := sortPaths(paths)
|
||||
have := sortedPaths(paths)
|
||||
if len(have) > len(want) {
|
||||
t.Fatal("Sorted list is bigger than original")
|
||||
} else if len(have) < len(want) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue