-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
113 lines (93 loc) · 3 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
package randstr
import (
"math/rand"
)
var (
alphabetSet = []string{
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
}
numberSet = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
defaultMaxStrLen = 8
defaultIsLenFixed = false
)
// RandStr is random string generator
type RandStr struct {
candidates []string
sampleSizeFixed bool
sampleSizeMax int
}
func New(candidates []string) *RandStr {
return NewRandStr(candidates, defaultIsLenFixed, defaultMaxStrLen)
}
// NewRandStr create a new generator
func NewRandStr(candidates []string, sampleSizeFixed bool, sampleSizeMax int) *RandStr {
return &RandStr{
candidates: candidates,
sampleSizeFixed: sampleSizeFixed,
sampleSizeMax: sampleSizeMax,
}
}
func (g *RandStr) sample(candidates []string) string {
sampleSize := g.sampleSizeMax
if !g.sampleSizeFixed {
sampleSize = rand.Intn(g.sampleSizeMax) + 1
}
randStr := ""
for i := 0; i < sampleSize; i++ {
randStr += candidates[rand.Intn(len(candidates))]
}
return randStr
}
// SetLenFixed sets if randstr generates fixed size random strings
func (g *RandStr) SetLenFixed(isFixed bool) {
g.sampleSizeFixed = isFixed
}
// SetLenMax sets the max length of random strings.
// If sample size is fixed, it is the sample size.
func (g *RandStr) SetLenMax(sampleSizeMax int) {
g.sampleSizeMax = sampleSizeMax
}
// Seed sets a seed to math.rand
func (g *RandStr) Seed(seed int64) {
rand.Seed(seed)
}
// Gen generates a random string according to candidates
func (g *RandStr) Gen() string {
return g.sample(g.candidates)
}
// GenSlice generates a slice of random strings according to candidates
func (g *RandStr) GenSlice(length int) []string {
return g.genSlice(length, g.candidates)
}
func (g *RandStr) genSlice(length int, candidates []string) []string {
strs := make([]string, length, length)
for i := range strs {
strs[i] = g.sample(candidates)
}
return strs
}
// Alphabets generates a random string with alphabets
func (g *RandStr) Alphabets() string {
return g.sample(alphabetSet)
}
// AlphabetSlice generates a slice of random strings with alphabets
func (g *RandStr) AlphabetSlice(length int) []string {
return g.genSlice(length, alphabetSet)
}
// Numbers generates a random string with numbers
func (g *RandStr) Numbers() string {
return g.sample(numberSet)
}
// NumberSlice generates a slice of random strings with numbers
func (g *RandStr) NumberSlice(length int) []string {
return g.genSlice(length, alphabetSet)
}
// Alnums generates a random string with alphabets and numbers
func (g *RandStr) Alnums() string {
return g.sample(append(alphabetSet, numberSet...))
}
// AlnumSlice generates a slice of random strings with alphabets and numbers
func (g *RandStr) AlnumSlice(length int) []string {
return g.genSlice(length, append(alphabetSet, numberSet...))
}