/
short_id.go
66 lines (57 loc) · 1.35 KB
/
short_id.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
package id
import (
"crypto/rand"
)
const (
// DefaultShortNameLen is defult length of short name
DefaultShortNameLen = 8
)
const (
alphaTab = "abcdfghjklmnpqrstvwxzABCDFGHJKLMNPQRSTVWXZ012456789"
upperTab = "ABCDFGHJKLMNPQRSTVWXZ012456789"
trimBits = 0
tabLen = len(alphaTab)
headTabLen = tabLen - 10 // first byte dont allow number character
)
// ShortID generate a random string with length n.
func ShortID(n int) string {
s, err := ShortIDWithError(n)
if err != nil {
panic(err)
}
return s
}
// UpperShortID generate a random string with length n.
func UpperShortID(n int) string {
s, err := ShortIDWithDic(n, upperTab, len(upperTab)-10)
if err != nil {
panic(err)
}
return s
}
// ShortIDWithError generate a random string with length n
func ShortIDWithError(n int) (string, error) {
return ShortIDWithDic(n, alphaTab, headTabLen)
}
func withDefaultLength(n int) int {
if n <= 0 {
n = DefaultShortNameLen
}
return n
}
// ShortIDWithDic generate a shortID with dictionary.
func ShortIDWithDic(n int, dic string, headDicLen int) (string, error) {
n = withDefaultLength(n)
b := make([]byte, n)
if nr, err := rand.Read(b); err != nil || nr != len(b) {
return "", err
}
mod := headDicLen
dicLen := len(dic)
for i, v := range b {
idx := (int(v>>trimBits) % mod)
mod = dicLen
b[i] = dic[idx]
}
return string(b), nil
}