-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
generate.go
257 lines (195 loc) · 4.84 KB
/
generate.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package genex
import (
"math"
"regexp/syntax"
)
// Generate yields all the strings that match the `input` regex after whitelisting `charset`.
// The `infinite` argument caps the maximum boundary of repetition operators.
func Generate(input, charset *syntax.Regexp, infinite int, callback func(string)) {
var generate func(input, charset *syntax.Regexp, infinite int) _Iterator
generate = func(input, charset *syntax.Regexp, infinite int) _Iterator {
result := []_Iterator{}
switch input.Op {
case syntax.OpStar, syntax.OpPlus, syntax.OpQuest, syntax.OpRepeat:
value := []_Iterator{}
for _, sub := range input.Sub {
value = append(value, generate(sub, charset, infinite))
}
switch input.Op {
case syntax.OpStar:
input.Min = 0
input.Max = -1
case syntax.OpPlus:
input.Min = 1
input.Max = -1
case syntax.OpQuest:
input.Min = 0
input.Max = 1
}
if input.Max == -1 && infinite >= 0 {
input.Max = input.Min + infinite
}
result = append(result, _NewRepeat(_NewStack(value), input.Min, input.Max))
case syntax.OpCharClass, syntax.OpAnyCharNotNL, syntax.OpAnyChar:
if input.Op != syntax.OpCharClass {
input = charset
}
data := []string{}
for i := 0; i < len(input.Rune); i += 2 {
for j := 0; j < len(charset.Rune); j += 2 {
bounds := []rune{
rune(math.Max(float64(input.Rune[i]), float64(charset.Rune[j]))),
rune(math.Min(float64(input.Rune[i+1]), float64(charset.Rune[j+1]))),
}
if bounds[0] <= bounds[1] {
for char := bounds[0]; char <= bounds[1]; char++ {
data = append(data, string(char))
}
}
}
}
result = append(result, _NewSet(data))
case syntax.OpCapture, syntax.OpConcat:
for _, sub := range input.Sub {
result = append(result, generate(sub, charset, infinite))
}
case syntax.OpAlternate:
options := []_Iterator{}
for _, sub := range input.Sub {
options = append(options, generate(sub, charset, infinite))
}
result = append(result, _NewOption(options))
case syntax.OpLiteral:
result = append(result, _NewSet([]string{string(input.Rune)}))
default:
result = append(result, _NewSet([]string{""}))
}
return _NewStack(result)
}
if charset.Op != syntax.OpCharClass {
charset, _ = syntax.Parse(`[[:print:]]`, syntax.Perl)
}
iterator := generate(input, charset, infinite)
for iterator.rewind(); iterator.valid(); iterator.next() {
callback(iterator.current())
}
}
type _Iterator interface {
rewind()
next()
valid() bool
current() string
clone() _Iterator
}
type _Set struct {
i int
data []string
}
func (this *_Set) rewind() {
this.i = 0
}
func (this *_Set) valid() bool {
return this.i < len(this.data)
}
func (this *_Set) current() string {
return string(this.data[this.i])
}
func (this *_Set) next() {
this.i++
}
func (this *_Set) clone() _Iterator {
clone := &_Set{data: make([]string, len(this.data))}
for key, value := range this.data {
clone.data[key] = value
}
return clone
}
func _NewSet(data []string) *_Set {
return &_Set{data: data}
}
type _Stack struct {
data []_Iterator
}
func (this *_Stack) rewind() {
for i := range this.data {
this.data[i].rewind()
}
}
func (this *_Stack) valid() bool {
return this.data[0].valid()
}
func (this *_Stack) current() string {
result := ""
for i := range this.data {
result += this.data[i].current()
}
return result
}
func (this *_Stack) next() {
if this.valid() {
i := len(this.data)
i--
this.data[i].next()
for i > 0 && !this.data[i].valid() {
this.data[i].rewind()
i--
this.data[i].next()
}
}
}
func (this *_Stack) clone() _Iterator {
clone := &_Stack{data: make([]_Iterator, len(this.data))}
for key, value := range this.data {
clone.data[key] = value.clone()
}
return clone
}
func _NewStack(data []_Iterator) *_Stack {
return &_Stack{data: data}
}
type _Option struct {
i int
data []_Iterator
}
func (this *_Option) rewind() {
this.i = 0
for i := range this.data {
this.data[i].rewind()
}
}
func (this *_Option) valid() bool {
return this.i < len(this.data)
}
func (this *_Option) current() string {
return this.data[this.i].current()
}
func (this *_Option) next() {
if this.valid() {
this.data[this.i].next()
for this.valid() && !this.data[this.i].valid() {
this.i++
}
}
}
func (this *_Option) clone() _Iterator {
clone := &_Option{data: make([]_Iterator, len(this.data))}
for key, value := range this.data {
clone.data[key] = value.clone()
}
return clone
}
func _NewOption(data []_Iterator) *_Option {
return &_Option{data: data}
}
func _NewRepeat(data _Iterator, min int, max int) *_Stack {
stack := []_Iterator{}
for i := 0; i < min; i++ {
stack = append(stack, data.clone())
}
if max > min {
stack = append(stack, _NewOption([]_Iterator{
_NewSet([]string{""}), _NewRepeat(data, 1, max-min),
}))
}
return _NewStack(stack)
}