Add iterator for strings
This commit is contained in:
parent
11c9d9498b
commit
bcb643ee78
1 changed files with 43 additions and 0 deletions
43
iterator.go
Normal file
43
iterator.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
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,
|
||||
}
|
||||
}
|
||||
}()
|
||||
return c
|
||||
|
||||
}
|
Loading…
Reference in a new issue