-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathtype-portlist.go
63 lines (55 loc) · 1.38 KB
/
type-portlist.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
package gonmap
import (
"regexp"
"strconv"
"strings"
)
var portRangeRegx = regexp.MustCompile("^(\\d+)(?:-(\\d+))?$")
var portGroupRegx = regexp.MustCompile("^(\\d+(?:-\\d+)?)(?:,\\d+(?:-\\d+)?)*$")
type PortList []int
var emptyPortList = PortList([]int{})
func parsePortList(express string) PortList {
var list = PortList([]int{})
if portGroupRegx.MatchString(express) == false {
panic("port expression string invalid")
}
for _, expr := range strings.Split(express, ",") {
rArr := portRangeRegx.FindStringSubmatch(expr)
var startPort, endPort int
startPort, _ = strconv.Atoi(rArr[1])
if rArr[2] != "" {
endPort, _ = strconv.Atoi(rArr[2])
} else {
endPort = startPort
}
for num := startPort; num <= endPort; num++ {
list = append(list, num)
}
}
list = list.removeDuplicate()
return list
}
func (p PortList) removeDuplicate() PortList {
result := make([]int, 0, len(p))
temp := map[int]struct{}{}
for _, item := range p {
if _, ok := temp[item]; !ok { //如果字典中找不到元素,ok=false,!ok为true,就往切片中append元素。
temp[item] = struct{}{}
result = append(result, item)
}
}
return result
}
func (p PortList) exist(port int) bool {
for _, num := range p {
if num == port {
return true
}
}
return false
}
func (p PortList) append(ports ...int) PortList {
p = append(p, ports...)
p = p.removeDuplicate()
return p
}