-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.go
174 lines (162 loc) · 4.03 KB
/
random.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
package random
import (
"math/rand"
"sync"
"time"
)
const (
bufferSize = 32
byte2print = `0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-`
)
var pool = sync.Pool{
New: func() interface{} {
return make([]byte, bufferSize)
},
}
var randomLock sync.Mutex
var auto rand.Source = rand.NewSource(time.Now().UnixNano())
func read(p []byte) {
var val uint64
var on uintptr
div := len(p) / 8
randomLock.Lock()
defer randomLock.Unlock()
for i := 0; i < div; i++ {
val = uint64(auto.Int63())
p[on] = byte(val)
p[on+1] = byte(val >> 8)
p[on+2] = byte(val >> 16)
p[on+3] = byte(val >> 24)
p[on+4] = byte(val >> 32)
p[on+5] = byte(val >> 40)
p[on+6] = byte(val >> 48)
p[on+7] = byte(val >> 56)
on += 8
}
switch uintptr(len(p)) - on {
case 0:
return
case 1:
p[on] = byte(auto.Int63())
case 2:
val = uint64(auto.Int63())
p[on] = byte(val)
p[on+1] = byte(val >> 8)
case 3:
val = uint64(auto.Int63())
p[on] = byte(val)
p[on+1] = byte(val >> 8)
p[on+2] = byte(val >> 16)
case 4:
val = uint64(auto.Int63())
p[on] = byte(val)
p[on+1] = byte(val >> 8)
p[on+2] = byte(val >> 16)
p[on+3] = byte(val >> 24)
case 5:
val = uint64(auto.Int63())
p[on] = byte(val)
p[on+1] = byte(val >> 8)
p[on+2] = byte(val >> 16)
p[on+3] = byte(val >> 24)
p[on+4] = byte(val >> 32)
case 6:
val = uint64(auto.Int63())
p[on] = byte(val)
p[on+1] = byte(val >> 8)
p[on+2] = byte(val >> 16)
p[on+3] = byte(val >> 24)
p[on+4] = byte(val >> 32)
p[on+5] = byte(val >> 40)
case 7:
val = uint64(auto.Int63())
p[on] = byte(val)
p[on+1] = byte(val >> 8)
p[on+2] = byte(val >> 16)
p[on+3] = byte(val >> 24)
p[on+4] = byte(val >> 32)
p[on+5] = byte(val >> 40)
p[on+6] = byte(val >> 48)
}
return
}
func printable(p []byte) {
var val uint64
var on uintptr
div := len(p) / 8
randomLock.Lock()
defer randomLock.Unlock()
for i := 0; i < div; i++ {
val = uint64(auto.Int63())
p[on] = byte2print[byte(val)]
p[on+1] = byte2print[byte(val>>8)]
p[on+2] = byte2print[byte(val>>16)]
p[on+3] = byte2print[byte(val>>24)]
p[on+4] = byte2print[byte(val>>32)]
p[on+5] = byte2print[byte(val>>40)]
p[on+6] = byte2print[byte(val>>48)]
p[on+7] = byte2print[byte(val>>56)]
on += 8
}
switch uintptr(len(p)) - on {
case 0:
return
case 1:
p[on] = byte2print[byte(auto.Int63())]
case 2:
val = uint64(auto.Int63())
p[on] = byte2print[byte(val)]
p[on+1] = byte2print[byte(val>>8)]
case 3:
val = uint64(auto.Int63())
p[on] = byte2print[byte(val)]
p[on+1] = byte2print[byte(val>>8)]
p[on+2] = byte2print[byte(val>>16)]
case 4:
val = uint64(auto.Int63())
p[on] = byte2print[byte(val)]
p[on+1] = byte2print[byte(val>>8)]
p[on+2] = byte2print[byte(val>>16)]
p[on+3] = byte2print[byte(val>>24)]
case 5:
val = uint64(auto.Int63())
p[on] = byte2print[byte(val)]
p[on+1] = byte2print[byte(val>>8)]
p[on+2] = byte2print[byte(val>>16)]
p[on+3] = byte2print[byte(val>>24)]
p[on+4] = byte2print[byte(val>>32)]
case 6:
val = uint64(auto.Int63())
p[on] = byte2print[byte(val)]
p[on+1] = byte2print[byte(val>>8)]
p[on+2] = byte2print[byte(val>>16)]
p[on+3] = byte2print[byte(val>>24)]
p[on+4] = byte2print[byte(val>>32)]
p[on+5] = byte2print[byte(val>>40)]
case 7:
val = uint64(auto.Int63())
p[on] = byte2print[byte(val)]
p[on+1] = byte2print[byte(val>>8)]
p[on+2] = byte2print[byte(val>>16)]
p[on+3] = byte2print[byte(val>>24)]
p[on+4] = byte2print[byte(val>>32)]
p[on+5] = byte2print[byte(val>>40)]
p[on+6] = byte2print[byte(val>>48)]
}
return
}
// String returns a random alphanumeric string given the length parameter.
func String(length int) string {
if length > bufferSize {
p := make([]byte, length)
printable(p)
return string(p)
}
if length <= 0 {
return ``
}
p := pool.Get().([]byte)
defer pool.Put(p)
printable(p[0:length])
return string(p[0:length])
}