-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathalphabet.go
62 lines (53 loc) · 1.19 KB
/
alphabet.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
package shortuuid
import (
"fmt"
"math"
"slices"
"unicode/utf8"
)
// DefaultAlphabet is the default alphabet used.
const (
DefaultAlphabet = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
)
type alphabet struct {
chars []rune
len int64
encLen uint8
maxBytes uint8
}
// Remove duplicates and sort it to ensure reproducibility.
func newAlphabet(s string) alphabet {
abc := []rune(s)
slices.Sort(abc)
abc = slices.Compact(abc)
if len(abc) < 2 {
panic("encoding alphabet must be at least two characters")
}
a := alphabet{
chars: abc,
len: int64(len(abc)),
encLen: uint8(math.Ceil(128 / math.Log2(float64(len(abc))))),
maxBytes: uint8(utf8.RuneLen(abc[len(abc)-1])),
}
return a
}
func (a *alphabet) Length() int64 {
return a.len
}
// Index returns the index of the first instance of t in the alphabet, or an
// error if t is not present.
func (a *alphabet) Index(t rune) (int64, error) {
i, j := 0, int(a.len)
for i < j {
h := int(uint(i+j) >> 1)
if a.chars[h] < t {
i = h + 1
} else {
j = h
}
}
if i >= int(a.len) || a.chars[i] != t {
return 0, fmt.Errorf("element '%v' is not part of the alphabet", t)
}
return int64(i), nil
}