-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworder.go
179 lines (159 loc) · 3.22 KB
/
worder.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package words
import (
"bufio"
"github.com/chainreactors/words/mask"
"github.com/chainreactors/words/rule"
"os"
"strings"
)
type WordFunc func(string) []string
var CustomWords [][]string
func NewWorder(word *Worder) *Worder {
word.Output = make(chan string)
return word
}
func NewWorderWithList(list []string) *Worder {
worder := &Worder{
Input: make(chan string),
Output: make(chan string),
}
go func() {
for _, l := range list {
worder.Input <- l
}
close(worder.Input)
}()
return worder
}
func NewWorderWithChan(ch chan string) *Worder {
worder := &Worder{
Input: make(chan string),
Output: make(chan string),
}
go func() {
for w := range ch {
worder.Input <- w
}
close(worder.Input)
}()
return worder
}
func NewWorderWithDsl(word string, params [][]string, keywords map[string][]string) (*Worder, error) {
worder := &Worder{
Input: make(chan string),
Output: make(chan string),
}
var err error
var input chan string
if params != nil {
input, err = mask.RunToStream(word, params, keywords)
} else {
input, err = mask.RunToStream(word, CustomWords, keywords)
}
if err != nil {
return nil, err
}
worder.Input = input
return worder, nil
}
func NewWorderWithFile(file *os.File) *Worder {
worder := &Worder{
Input: make(chan string),
Output: make(chan string),
}
scanner := bufio.NewScanner(file)
go func() {
for scanner.Scan() {
worder.Input <- strings.TrimSpace(scanner.Text())
}
close(worder.Input)
}()
return worder
}
type Worder struct {
Input chan string
Output chan string
Rules []rule.Expression
Fns []WordFunc
token int
count int
}
func (word *Worder) SetRules(rules string, filter string) {
word.Rules = rule.Compile(rules, filter).Expressions
}
func (word *Worder) AddFunction(f WordFunc) {
word.Fns = append(word.Fns, f)
}
func (word *Worder) EvalFunctions(w string) []string {
var ss []string
for _, f := range word.Fns {
if ss != nil {
for i, s := range ss {
newss := f(s)
if len(newss) == 1 {
ss[i] = newss[0]
} else if len(newss) > 1 {
ss = append(ss[:i], newss...)
ss = append(ss, ss[i+1:]...)
} else {
ss = append(ss[:i], ss[i+1:]...)
}
}
} else {
ss = f(w)
}
}
return ss
}
func (word *Worder) Run() {
go func() {
for w := range word.Input {
word.count++
if w == "" {
continue
}
if word.Rules != nil {
for r := range rule.RunAsStream(word.Rules, w) {
if word.Fns != nil {
if ws := word.EvalFunctions(w); ws != nil {
for _, i := range word.EvalFunctions(w) {
word.count++
word.Output <- i
}
} else {
word.Output <- "" // 表示skip
}
} else {
word.count++
word.Output <- r
}
}
} else {
if word.Fns != nil {
if ws := word.EvalFunctions(w); ws != nil {
for _, i := range word.EvalFunctions(w) {
word.count++
word.Output <- i
}
} else {
word.Output <- "" // 表示skip
}
} else {
word.count++
word.Output <- w
}
}
}
close(word.Output)
}()
}
func (word *Worder) All() []string {
var ws []string
for w := range word.Output {
ws = append(ws, w)
}
return ws
}
func (word *Worder) Count() int {
return word.count
}