forked from Succo/emoji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glyph.go
166 lines (155 loc) · 3.38 KB
/
glyph.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
package emoji
import (
"bytes"
"unicode"
"unicode/utf8"
)
const zeroWidthJoiner = rune(0x200D)
const emojiVS = rune(0xFE0F)
const enclosingKeycap = rune(0x20E3)
const termTag = rune(0xE007F)
var enclosingKeycapS = string(enclosingKeycap)
var enclosingKeycapB = []byte(enclosingKeycapS)
// PossibleGlyph checks is the given string might be an emoji
// based on the EBNF from https://www.unicode.org/reports/tr51/#EBNF_and_Regex
//
// possible_emoji :=
// flag_sequence
// | zwj_element (\x{200D} zwj_element)*
//
//
// flag_sequence :=
// \p{RI}\p{RI}
//
// zwj_element :=
// \p{Emoji} emoji_modification?
//
// emoji_modification :=
// \p{EMod}
// | \x{FE0F} \x{20E3}?
//
// tag_modifier :=
// [\x{E0020}-\x{E007E}]+ \x{E007F}
//
// There should not be false negative (glyph wrongly detected as an emoji)
// There is false positive such inexistant flag indicator
func PossibleGlyph(b []byte) bool {
_, ok, n := Decode(b)
return ok && n == len(b)
}
// Decode returns
// - the first complete emoji, true, and it's width in bytes is available
// - the full non emoji sequence, false and it's width in bytes (might be a rune or multiples in case of malformed emoji)
func Decode(b []byte) ([]byte, bool, int) {
r1, n1 := utf8.DecodeRune(b)
if n1 == 0 {
return nil, false, 0
}
u1 := uint32(r1)
if RegionalIndicator.R32[0].Lo <= u1 && u1 <= RegionalIndicator.R32[0].Hi {
r2, n2 := utf8.DecodeRune(b[n1:])
u2 := uint32(r2)
if RegionalIndicator.R32[0].Lo > u2 || u2 > RegionalIndicator.R32[0].Hi {
return []byte(string(r1)), false, n1
}
return b[:n1+n2], true, n1 + n2
}
n := n1
for unicode.Is(Emoji, r1) {
r2, n2 := utf8.DecodeRune(b[n:])
if n2 == 0 {
return b[:n], unicode.Is(ExtendedPictographic, r1), n
}
if r2 == emojiVS {
n += n2
if bytes.HasPrefix(b[n:], enclosingKeycapB) {
n += len(enclosingKeycapB)
}
r2, n2 = utf8.DecodeRune(b[n:])
if n2 == 0 {
return b[:n], true, n
}
} else if isEmod(r2) && unicode.Is(EmojiModifierBase, r1) {
n += n2
r2, n2 = utf8.DecodeRune(b[n:])
if n2 == 0 {
return b[:n], true, n
}
} else if r1 == '🏴' && isTag(r2) {
for isTag(r2) {
r2, n2 = utf8.DecodeRune(b[n:])
n += n2
}
if r2 != termTag {
return b[:n], false, n
}
r2, n2 = utf8.DecodeRune(b[n:])
if n2 == 0 {
return b[:n], true, n
}
}
if r2 != zeroWidthJoiner {
return b[:n], true, n
}
n += n2
r1, n1 = utf8.DecodeRune(b[n:])
n += n1
}
return b[:n], false, n
}
// Find returns the n first emoji in b
// of all of thems if max == -1
func Find(b []byte, max int) [][]byte {
emojis := [][]byte{}
if max == 0 {
return emojis
}
for {
g, ok, n := Decode(b)
if n == 0 {
break
}
if ok {
emojis = append(emojis, g)
if len(emojis) == max {
return emojis
}
}
b = b[n:]
}
return emojis
}
// Replace replace the n first all emoji with f(b)
// of all of thems if max == -1
func Replace(b []byte, max int, f func([]byte) []byte) []byte {
if max == 0 {
return b
}
if len(Find(b, max)) == 0 {
return b
}
var buf bytes.Buffer
var count int
for {
g, ok, n := Decode(b)
b = b[n:]
if n == 0 {
break
}
if ok {
buf.Write(f(g))
count++
if count == max {
buf.Write(b)
return buf.Bytes()
}
} else {
buf.Write(g)
}
}
return buf.Bytes()
}
func isEmod(r rune) bool {
u := uint32(r)
return EmojiModifier.R32[0].Lo <= u && u <= EmojiModifier.R32[0].Hi
}