-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
emoji.go
164 lines (153 loc) · 4.25 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package inference
import (
"fmt"
"regexp"
"strconv"
"strings"
"unicode"
"unicode/utf8"
"github.com/enescakir/emoji"
"golang.org/x/text/unicode/runenames"
)
const zwj = '\u200d' // ZWJ (Zero Width Joiner)
var emojiPartsRegexp = *regexp.MustCompile(`\<(\d+)\>`)
var emojiToAliasMap map[string]string
type RuneData struct {
Rune rune
RuneName string
UnicodeEscape string
}
// See: https://tutorialedge.net/golang/the-go-init-function/
func init() {
aliasToEmojiMap := emoji.Map()
emojiToAliasMap = make(map[string]string)
for alias, emoji := range aliasToEmojiMap {
// if current emoji exists multiple times (with different aliases), prefer alphabetically prior one
if existingAlias, ok := emojiToAliasMap[emoji]; ok {
if strings.Compare(existingAlias, alias) == 1 {
emojiToAliasMap[emoji] = alias
}
} else {
emojiToAliasMap[emoji] = alias
}
}
}
func extractRuneData(str string) []RuneData {
result := make([]RuneData, 0)
for _, r := range str {
result = append(result, RuneData{
Rune: r,
RuneName: runenames.Name(r),
UnicodeEscape: fmt.Sprintf("\\U%08X", r),
})
}
return result
}
func processEmoji(decodingContext *generationDecodingContext, r rune) string {
if decodingContext.decodingFinished {
decodingContext.decodingFinished = false
}
if unicode.IsGraphic(r) || r == zwj {
decodingContext.waitingRunes += string(r)
}
decodingContext.waitingRunesExtraStr = ""
if emojiParts, ok := searchMinimumEmojiCount(decodingContext.waitingRunes); ok {
decodingContext.waitingRunesExtraStr = ""
for _, emojiPart := range emojiParts {
emojiAlias, ok := emojiToAliasMap[emojiPart]
runeData := extractRuneData(emojiPart)
if ok {
compositeUnicodeEscape := ""
for _, runeItem := range runeData {
compositeUnicodeEscape += runeItem.UnicodeEscape
}
decodingContext.waitingRunesExtraStr += fmt.Sprintf("[%s%s]", emojiAlias, compositeUnicodeEscape)
} else {
runeData := extractRuneData(emojiPart)
for _, runeItem := range runeData {
decodingContext.waitingRunesExtraStr += fmt.Sprintf("[:%s:%s]", runeItem.RuneName, runeItem.UnicodeEscape)
}
}
}
} else {
runeData := extractRuneData(decodingContext.waitingRunes)
for _, runeItem := range runeData {
decodingContext.waitingRunesExtraStr += fmt.Sprintf("[:%s:%s]", runeItem.RuneName, runeItem.UnicodeEscape)
}
}
if !unicode.IsGraphic(r) && r != zwj {
extraStr := decodingContext.waitingRunesExtraStr
decodingContext.waitingRunes = ""
decodingContext.waitingRunesExtraStr = ""
return extraStr + string(r)
}
return string(r)
}
func searchMinimumEmojiCount(str string) ([]string, bool) {
strOriginal := str
for i := 0; i < len(str); {
increment_i := true
for j := len(str); j > i; {
skip := false
substr := str[i:j]
if j > 0 && substr[len(substr)-1:] == ">" {
openIdx := strings.LastIndex(substr, "<")
if openIdx > -1 {
j = openIdx - 1
skip = true
}
}
if substr[0:1] == "<" {
closeIdx := strings.Index(substr, ">")
if closeIdx > -1 {
i = closeIdx + 1
skip = true
}
}
if skip {
continue
}
if _, ok := emojiToAliasMap[substr]; ok {
replaceText := fmt.Sprintf("<%d>", len(substr))
str = str[:i] + replaceText + str[j:]
i += len(replaceText)
j += len(replaceText) - len(substr)
//foundEmojiCount++
increment_i = false
continue
}
_, rsize := utf8.DecodeLastRuneInString(str[i:j])
j -= rsize
}
if increment_i {
_, rsize := utf8.DecodeRuneInString(str[i:])
i += rsize
}
}
runeSizes := make([]int, 0)
for len(str) > 0 {
matchLoc := emojiPartsRegexp.FindStringIndex(str)
var rsize int
var err error
if matchLoc == nil || matchLoc[0] > 0 {
_, rsize = utf8.DecodeRuneInString(str)
runeSizes = append(runeSizes, rsize)
str = str[rsize:]
} else {
match := emojiPartsRegexp.FindStringSubmatch(str)
rsize, err = strconv.Atoi(match[1])
if err != nil {
return nil, false
}
runeSizes = append(runeSizes, rsize)
str = str[len(match[0]):]
}
}
result := make([]string, 0)
idx := 0
for runeSizeIdx := 0; runeSizeIdx < len(runeSizes); runeSizeIdx++ {
result = append(result, strOriginal[idx:idx+runeSizes[runeSizeIdx]])
idx += runeSizes[runeSizeIdx]
}
return result, len(result) > 0
}