-
Notifications
You must be signed in to change notification settings - Fork 0
/
id.go
42 lines (34 loc) · 819 Bytes
/
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
package qp
import (
"math/rand"
"strings"
)
func ID(prefix string) string {
if len(prefix) > 7 {
panic("prefix must be less than 8 characters: " + prefix)
}
return prefix + "-" + newID(16)
}
const (
chars = "abcdefghjklmnopqrstvwxyzABCDEFGHJKLMNOPQRSTVWXYZ023456789"
)
func newID(n int) string {
return randString(chars, 6, 1<<6-1, 63/6, n)
}
// borrowed from https://stackoverflow.com/questions/22892120/
func randString(chars string, idxBits uint, idxMask int64, idxMax int, n int) string {
var sb strings.Builder
sb.Grow(n)
for i, cache, remain := n-1, rand.Int63(), idxMax; i >= 0; {
if remain == 0 {
cache, remain = rand.Int63(), idxMax
}
if idx := int(cache & idxMask); idx < len(chars) {
sb.WriteByte(chars[idx])
i--
}
cache >>= idxBits
remain--
}
return sb.String()
}