@@ -2,22 +2,24 @@ package random
2
2
3
3
import (
4
4
"math/rand"
5
+ "strings"
5
6
"time"
6
7
)
7
8
8
9
type (
9
10
Random struct {
10
- charset Charset
11
11
}
12
-
13
- Charset string
14
12
)
15
13
14
+ // Charsets
16
15
const (
17
- Alphanumeric Charset = Alphabetic + Numeric
18
- Alphabetic Charset = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
19
- Numeric Charset = "0123456789"
20
- Hex Charset = Numeric + "abcdef"
16
+ Uppercase string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
17
+ Lowercase = "abcdefghijklmnopqrstuvwxyz"
18
+ Alphabetic = Uppercase + Lowercase
19
+ Numeric = "0123456789"
20
+ Alphanumeric = Alphabetic + Numeric
21
+ Symbols = "`" + `~!@#$%^&*()-_+={}[]|\;:"<>,./?`
22
+ Hex = Numeric + "abcdef"
21
23
)
22
24
23
25
var (
@@ -26,27 +28,21 @@ var (
26
28
27
29
func New () * Random {
28
30
rand .Seed (time .Now ().UnixNano ())
29
- return & Random {
30
- charset : Alphanumeric ,
31
- }
32
- }
33
-
34
- func (r * Random ) SetCharset (c Charset ) {
35
- r .charset = c
31
+ return new (Random )
36
32
}
37
33
38
- func (r * Random ) String (length uint8 ) string {
34
+ func (r * Random ) String (length uint8 , charsets ... string ) string {
35
+ charset := strings .Join (charsets , "" )
36
+ if charset == "" {
37
+ charset = Alphanumeric
38
+ }
39
39
b := make ([]byte , length )
40
40
for i := range b {
41
- b [i ] = r . charset [rand .Int63 ()% int64 (len (r . charset ))]
41
+ b [i ] = charset [rand .Int63 ()% int64 (len (charset ))]
42
42
}
43
43
return string (b )
44
44
}
45
45
46
- func SetCharset (c Charset ) {
47
- global .SetCharset (c )
48
- }
49
-
50
- func String (length uint8 ) string {
51
- return global .String (length )
46
+ func String (length uint8 , charsets ... string ) string {
47
+ return global .String (length , charsets ... )
52
48
}
0 commit comments