-
Notifications
You must be signed in to change notification settings - Fork 3
/
emoji.go
83 lines (73 loc) · 1.33 KB
/
emoji.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package senpai
import (
_ "embed"
"encoding/json"
"sort"
"strings"
)
//go:embed emoji.json
var emojiJSON []byte
type emoji struct {
Emoji string
Alias string
}
type emojis []emoji
func (e emojis) Len() int {
return len(e)
}
func (e emojis) Less(i, j int) bool {
return e[i].Alias < e[j].Alias
}
func (e emojis) Swap(i, j int) {
e[i], e[j] = e[j], e[i]
}
var emojiData []emoji
func init() {
type rawEmoji struct {
Emoji string `json:"emoji"`
Aliases []string `json:"aliases"`
}
var data []rawEmoji
_ = json.Unmarshal(emojiJSON, &data)
for _, e := range data {
for _, alias := range e.Aliases {
emojiData = append(emojiData, emoji{
Emoji: e.Emoji,
Alias: alias,
})
}
}
sort.Sort(emojis(emojiData))
}
func findEmoji(s string) []emoji {
if len(emojiData) == 0 {
return nil
}
i, ok := sort.Find(len(emojiData), func(i int) int {
return strings.Compare(s, emojiData[i].Alias)
})
var b, e int
for b = i; b >= 0; b-- {
if !strings.HasPrefix(emojiData[b].Alias, s) {
break
}
}
b++
for e = i; e < len(emojiData); e++ {
if !strings.HasPrefix(emojiData[e].Alias, s) {
break
}
}
if b >= e {
return nil
}
if ok {
r := make([]emoji, 0, e-b)
r = append(r, emojiData[i])
r = append(r, emojiData[b:i]...)
r = append(r, emojiData[i+1:e]...)
return r
} else {
return emojiData[b:e]
}
}