-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathword.go
152 lines (142 loc) · 3.53 KB
/
word.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
package randomizer
import (
"bytes"
"strings"
)
const (
deci string = "0123456789"
lhexdict string = "0123456789abcdef"
uhexdict string = "0123456789ABCDEF"
)
type word struct{}
var Word word
// Decimal generates a random numeric string of the specified length,
// consisting of characters from "0123456789".
func (word) Decimal(length int) string {
if length <= 0 {
return ""
}
var sb strings.Builder
sb.Grow(length)
for i, hash := 0, uint64(0); i < length; i++ {
if i&7 == 0 {
hash = DefaultHashPool.Sum64()
for j, digit := 0, uint64(0); j < 8 && (i+j) < length; j++ {
digit = (hash >> (j * 4)) % 10
sb.WriteByte(deci[digit])
}
i += 7
}
}
return sb.String()
}
// DecimalBytes generates a random numeric byte slice of the specified length,
// consisting of digits from "0123456789".
func (word) DecimalBytes(length int) []byte {
if length <= 0 {
return nil
}
var bb bytes.Buffer
bb.Grow(length)
for i, hash := 0, uint64(0); i < length; i++ {
if i&7 == 0 {
hash = DefaultHashPool.Sum64()
for j, digit := 0, uint64(0); j < 8 && (i+j) < length; j++ {
digit = (hash >> (j * 4)) % 10
bb.WriteByte(deci[digit])
}
i += 7
}
}
return bb.Bytes()
}
// Hex generates a random hexadecimal string of the specified length.
// If the uppercase parameter is true, the generated string will use uppercase letters (A-F),
// otherwise, it will use lowercase letters (a-f).
func (word) Hex(length int, uppercase bool) string {
if length <= 0 {
return ""
}
var sb strings.Builder
sb.Grow(length)
for i, hash := 0, uint64(0); i < length; i++ {
if i&7 == 0 {
hash = DefaultHashPool.Sum64()
for j, digit := 0, uint64(0); j < 8 && (i+j) < length; j++ {
digit = (hash >> (j * 4)) & 0x0F
if uppercase {
sb.WriteByte(uhexdict[digit])
continue
}
sb.WriteByte(lhexdict[digit])
}
i += 7
}
}
return sb.String()
}
// HexBytes generates a random hexadecimal byte slice of the specified length.
// If the uppercase parameter is true, the generated bytes will use uppercase letters (A-F),
// otherwise, it will use lowercase letters (a-f).
func (word) HexBytes(length int, uppercase bool) []byte {
if length <= 0 {
return nil
}
var bb bytes.Buffer
bb.Grow(length)
for i, hash := 0, uint64(0); i < length; i++ {
if i&7 == 0 {
hash = DefaultHashPool.Sum64()
for j, digit := 0, uint64(0); j < 8 && (i+j) < length; j++ {
digit = (hash >> (j * 4)) & 0x0F
if uppercase {
bb.WriteByte(uhexdict[digit])
continue
}
bb.WriteByte(lhexdict[digit])
}
i += 7
}
}
return bb.Bytes()
}
// Octal generates a random octal string of the specified length,
// consisting of characters from "01234567".
func (word) Octal(length int) string {
if length <= 0 {
return ""
}
var sb strings.Builder
sb.Grow(length)
for i, hash := 0, uint64(0); i < length; i++ {
if i&7 == 0 {
hash = DefaultHashPool.Sum64()
for j, digit := 0, uint64(0); j < 8 && (i+j) < length; j++ {
digit = (hash >> (j * 3)) & 0x7
sb.WriteByte(deci[digit])
}
i += 7
}
}
return sb.String()
}
// OctalBytes generates a random octal byte slice of the specified length,
// consisting of digits from "01234567".
func (word) OctalBytes(length int) []byte {
if length <= 0 {
return nil
}
var bb bytes.Buffer
bb.Grow(length)
for i, hash := 0, uint64(0); i < length; i++ {
if i&7 == 0 {
hash = DefaultHashPool.Sum64()
for j, digit := 0, uint64(0); j < 8 && (i+j) < length; j++ {
digit = (hash >> (j * 3)) & 0x7
bb.WriteByte(deci[digit])
}
i += 7
}
}
return bb.Bytes()
}