-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathstringgen.go
70 lines (55 loc) · 1.36 KB
/
stringgen.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
package main
import "math"
type StringGen struct {
charset string // The set of characters to choose from
charsetlen int // Length of charset
length int // lenght of the generated string
indexdata []int // Index data for where we are in each byte
data []byte // The actual bytes that make up the string
done int // If a leading position is done, we increment this
first bool
}
func NewStringGen(charset string, length int) *StringGen {
sg := StringGen{
charset: charset,
charsetlen: len(charset),
length: length,
indexdata: make([]int, length),
data: make([]byte, length),
done: 0,
first: true,
}
return &sg
}
func (sg *StringGen) Complexity() int64 {
return int64(float32(math.Pow(float64(sg.charsetlen), float64(sg.length))))
}
func (sg *StringGen) Next() bool {
if sg.first {
for i := 0; i < sg.length; i++ {
sg.data[i] = sg.charset[0]
}
sg.first = false
return true
}
if sg.done == sg.charsetlen {
return false
}
for i := sg.length - 1; i >= 0; i-- {
if sg.indexdata[i] == sg.charsetlen-1 {
sg.indexdata[i] = 0
sg.data[i] = sg.charset[0]
} else {
sg.indexdata[i]++
sg.data[i] = sg.charset[sg.indexdata[i]]
if sg.done == i && sg.indexdata[i] == sg.charsetlen-1 {
sg.done++
}
break
}
}
return true
}
func (sg *StringGen) String() string {
return string(sg.data)
}