2018-11-16 10:39:48 +00:00
|
|
|
package emoji
|
|
|
|
|
|
|
|
// Fragment is either a rune or an emoji
|
|
|
|
type Fragment struct {
|
|
|
|
Offset int
|
|
|
|
IsEmoji bool
|
|
|
|
Emoji Emoji
|
|
|
|
Rune rune
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate iterates through a string and returns its runes (for characters) and emojis
|
|
|
|
func (em Table) Iterate(str string) <-chan Fragment {
|
|
|
|
c := make(chan Fragment)
|
|
|
|
go func() {
|
|
|
|
nextchar := 0
|
|
|
|
for index, r := range str {
|
|
|
|
// Check if we need to skip entries
|
|
|
|
if nextchar > index {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Check if rune is an emoji
|
|
|
|
if em.IsEmoji(r) {
|
|
|
|
icon := em.Find(str[index:])
|
|
|
|
if icon != nil {
|
|
|
|
nextchar = index + icon.Length() - 1
|
|
|
|
c <- Fragment{
|
|
|
|
IsEmoji: true,
|
|
|
|
Offset: index,
|
|
|
|
Emoji: *icon,
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c <- Fragment{
|
|
|
|
IsEmoji: false,
|
|
|
|
Offset: index,
|
|
|
|
Rune: r,
|
|
|
|
}
|
|
|
|
}
|
2018-11-16 10:45:24 +00:00
|
|
|
close(c)
|
2018-11-16 10:39:48 +00:00
|
|
|
}()
|
|
|
|
return c
|
|
|
|
|
|
|
|
}
|