@@ -11,8 +11,11 @@ const (
11
11
Uppercase Charset = 1 << iota // 1 << 0 which is 00000001
12
12
Lowercase // 1 << 1 which is 00000010
13
13
Numbers // 1 << 2 which is 00000100
14
- Symbols // 1 << 3 which is 00001000
15
- SimpleSymbols // 1 << 4 which is 00001000
14
+ Unreserved // 1 << 3 which is 00001000
15
+ Reserved // 1 << 4 which is 00010000
16
+ SimpleSymbols // 1 << 5 which is 00100000
17
+ Symbols // 1 << 6 which is 01000000
18
+ Default = Uppercase | Lowercase | Numbers | SimpleSymbols
16
19
)
17
20
18
21
var (
@@ -22,14 +25,18 @@ var (
22
25
len_lowercase = len (lowercase )
23
26
numbers = []byte (`0123456789` )
24
27
len_numbers = len (numbers )
28
+ unreserved = []byte (`-._~` ) // ref: https://perishablepress.com/stop-using-unsafe-characters-in-urls/
29
+ len_unreserved = len (unreserved )
30
+ reserved = []byte (`!#$&'()*+,/:;=?@[]` )
31
+ len_reserved = len (reserved )
32
+ simple_symbols = []byte (`!$&'()*+,-.:;=@_~` ) // ref: https://github.com/golang/go/blob/release-branch.go1.15/src/net/url/url.go#L1158-L1186 , missing: Unreserved | Reserved - #/?[]
33
+ len_simple_symbols = len (simple_symbols )
25
34
symbols = []byte (`!"#$%&'()*+,-./:;<=>?@^[\]_{|}~` + "`" )
26
35
len_symbols = len (symbols )
27
- simple_symbols = []byte (`!#$%&*+-=?@^_|` )
28
- len_simple_symbols = len (simple_symbols )
29
36
)
30
37
31
38
func Generate (n int ) string {
32
- return GenerateForCharset (n , Uppercase | Lowercase | Numbers | SimpleSymbols )
39
+ return GenerateForCharset (n , Default )
33
40
}
34
41
35
42
func GenerateForCharset (n int , chset Charset ) string {
@@ -45,12 +52,18 @@ func GenerateForCharset(n int, chset Charset) string {
45
52
if chset & Numbers != 0 {
46
53
count += len_numbers
47
54
}
48
- if chset & Symbols != 0 {
49
- count += len_symbols
55
+ if chset & Unreserved != 0 {
56
+ count += len_unreserved
57
+ }
58
+ if chset & Reserved != 0 {
59
+ count += len_reserved
50
60
}
51
61
if chset & SimpleSymbols != 0 {
52
62
count += len_simple_symbols
53
63
}
64
+ if chset & Symbols != 0 {
65
+ count += len_symbols
66
+ }
54
67
max := big .NewInt (int64 (count ))
55
68
56
69
for i := 0 ; i < n ; i ++ {
@@ -84,12 +97,20 @@ func GenerateForCharset(n int, chset Charset) string {
84
97
idx -= len_numbers
85
98
}
86
99
}
87
- if chset & Symbols != 0 {
88
- if idx < len_symbols {
89
- buf [i ] = symbols [idx ]
100
+ if chset & Unreserved != 0 {
101
+ if idx < len_unreserved {
102
+ buf [i ] = unreserved [idx ]
90
103
continue
91
104
} else {
92
- idx -= len_symbols
105
+ idx -= len_unreserved
106
+ }
107
+ }
108
+ if chset & Reserved != 0 {
109
+ if idx < len_reserved {
110
+ buf [i ] = reserved [idx ]
111
+ continue
112
+ } else {
113
+ idx -= len_reserved
93
114
}
94
115
}
95
116
if chset & SimpleSymbols != 0 {
@@ -100,6 +121,14 @@ func GenerateForCharset(n int, chset Charset) string {
100
121
idx -= len_simple_symbols
101
122
}
102
123
}
124
+ if chset & Symbols != 0 {
125
+ if idx < len_symbols {
126
+ buf [i ] = symbols [idx ]
127
+ continue
128
+ } else {
129
+ idx -= len_symbols
130
+ }
131
+ }
103
132
}
104
133
return string (buf )
105
134
}
0 commit comments