77 "math"
88)
99
10+ var defaultAlphabet = []rune ("_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" )
1011const (
11- defaultAlphabet = "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // len=64
1212 defaultSize = 21
1313 defaultMaskSize = 5
1414)
@@ -34,7 +34,7 @@ func initMasks(params ...int) []uint {
3434 return masks
3535}
3636
37- func getMask (alphabet string , masks []uint ) int {
37+ func getMask (alphabet [] rune , masks []uint ) int {
3838 for i := 0 ; i < len (masks ); i ++ {
3939 curr := int (masks [i ])
4040 if curr >= len (alphabet )- 1 {
@@ -45,7 +45,9 @@ func getMask(alphabet string, masks []uint) int {
4545}
4646
4747// Generate is a low-level function to change alphabet and ID size.
48- func Generate (alphabet string , size int ) (string , error ) {
48+ func Generate (rawAlphabet string , size int ) (string , error ) {
49+ alphabet := []rune (rawAlphabet )
50+
4951 if len (alphabet ) == 0 || len (alphabet ) > 255 {
5052 return "" , fmt .Errorf ("alphabet must not empty and contain no more than 255 chars. Current len is %d" , len (alphabet ))
5153 }
@@ -58,7 +60,7 @@ func Generate(alphabet string, size int) (string, error) {
5860 ceilArg := 1.6 * float64 (mask * size ) / float64 (len (alphabet ))
5961 step := int (math .Ceil (ceilArg ))
6062
61- id := make ([]byte , size )
63+ id := make ([]rune , size )
6264 bytes := make ([]byte , step )
6365 for j := 0 ; ; {
6466 _ , err := BytesGenerator (bytes )
@@ -97,7 +99,7 @@ func Nanoid(param ...int) (string, error) {
9799 if err != nil {
98100 return "" , err
99101 }
100- id := make ([]byte , size )
102+ id := make ([]rune , size )
101103 for i := 0 ; i < size ; i ++ {
102104 id [i ] = defaultAlphabet [bytes [i ]& 63 ]
103105 }
0 commit comments