-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemoji.go
142 lines (130 loc) · 3.13 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
package goemoji
import "strings"
type HandlerFunc = func(s string)
type ReplaceFunc = func(s string) string
var tree *node
func init() {
root, _ := readAllEmoji()
tree = root
}
// HandleAll will call the handler function for each emoji and text
func HandleAll(s string, emojiHandler HandlerFunc, textHandler HandlerFunc) {
next := tree
startText := 0
endText := 0
inEmoji := false
startEmoji := 0
endEmoji := 0
isEmoji := false
for i, c := range s {
next = next.getNode(int(c))
if next != nil {
if !inEmoji {
startEmoji = i
}
inEmoji = true
isEmoji = next.IsEnd
}
if next == nil && inEmoji {
inEmoji = false
if isEmoji {
endEmoji = i
text := s[startText:endText]
textHandler(text)
emojiHandler(s[startEmoji:endEmoji])
startText = i
}
endText = i
isEmoji = false
}
if next == nil {
next = tree
if next.getNode(int(c)) != nil {
next = next.getNode(int(c))
if !inEmoji {
startEmoji = i
}
inEmoji = true
isEmoji = next.IsEnd
}
}
if !inEmoji {
endText = i + 1
}
}
// handle last emoji
if inEmoji && next != nil && next.IsEnd {
textHandler(s[startText:endText])
endEmoji = len(s)
emojiHandler(s[startEmoji:endEmoji])
textHandler(s[endEmoji:])
} else {
textHandler(s[startText:])
}
}
// ReplaceEmojis will replace all emojis with the replace function
func ReplaceEmojis(s string, replaceFunc ReplaceFunc) string {
result := &strings.Builder{}
HandleAll(s, func(emoji string) {
result.WriteString(replaceFunc(emoji))
}, func(text string) {
result.WriteString(text)
})
return result.String()
}
// ReplaceText will replace all text with the replace function
func ReplaceText(s string, replaceFunc ReplaceFunc) string {
result := &strings.Builder{}
HandleAll(s, func(emoji string) {
result.WriteString(emoji)
}, func(text string) {
result.WriteString(replaceFunc(text))
})
return result.String()
}
// Replace will replace all emojis and text with the replace function
func Replace(s string, replaceEmojiFunc ReplaceFunc, replaceTextFunc ReplaceFunc) string {
result := &strings.Builder{}
HandleAll(s, func(emoji string) {
result.WriteString(replaceEmojiFunc(emoji))
}, func(text string) {
result.WriteString(replaceTextFunc(text))
})
return result.String()
}
// RemoveText will remove all text
func RemoveText(s string) string {
result := &strings.Builder{}
HandleAll(s, func(emoji string) {
result.WriteString(emoji)
}, func(text string) {})
return result.String()
}
// RemoveEmojis will remove all emojis
func RemoveEmojis(s string) string {
result := &strings.Builder{}
HandleAll(s, func(emoji string) {}, func(text string) {
result.WriteString(text)
})
return result.String()
}
// Split will split the string into emojis and text
func Split(s string, withEmoji bool) []string {
result := make([]string, 0)
HandleAll(s, func(emoji string) {
if withEmoji {
result = append(result, emoji)
}
}, func(text string) {
result = append(result, text)
})
return result
}
// Count will count the number of emojis
func Count(s string) int {
i := 0
HandleAll(s, func(emoji string) {
i++
}, func(text string) {})
return i
}