diff --git a/misc.go b/misc.go index 52b4334d..02fb2a5b 100644 --- a/misc.go +++ b/misc.go @@ -2,12 +2,11 @@ package main import ( "fmt" - "log" "path/filepath" + "regexp" "strconv" "strings" "unicode" - "unicode/utf8" "github.com/mattn/go-runewidth" ) @@ -176,53 +175,32 @@ func humanize(size int64) string { return "" } -// This function extracts numbers from a string and returns with the rest. -// It is used for numeric sorting of files when the file name consists of -// both digits and letters. -// -// For instance if your input is 'foo123bar456' you get a slice of number -// consisting of elements '123' and '456' and rest of the string as a slice -// consisting of elements 'foo' and 'bar'. The last return argument denotes -// whether or not the first partition is a number. -func extractNums(s string) (nums []int, rest []string, numFirst bool) { - var buf []rune +// This regexp is used to partition a given string as numbers and non-numbers. +// For instance, if your input is 'foo123bar456' you get a slice of 'foo', +// '123', 'bar', and '456'. This is useful for natural sorting which takes into +// account values of numbers within strings. +var rePart = regexp.MustCompile(`\d+|\D+`) - r, _ := utf8.DecodeRuneInString(s) - digit := unicode.IsDigit(r) - numFirst = digit +func naturalLess(s1, s2 string) bool { + parts1 := rePart.FindAllString(s1, -1) + parts2 := rePart.FindAllString(s2, -1) - for _, c := range s { - if unicode.IsDigit(c) == digit { - buf = append(buf, c) + for i := 0; i < len(parts1) && i < len(parts2); i++ { + if parts1[i] == parts2[i] { continue } - if digit { - i, err := strconv.Atoi(string(buf)) - if err != nil { - log.Printf("extracting numbers: %s", err) - } - nums = append(nums, i) - } else { - rest = append(rest, string(buf)) + num1, err1 := strconv.Atoi(parts1[i]) + num2, err2 := strconv.Atoi(parts2[i]) + + if err1 == nil && err2 == nil { + return num1 < num2 } - buf = nil - buf = append(buf, c) - digit = !digit + return parts1[i] < parts2[i] } - if digit { - i, err := strconv.Atoi(string(buf)) - if err != nil { - log.Printf("extracting numbers: %s", err) - } - nums = append(nums, i) - } else { - rest = append(rest, string(buf)) - } - - return + return len(parts1) < len(parts2) } func min(a, b int) int { diff --git a/misc_test.go b/misc_test.go index cc363c4b..f5e5ce3f 100644 --- a/misc_test.go +++ b/misc_test.go @@ -201,32 +201,29 @@ func TestHumanize(t *testing.T) { } } -func TestExtractNums(t *testing.T) { - names := []struct { - s string - nums []int - rest []string - numFirst bool +func TestNaturalLess(t *testing.T) { + tests := []struct { + s1 string + s2 string + expected bool }{ - {"foo123bar456", []int{123, 456}, []string{"foo", "bar"}, false}, - {"123foo456bar", []int{123, 456}, []string{"foo", "bar"}, true}, - {"a-1-1", []int{1, 1}, []string{"a-", "-"}, false}, - {"a-1-1.", []int{1, 1}, []string{"a-", "-", "."}, false}, - {"a-1-1.txt", []int{1, 1}, []string{"a-", "-", ".txt"}, false}, - {"a-1-10.txt", []int{1, 10}, []string{"a-", "-", ".txt"}, false}, - {"a-10-1.txt", []int{10, 1}, []string{"a-", "-", ".txt"}, false}, + {"foo", "bar", false}, + {"bar", "baz", true}, + {"foo", "123", false}, + {"foo1", "foobar", true}, + {"foo1", "foo10", true}, + {"foo2", "foo10", true}, + {"foo1", "foo10bar", true}, + {"foo2", "foo10bar", true}, + {"foo1bar", "foo10bar", true}, + {"foo2bar", "foo10bar", true}, + {"foo1bar", "foo10", true}, + {"foo2bar", "foo10", true}, } - for _, name := range names { - nums, rest, numFirst := extractNums(name.s) - if !reflect.DeepEqual(nums, name.nums) { - t.Errorf("at input '%s' expected '%v' but got '%v'", name.s, name.nums, nums) - } - if !reflect.DeepEqual(rest, name.rest) { - t.Errorf("at input '%s' expected '%v' but got '%v'", name.s, name.rest, rest) - } - if !numFirst == name.numFirst { - t.Errorf("at input '%s' expected '%t' but got '%t'", name.s, name.numFirst, numFirst) + for _, test := range tests { + if b := naturalLess(test.s1, test.s2); b != test.expected { + t.Errorf("at input '%s' and '%s' expected '%t' but got '%t'", test.s1, test.s2, test.expected, b) } } } diff --git a/nav.go b/nav.go index 3de8ab11..d25af6d9 100644 --- a/nav.go +++ b/nav.go @@ -48,31 +48,7 @@ func getFilesSorted(path string) []*File { switch gOpts.sortby { case "natural": sortFilesStable(fi, func(i, j int) bool { - nums1, rest1, numFirst1 := extractNums(fi[i].Name()) - nums2, rest2, numFirst2 := extractNums(fi[j].Name()) - - if numFirst1 != numFirst2 { - return strings.ToLower(fi[i].Name()) < strings.ToLower(fi[j].Name()) - } - - if numFirst1 { - if nums1[0] != nums2[0] { - return nums1[0] < nums2[0] - } - nums1 = nums1[1:] - nums2 = nums2[1:] - } - - for k := 0; k < len(nums1) && k < len(nums2); k++ { - if rest1[k] != rest2[k] { - return strings.ToLower(fi[i].Name()) < strings.ToLower(fi[j].Name()) - } - if nums1[k] != nums2[k] { - return nums1[k] < nums2[k] - } - } - - return strings.ToLower(fi[i].Name()) < strings.ToLower(fi[j].Name()) + return naturalLess(strings.ToLower(fi[i].Name()), strings.ToLower(fi[j].Name())) }) case "name": sortFilesStable(fi, func(i, j int) bool {