-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.go
135 lines (114 loc) · 2.7 KB
/
strategy.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package regex
type (
MatchStrategy interface {
Match(c byte, edge string) bool
}
BaseMatchStrategy struct {
IsReversed bool
}
CharMatchStrategy struct {
*BaseMatchStrategy
}
CharSetMatchStrategy struct {
*BaseMatchStrategy
}
EpsilonMatchStrategy struct {
*BaseMatchStrategy
}
DotMatchStrategy struct {
*BaseMatchStrategy
}
SpaceMatchStrategy struct {
*BaseMatchStrategy
}
// WMatchStrategy 匹配 \w和\W
WMatchStrategy struct {
*BaseMatchStrategy
}
// DigitalMatchStrategy 匹配数字
DigitalMatchStrategy struct {
*BaseMatchStrategy
}
MatchStrategyManager struct {
matchStrategyMap map[string]MatchStrategy
}
)
var Manager = &MatchStrategyManager{
matchStrategyMap: map[string]MatchStrategy{
"\\d": &DigitalMatchStrategy{&BaseMatchStrategy{false}},
"\\D": &DigitalMatchStrategy{&BaseMatchStrategy{true}},
"\\w": &WMatchStrategy{&BaseMatchStrategy{false}},
"\\W": &WMatchStrategy{&BaseMatchStrategy{true}},
"\\s": &SpaceMatchStrategy{&BaseMatchStrategy{false}},
"\\S": &SpaceMatchStrategy{&BaseMatchStrategy{true}},
".": &DotMatchStrategy{&BaseMatchStrategy{false}},
EPSILON: &EpsilonMatchStrategy{&BaseMatchStrategy{false}},
CHAR: &CharMatchStrategy{&BaseMatchStrategy{false}},
CHARSET: &CharSetMatchStrategy{&BaseMatchStrategy{false}},
},
}
func (m *MatchStrategyManager) Get(key string) MatchStrategy {
// 特殊字符的匹配
if s, ok := m.matchStrategyMap[key]; ok {
return s
}
if len(key) == 1 {
return m.matchStrategyMap[CHAR]
}
return m.matchStrategyMap[CHARSET]
}
func (s *EpsilonMatchStrategy) Match(c byte, edge string) bool {
return true
}
func (s *WMatchStrategy) Match(c byte, edge string) bool {
res := c >= 'A' && c <= 'Z' ||
c >= 'a' && c <= 'z' || c >= '0' && c <= '9'
if s.IsReversed {
return !res
}
return res
}
func (s *SpaceMatchStrategy) Match(c byte, edge string) bool {
res := c == '\f' || c == '\n' ||
c == '\r' || c == '\t' || c == ' '
if s.IsReversed {
return !res
}
return res
}
func (s *DotMatchStrategy) Match(c byte, edge string) bool {
return c != '\n' && c != '\r'
}
func (s *DigitalMatchStrategy) Match(c byte, edge string) bool {
res := c >= '0' && c <= '9'
if s.IsReversed {
return !res
}
return res
}
func (s *CharSetMatchStrategy) Match(c byte, edge string) bool {
ret := false
not := edge[0] == '^'
for i, item := range edge {
if not {
continue
}
if item == '-' {
return c >= edge[i-1] && c <= edge[i+1]
}
if c == byte(item) {
ret = true
break
}
}
if not {
return !ret
}
return ret
}
func (s *CharMatchStrategy) Match(c byte, edge string) bool {
return edge[0] == c
}
func (s *BaseMatchStrategy) Match(c byte, edge string) bool {
return false
}