-
Notifications
You must be signed in to change notification settings - Fork 22
/
reggen.go
214 lines (204 loc) · 4.82 KB
/
reggen.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
// Package reggen generates text based on regex definitions
package reggen
import (
"fmt"
"math"
"math/rand"
"os"
"regexp/syntax"
"time"
)
const runeRangeEnd = 0x10ffff
const printableChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r"
var printableCharsNoNL = printableChars[:len(printableChars)-2]
type state struct {
limit int
}
type Generator struct {
re *syntax.Regexp
rand *rand.Rand
debug bool
}
func (g *Generator) generate(s *state, re *syntax.Regexp) string {
//fmt.Println("re:", re, "sub:", re.Sub)
op := re.Op
switch op {
case syntax.OpNoMatch:
case syntax.OpEmptyMatch:
return ""
case syntax.OpLiteral:
res := ""
for _, r := range re.Rune {
res += string(r)
}
return res
case syntax.OpCharClass:
// number of possible chars
sum := 0
for i := 0; i < len(re.Rune); i += 2 {
if g.debug {
fmt.Printf("Range: %#U-%#U\n", re.Rune[i], re.Rune[i+1])
}
sum += int(re.Rune[i+1]-re.Rune[i]) + 1
if re.Rune[i+1] == runeRangeEnd {
sum = -1
break
}
}
// pick random char in range (inverse match group)
if sum == -1 {
possibleChars := []uint8{}
for j := 0; j < len(printableChars); j++ {
c := printableChars[j]
//fmt.Printf("Char %c %d\n", c, c)
// Check c in range
for i := 0; i < len(re.Rune); i += 2 {
if rune(c) >= re.Rune[i] && rune(c) <= re.Rune[i+1] {
possibleChars = append(possibleChars, c)
break
}
}
}
//fmt.Println("Possible chars: ", possibleChars)
if len(possibleChars) > 0 {
c := possibleChars[g.rand.Intn(len(possibleChars))]
if g.debug {
fmt.Printf("Generated rune %c for inverse range %v\n", c, re)
}
return string([]byte{c})
}
}
if g.debug {
fmt.Println("Char range: ", sum)
}
r := g.rand.Intn(int(sum))
var ru rune
sum = 0
for i := 0; i < len(re.Rune); i += 2 {
gap := int(re.Rune[i+1]-re.Rune[i]) + 1
if sum+gap > r {
ru = re.Rune[i] + rune(r-sum)
break
}
sum += gap
}
if g.debug {
fmt.Printf("Generated rune %c for range %v\n", ru, re)
}
return string(ru)
case syntax.OpAnyCharNotNL, syntax.OpAnyChar:
chars := printableChars
if op == syntax.OpAnyCharNotNL {
chars = printableCharsNoNL
}
c := chars[g.rand.Intn(len(chars))]
return string([]byte{c})
case syntax.OpBeginLine:
case syntax.OpEndLine:
case syntax.OpBeginText:
case syntax.OpEndText:
case syntax.OpWordBoundary:
case syntax.OpNoWordBoundary:
case syntax.OpCapture:
if g.debug {
fmt.Println("OpCapture", re.Sub, len(re.Sub))
}
return g.generate(s, re.Sub0[0])
case syntax.OpStar:
// Repeat zero or more times
res := ""
count := g.rand.Intn(s.limit + 1)
for i := 0; i < count; i++ {
for _, r := range re.Sub {
res += g.generate(s, r)
}
}
return res
case syntax.OpPlus:
// Repeat one or more times
res := ""
count := g.rand.Intn(s.limit) + 1
for i := 0; i < count; i++ {
for _, r := range re.Sub {
res += g.generate(s, r)
}
}
return res
case syntax.OpQuest:
// Zero or one instances
res := ""
count := g.rand.Intn(2)
if g.debug {
fmt.Println("Quest", count)
}
for i := 0; i < count; i++ {
for _, r := range re.Sub {
res += g.generate(s, r)
}
}
return res
case syntax.OpRepeat:
// Repeat one or more times
if g.debug {
fmt.Println("OpRepeat", re.Min, re.Max)
}
res := ""
count := 0
re.Max = int(math.Min(float64(re.Max), float64(s.limit)))
if re.Max > re.Min {
count = g.rand.Intn(re.Max - re.Min + 1)
}
if g.debug {
fmt.Println(re.Max, count)
}
for i := 0; i < re.Min || i < (re.Min+count); i++ {
for _, r := range re.Sub {
res += g.generate(s, r)
}
}
return res
case syntax.OpConcat:
// Concatenate sub-regexes
res := ""
for _, r := range re.Sub {
res += g.generate(s, r)
}
return res
case syntax.OpAlternate:
if g.debug {
fmt.Println("OpAlternative", re.Sub, len(re.Sub))
}
i := g.rand.Intn(len(re.Sub))
return g.generate(s, re.Sub[i])
default:
fmt.Fprintln(os.Stderr, "[reg-gen] Unhandled op: ", op)
}
return ""
}
// limit is the maximum number of times star, range or plus should repeat
// i.e. [0-9]+ will generate at most 10 characters if this is set to 10
func (g *Generator) Generate(limit int) string {
return g.generate(&state{limit: limit}, g.re)
}
// create a new generator
func NewGenerator(regex string) (*Generator, error) {
re, err := syntax.Parse(regex, syntax.Perl)
if err != nil {
return nil, err
}
//fmt.Println("Compiled re ", re)
return &Generator{
re: re,
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}, nil
}
func (gen *Generator) SetSeed(seed int64) {
gen.rand = rand.New(rand.NewSource(seed))
}
func Generate(regex string, limit int) (string, error) {
g, err := NewGenerator(regex)
if err != nil {
return "", err
}
return g.Generate(limit), nil
}