-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathless.go
More file actions
48 lines (42 loc) · 1.16 KB
/
Copy pathless.go
File metadata and controls
48 lines (42 loc) · 1.16 KB
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
package command
import (
"bytes"
"strconv"
)
// lessFunc reports whether key a sorts before key b.
type lessFunc func(a, b []byte) bool
// chooseLess selects the ordering for the active comparison flag. At most one
// of -n, -h, -M, -V applies; lexical byte order is the default.
func chooseLess(f flags) lessFunc {
switch {
case bool(f.numericEnabled):
return numericLess
case bool(f.humanNumericEnabled):
return humanLess
case bool(f.monthSortEnabled):
return monthLess
case bool(f.versionSortEnabled):
return versionLess
default:
return lexicalLess
}
}
// lexicalLess compares keys as raw bytes.
func lexicalLess(a, b []byte) bool {
return bytes.Compare(a, b) < 0
}
// numericLess compares the leading numeric value of each key (-n), falling back
// to lexical order when either key is not a number.
func numericLess(a, b []byte) bool {
na, oka := parseFloat(a)
nb, okb := parseFloat(b)
if oka && okb {
return na < nb
}
return lexicalLess(a, b)
}
// parseFloat parses a trimmed key as a float, reporting success.
func parseFloat(key []byte) (float64, bool) {
n, err := strconv.ParseFloat(string(bytes.TrimSpace(key)), 64)
return n, err == nil
}