mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-04-24 13:08:53 +08:00
44 lines
573 B
Go
44 lines
573 B
Go
package util
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
)
|
|
|
|
// 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
|
|
}
|