@@ -2,22 +2,24 @@ package random
22
33import (
44 "math/rand"
5+ "strings"
56 "time"
67)
78
89type (
910 Random struct {
10- charset Charset
1111 }
12-
13- Charset string
1412)
1513
14+ // Charsets
1615const (
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"
2123)
2224
2325var (
@@ -26,27 +28,21 @@ var (
2628
2729func New () * Random {
2830 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 )
3632}
3733
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+ }
3939 b := make ([]byte , length )
4040 for i := range b {
41- b [i ] = r . charset [rand .Int63 ()% int64 (len (r . charset ))]
41+ b [i ] = charset [rand .Int63 ()% int64 (len (charset ))]
4242 }
4343 return string (b )
4444}
4545
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 ... )
5248}
0 commit comments