-
Notifications
You must be signed in to change notification settings - Fork 5
/
unpack.go
72 lines (64 loc) · 1.51 KB
/
unpack.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
61
62
63
64
65
66
67
68
69
70
71
72
package hostutils
import (
"fmt"
"sort"
)
// UnpackString Unpack space septated short abbreviated hosts into full hosts list.
func UnpackString(packedHosts string) (hosts []string) {
return Unpack([]string{packedHosts})
}
// Unpack Unpack short abbreviated hosts into full hosts list.
func Unpack(packedHosts []string) (hosts []string) {
regHosts := regularizeHosts(packedHosts)
if regHosts == nil {
return nil
}
resultSet := make(map[string]bool)
for _, packedHost := range regHosts {
unpackHosts(packedHost, resultSet)
}
result := make([]string, len(resultSet))
i := 0
for key := range resultSet {
result[i] = key
i++
}
sort.Strings(result)
return result
}
func unpackHosts(packedHost string, resultSet map[string]bool) {
m := rePackedHost.FindStringSubmatch(packedHost)
if m != nil {
prefix := m[1]
cond := m[2]
suffix := m[3]
for _, num := range unpackCond(cond) {
newHost := fmt.Sprintf("%s%s%s", prefix, num, suffix)
unpackHosts(newHost, resultSet)
}
} else {
resultSet[packedHost] = true
}
}
func unpackCond(cond string) []string {
var result []string
for _, blk := range reCondSpace.Split(cond, -1) {
m := reCondBlk.FindStringSubmatch(blk)
if m != nil {
if m[2] == "" {
result = append(result, m[1])
} else {
len := maxi(len(m[1]), len(m[3]))
low := atoi(m[1])
high := atoi(m[3])
if low > high {
low, high = high, low
}
for i := low; i <= high; i++ {
result = append(result, fmt.Sprintf("%0*d", len, i))
}
}
}
}
return result
}