Compare commits

...

2 Commits

Author SHA1 Message Date
Hamcha bcb643ee78
Add iterator for strings 2018-11-16 11:39:48 +01:00
Hamcha 11c9d9498b
Fix package and naming 2018-11-16 11:39:41 +01:00
2 changed files with 55 additions and 12 deletions

View File

@ -1,4 +1,4 @@
package freetype
package emoji
import (
"fmt"
@ -9,12 +9,12 @@ import (
"unicode/utf8"
)
// Emoji is a node of an EmojiTable
// Emoji is a node of a Table
type Emoji struct {
Codepoint []rune
IsEmoji bool
Path string
Sub EmojiTable
Sub Table
}
func (e Emoji) String() (str string) {
@ -41,11 +41,11 @@ func (e Emoji) Length() int {
return total
}
// EmojiTable is a table of detected Unicode codepoints sequences for which an emoticon is available
type EmojiTable map[rune]Emoji
// Table is a table of detected Unicode codepoints sequences for which an emoticon is available
type Table map[rune]Emoji
// Find checks if a given strings begins with an emoji made by one or more sequential runes
func (em EmojiTable) Find(str string) *Emoji {
func (em Table) Find(str string) *Emoji {
for i, r := range str {
e, ok := em[r]
if !ok {
@ -67,12 +67,12 @@ func (em EmojiTable) Find(str string) *Emoji {
}
// IsEmoji checks whether the given rune is an emoji
func (em EmojiTable) IsEmoji(cp rune) bool {
func (em Table) IsEmoji(cp rune) bool {
_, ok := em[cp]
return ok
}
func (em EmojiTable) tostring(indent string, nomarker bool) (str string) {
func (em Table) tostring(indent string, nomarker bool) (str string) {
counter := len(em)
marker := "│ "
if nomarker {
@ -96,7 +96,7 @@ func (em EmojiTable) tostring(indent string, nomarker bool) (str string) {
return
}
func (em EmojiTable) String() string {
func (em Table) String() string {
return "Emoji table\n" + em.tostring("", true)
}
@ -105,8 +105,8 @@ func (em EmojiTable) String() string {
// emoji_uXXXX_XXXX.png
// where XXXX are Unicode codepoints
// See <https://github.com/googlei18n/noto-emoji/tree/master/png/128>
func ScanEmojiDirectory(emojipath string) (tab EmojiTable, err error) {
tab = make(EmojiTable)
func ScanEmojiDirectory(emojipath string) (tab Table, err error) {
tab = make(Table)
filepath.Walk(emojipath, func(path string, info os.FileInfo, err error) error {
// Ignore non-images
if !strings.HasSuffix(strings.ToLower(path), ".png") {
@ -143,7 +143,7 @@ func ScanEmojiDirectory(emojipath string) (tab EmojiTable, err error) {
} else {
// Add sub-entry if not existant
if newemo.Sub == nil {
newemo.Sub = make(EmojiTable)
newemo.Sub = make(Table)
}
}
(*curtab)[cprune] = newemo

43
iterator.go Normal file
View 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
}