-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.go
110 lines (102 loc) · 1.86 KB
/
strings.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package rules
import "regexp"
var (
ip4ExpCompile = regexp.MustCompile(`^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$`)
domainExpCompile = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_-]{0,62}(\.[a-zA-Z0-9][a-zA-Z0-9_-]{0,62})*(\.[a-zA-Z][a-zA-Z0-9]{0,10}){1}$`)
ip4ExpMustCompile = regexp.MustCompile(`((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)`)
domainExpMustCompile = regexp.MustCompile(`[a-zA-Z0-9][a-zA-Z0-9_-]{0,62}(\.[a-zA-Z0-9][a-zA-Z0-9_-]{0,62})*(\.[a-zA-Z][a-zA-Z0-9]{0,10}){1}`)
)
func domainSuffix(s string) string {
i := len(s)
count := 0
end := 0
for ; i != 0; i-- {
if s[i-1] == '.' {
if (i - 1) == 0 {
return s
}
count += 1
switch count {
case 1:
end = i - 1
case 2:
switch s[i:end] {
case "com", "co", "gov", "edu", "org", "net":
end = i - 1
default:
return s[i:]
}
default:
return s[i:]
}
}
}
if count == 0 {
return ""
}
return s
}
func domainKeyword(s string) string {
i := len(s)
count := 0
end := i
for ; i != 0; i-- {
if s[i-1] == '.' {
if (i - 1) == 0 {
return s[:end]
}
count += 1
switch count {
case 1:
end = i - 1
case 2:
switch s[i:end] {
case "com", "co", "gov", "edu", "org", "net":
end = i - 1
default:
return s[i:end]
}
default:
return s[i:end]
}
}
}
if count == 0 {
return ""
}
return s[i:end]
}
func domainCountry(s string) string {
i := len(s)
for ; i != 0; i-- {
if s[i-1] == '.' {
return s[i-1:]
}
}
return s
}
func isValid(s string) bool {
i := 0
for _, ch := range s {
if ch >= 'A' && ch <= 'Z' {
continue
}
if ch >= 'a' && ch <= 'z' {
continue
}
if ch >= '0' && ch <= '9' {
continue
}
switch ch {
case '.':
i++
case '-', '_':
default:
return false
}
}
if i == 0 {
return false
}
return true
}