-
Notifications
You must be signed in to change notification settings - Fork 1
/
categories.go
51 lines (42 loc) · 964 Bytes
/
categories.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
package confusablehomoglyphs
//go:generate go run tools/gen.go
func AliasesCategories(chr rune) (string, string) {
var l, r, c, m int
l = 0
r = len(categoryData.CodePointsRanges) - 1
c = int(chr)
// binary search
for r >= l {
m = (l + r) / 2
if c < categoryData.CodePointsRanges[m][0] {
r = m - 1
} else if c > categoryData.CodePointsRanges[m][1] {
l = m + 1
} else {
return categoryData.ISO15924aliases[categoryData.CodePointsRanges[m][2]],
categoryData.Categories[categoryData.CodePointsRanges[m][3]]
}
}
return "Unknown", "Zzzz"
}
func Alias(chr rune) string {
a, _ := AliasesCategories(chr)
return a
}
func Category(chr rune) string {
_, c := AliasesCategories(chr)
return c
}
func UniqueAliases(str string) []string {
aSet := map[string]struct{}{}
for _, chr := range str {
aSet[Alias(chr)] = struct{}{}
}
keys := make([]string, len(aSet))
i := 0
for k := range aSet {
keys[i] = k
i++
}
return keys
}