forked from tmdvs/Go-Emoji-Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
198 lines (166 loc) · 4.87 KB
/
search.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package emojiutils
import (
"errors"
"strings"
"github.com/teacat/emojiutils/utils"
)
// SearchResult - Occurence of an emoji in a string
type SearchResult struct {
Match Emoji
Occurrences int
Locations [][]int
}
// SearchResults - The result of a search
type SearchResults []SearchResult
// IndexOf - Check to see if search results contains a specific element
func (results SearchResults) IndexOf(result interface{}) int {
switch v := result.(type) {
case string:
var err error
result, err = LookupEmoji(v)
if err != nil {
return -1
}
}
for i, r := range results {
if r.Match == result {
return i
}
}
return -1
}
// Find a specific emoji character within a srting
func Find(emojiString string, input string) (result SearchResult, err error) {
// Firstly we'll grab the emoji record for the emoji we're looking for
var emoji Emoji
if emoji, err = LookupEmoji(emojiString); err != nil {
return // The emoji doesn't exist, return an error
}
// Find all of the emojis from in the input string
allEmoji := FindAll(input)
// Loop through emoji present in input and if any match the
// emoji we're looking for we'll return the result
for _, r := range allEmoji {
if r.Match.Key == emoji.Key {
result = r
return
}
}
// The emoji wasn't found, return an error
return result, errors.New("Emoji was not found")
}
// isDecoratorUnicode
func isDecoratorUnicode(r rune) bool {
decorators := []string{
//"FE0F", // Variation // NOTE: Already blocked in L158
"200D", // Connector
}
s := utils.RunesToHexKey([]rune{r})
for _, v := range decorators {
if s == v {
return true
}
}
return false
}
// FindAll - Find all instances of emoji
func FindAll(input string) (detectedEmojis SearchResults) {
// Convert our string to UTF runes
runes := []rune(input)
// Any potential modifiers such as a skin tone/gender
detectedModifiers := map[int]bool{}
// Loop over each "word" in the string
for index, r := range runes {
// If this index has been flaged as a modifier we do
// not want to process it again
if detectedModifiers[index] {
continue
}
// NOTE:https://github.com/tmdvs/Go-Emoji-Utils/issues/12
if index-1 >= 0 && isDecoratorUnicode(runes[index-1]) {
continue
}
// Grab the initial hex value of this run
hexKey := utils.RunesToHexKey([]rune{r})
// Ignore any basic runes, we'll get funny partials
// that we dont care about
//
// NOTE: 31-FE0F-20E3 1️⃣ Keycap Digit, https://github.com/tmdvs/Go-Emoji-Utils/issues/5
if len(hexKey) < 2 {
continue
}
previousKey := hexKey
potentialMatches := Emojis
nextIndex := index + 1
for {
// Search the Emoji definitions map to see if we have
// any matching results
potentialMatches = findEmoji(hexKey, potentialMatches)
// We found a definitive match
if len(potentialMatches) == 1 {
break
} else if len(potentialMatches) == 0 {
// We didnt find anything, so we'll check if its a single rune emoji
// Reset to original hexKey
if _, match := Emojis[previousKey]; match {
potentialMatches[previousKey] = Emojis[previousKey]
}
// Definately no modifiers
detectedModifiers = map[int]bool{}
break
} else {
// Have we hit the last rune? If so we'll stop
if nextIndex == len(runes) {
// We need to return the match for this current key
potentialMatches = map[string]Emoji{
hexKey: Emojis[hexKey],
}
break
}
// We have more than one potential match so we'll add the
// next UTF rune to the key and search again!
previousKey = hexKey
hexKey = hexKey + "-" + utils.RunesToHexKey([]rune{runes[nextIndex]})
detectedModifiers[nextIndex] = true
nextIndex++
}
}
// Loop over potential matches and ensure we're not counting partials
for key, e := range potentialMatches {
if _, match := Emojis[key]; match {
// How many runes does this emoji use
emojiRuneLength := len(strings.Split(e.Key, "-"))
// Have we already accounted for this match?
if i := detectedEmojis.IndexOf(e); i != -1 {
detectedEmojis[i].Occurrences++
detectedEmojis[i].Locations = append(detectedEmojis[i].Locations, []int{index, index + emojiRuneLength})
} else {
// Don't count the Variation Unicode for colored emojis as an emoji
if e.Key == "FE0F" {
continue
}
detectedEmojis = append(detectedEmojis, SearchResult{
Match: e,
Occurrences: 1,
Locations: [][]int{
[]int{index, index + emojiRuneLength},
},
})
}
}
}
}
// Return a map of Emojis and their counts
return detectedEmojis
}
// Search an array of emoji definitions for a key with a partial match
func findEmoji(term string, list map[string]Emoji) (results map[string]Emoji) {
results = map[string]Emoji{}
// Look for anything that has
for key, value := range list {
if strings.Index(key, term) == 0 {
results[key] = value
}
}
return
}