-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
142 lines (120 loc) · 3.01 KB
/
generator.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
package er
import (
"bytes"
"errors"
"math/rand"
"regexp/syntax"
"time"
)
// generate
func generate(writer *bytes.Buffer, regexp *syntax.Regexp) error {
switch regexp.Op {
case syntax.OpNoMatch,
syntax.OpEmptyMatch,
syntax.OpNoWordBoundary,
syntax.OpBeginLine,
syntax.OpBeginText,
syntax.OpEndText:
case syntax.OpLiteral:
writer.WriteString(string(regexp.Rune))
case syntax.OpCharClass:
if len(regexp.Rune)%2 != 0 || len(regexp.Rune) == 0 {
return nil
}
writer.WriteRune(RandRune(regexp.Rune))
case syntax.OpAnyCharNotNL:
writer.WriteRune(RandAscii())
case syntax.OpAnyChar:
r := RandInRange(32, 128)
if r == 127 {
writer.WriteRune('\n')
} else {
writer.WriteRune(rune(r))
}
case syntax.OpEndLine:
writer.WriteRune('\n')
case syntax.OpWordBoundary:
writer.WriteRune(0x20)
case syntax.OpStar:
return repeat(writer, regexp.Sub, 0, -1)
case syntax.OpPlus:
return repeat(writer, regexp.Sub, 1, -1)
case syntax.OpQuest:
if RandBool() {
if err := traversal(writer, regexp.Sub); err != nil {
return err
}
}
case syntax.OpRepeat:
return repeat(writer, regexp.Sub, regexp.Min, regexp.Max)
case syntax.OpConcat, syntax.OpCapture:
return traversal(writer, regexp.Sub)
case syntax.OpAlternate:
return generate(writer, regexp.Sub[RandInt(len(regexp.Sub))])
}
return nil
}
// Generate a random string according to *syntax.Regexp ( got by syntax.Parse() )
func Generate(regexp *syntax.Regexp) (string, error) {
var buf bytes.Buffer
err := generate(&buf, regexp)
return buf.String(), err
}
func traversal(writer *bytes.Buffer, children []*syntax.Regexp) error {
for _, child := range children {
if err := generate(writer, child); err != nil {
return err
}
}
return nil
}
// repeat for [min, max) times
func repeat(writer *bytes.Buffer, list []*syntax.Regexp, min, max int) error {
for count := RandRepeat(min, max); count > 0; count-- {
if err := traversal(writer, list); err != nil {
return err
}
}
return nil
}
// Generator generate string(s)
type Generator interface {
// Generator a random string
Generate() (string, error)
// Generator multiple strings
GenerateMultiple(amount int) ([]string, error)
}
type generator struct {
regexp *syntax.Regexp
}
// generate one string
func (g *generator) Generate() (string, error) {
rand.Seed(time.Now().UnixNano())
return Generate(g.regexp)
}
// generate multiple strings
func (g *generator) GenerateMultiple(amount int) ([]string, error) {
rand.Seed(time.Now().UnixNano())
if amount < 1 {
return nil, errors.New("amount must be greater than or equal to 1")
}
result := make([]string, amount)
var err error
for i := 0; i < amount; i++ {
if result[i], err = Generate(g.regexp); err != nil {
return nil, err
}
}
return result, nil
}
// Parse a pattern string and return a Generator if success
func Parse(pattern string, flags syntax.Flags) (Generator, error) {
reg, err := syntax.Parse(pattern, flags)
if err != nil {
return nil, err
}
g := &generator{
regexp: reg.Simplify(),
}
return g, nil
}