File: sort.go

package info (click to toggle)
golang-github-a8m-tree 0.0~git20240104.2c8764a-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 168 kB
  • sloc: sh: 23; makefile: 5
file content (116 lines) | stat: -rw-r--r-- 3,174 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package tree

import "os"

func (n Nodes) Len() int      { return len(n) }
func (n Nodes) Swap(i, j int) { n[i], n[j] = n[j], n[i] }

type ByFunc struct {
	Nodes
	Fn SortFunc
}

func (b ByFunc) Less(i, j int) bool {
	return b.Fn(b.Nodes[i].FileInfo, b.Nodes[j].FileInfo)
}

type SortFunc func(f1, f2 os.FileInfo) bool

func ModSort(f1, f2 os.FileInfo) bool {
	// This ensures any nil os.FileInfos sort at the end
	if f1 == nil || f2 == nil {
		return f2 == nil
	}
	return f1.ModTime().Before(f2.ModTime())
}

func DirSort(f1, f2 os.FileInfo) bool {
	if f1 == nil || f2 == nil {
		return f2 == nil
	}
	return f1.IsDir() && !f2.IsDir()
}

func SizeSort(f1, f2 os.FileInfo) bool {
	if f1 == nil || f2 == nil {
		return f2 == nil
	}
	return f1.Size() < f2.Size()
}

func NameSort(f1, f2 os.FileInfo) bool {
	if f1 == nil || f2 == nil {
		return f2 == nil
	}
	return f1.Name() < f2.Name()
}

func VerSort(f1, f2 os.FileInfo) bool {
	if f1 == nil || f2 == nil {
		return f2 == nil
	}
	return NaturalLess(f1.Name(), f2.Name())
}

func isdigit(b byte) bool { return '0' <= b && b <= '9' }

// NaturalLess compares two strings using natural ordering. This means that e.g.
// "abc2" < "abc12".
//
// Non-digit sequences and numbers are compared separately. The former are
// compared bytewise, while the latter are compared numerically (except that
// the number of leading zeros is used as a tie-breaker, so e.g. "2" < "02")
//
// Limitation: only ASCII digits (0-9) are considered.
// Code taken from:
// https://github.com/fvbommel/util/blob/master/sortorder/natsort.go
func NaturalLess(str1, str2 string) bool {
	idx1, idx2 := 0, 0
	for idx1 < len(str1) && idx2 < len(str2) {
		c1, c2 := str1[idx1], str2[idx2]
		dig1, dig2 := isdigit(c1), isdigit(c2)
		switch {
		case dig1 != dig2: // Digits before other characters.
			return dig1 // True if LHS is a digit, false if the RHS is one.
		case !dig1: // && !dig2, because dig1 == dig2
			// UTF-8 compares bytewise-lexicographically, no need to decode
			// codepoints.
			if c1 != c2 {
				return c1 < c2
			}
			idx1++
			idx2++
		default: // Digits
			// Eat zeros.
			for ; idx1 < len(str1) && str1[idx1] == '0'; idx1++ {
			}
			for ; idx2 < len(str2) && str2[idx2] == '0'; idx2++ {
			}
			// Eat all digits.
			nonZero1, nonZero2 := idx1, idx2
			for ; idx1 < len(str1) && isdigit(str1[idx1]); idx1++ {
			}
			for ; idx2 < len(str2) && isdigit(str2[idx2]); idx2++ {
			}
			// If lengths of numbers with non-zero prefix differ, the shorter
			// one is less.
			if len1, len2 := idx1-nonZero1, idx2-nonZero2; len1 != len2 {
				return len1 < len2
			}
			// If they're not equal, string comparison is correct.
			if nr1, nr2 := str1[nonZero1:idx1], str2[nonZero2:idx2]; nr1 != nr2 {
				return nr1 < nr2
			}
			// Otherwise, the one with less zeros is less.
			// Because everything up to the number is equal, comparing the index
			// after the zeros is sufficient.
			if nonZero1 != nonZero2 {
				return nonZero1 < nonZero2
			}
		}
		// They're identical so far, so continue comparing.
	}
	// So far they are identical. At least one is ended. If the other continues,
	// it sorts last.
	return len(str1) < len(str2)
}