-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilsHostString.go
77 lines (65 loc) · 1.27 KB
/
utilsHostString.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
73
74
75
76
77
package pighosts
import (
"regexp"
"strings"
)
func isSpecificHost(s string) bool {
for i := range filterSpecificHostTmp {
if filterSpecificHostTmp[i] == s {
return true
}
}
return false
}
func removeLocalHost(s string) string {
if !isSpecificHost(s) {
s = strings.ReplaceAll(s, localHostIP4, "")
s = strings.ReplaceAll(s, localHostIP6, "")
s = strings.ReplaceAll(s, nonRoutable, "")
return strings.TrimSpace(s)
}
return ""
}
func removeComments(s string) string {
pos := strings.Index(s, "#")
if pos > -1 {
s = s[0:pos]
}
s = regexp.MustCompile(`\s+`).ReplaceAllString(s, " ")
s = strings.TrimSpace(s)
s = strings.ToLower(s)
return s
}
func prepareHostsList(downloadHosts []string) map[string]int {
hosts := make(map[string]int, 0)
for l := range downloadHosts {
hst := downloadHosts[l]
hst = removeComments(hst)
hst = removeLocalHost(hst)
if hst == "" {
continue
}
hosts[hst]++
}
return hosts
}
func splitHostPerLine(hosts map[string]int) []string {
result := make([]string, 0)
t := nonRoutable
c := 0
for v := range hosts {
t = t + " " + v
c++
if c >= numHostPerLine {
result = append(result, t)
c = 0
t = nonRoutable
}
}
if c > 0 {
result = append(result, t)
c = 0
t = nonRoutable
}
return result
}