forked from seaweedfs/seaweedfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inits.go
52 lines (41 loc) · 843 Bytes
/
inits.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
package util
import (
"fmt"
"sort"
)
// HumanReadableIntsMax joins a serials of inits into a smart one like 1-3 5 ... for human readable.
func HumanReadableIntsMax(max int, ids ...int) string {
if len(ids) <= max {
return HumanReadableInts(ids...)
}
return HumanReadableInts(ids[:max]...) + " ..."
}
// HumanReadableInts joins a serials of inits into a smart one like 1-3 5 7-10 for human readable.
func HumanReadableInts(ids ...int) string {
sort.Ints(ids)
s := ""
start := 0
last := 0
for i, v := range ids {
if i == 0 {
start = v
last = v
s = fmt.Sprintf("%d", v)
continue
}
if last+1 == v {
last = v
continue
}
if last > start {
s += fmt.Sprintf("-%d", last)
}
s += fmt.Sprintf(" %d", v)
start = v
last = v
}
if last != start {
s += fmt.Sprintf("-%d", last)
}
return s
}