-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
60 lines (53 loc) · 1.09 KB
/
util.go
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
package main
import (
"fmt"
"path/filepath"
"slices"
)
func splitPath(path string) []string {
subPath := path
var result []string
for {
subPath = filepath.Clean(subPath) // Amongst others, removes trailing slashes (except for the root directory).
dir, last := filepath.Split(subPath)
if last == "" {
if dir != "" { // Root directory.
result = append(result, dir)
}
break
}
result = append(result, last)
if dir == "" { // Nothing to split anymore.
break
}
subPath = dir
}
slices.Reverse(result)
return result
}
func ByteCountSI(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB",
float64(b)/float64(div), "kMGTPE"[exp])
}
func ByteCountIEC(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB",
float64(b)/float64(div), "KMGTPE"[exp])
}