emoji/iterator.go

45 lines
825 B
Go

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,
}
}
close(c)
}()
return c
}