Skip to content

Commit

Permalink
Merge pull request #49 from kyokomi/initcosts
Browse files Browse the repository at this point in the history
Fix init costs
  • Loading branch information
kyokomi authored Dec 30, 2020
2 parents 24a0d8a + 68c6c72 commit a38e5d4
Show file tree
Hide file tree
Showing 3 changed files with 7,717 additions and 7,679 deletions.
31 changes: 24 additions & 7 deletions cmd/generateEmojiCodeMap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,36 @@ type TemplateData struct {
const templateMapCode = `
package {{.PkgName}}
import (
"sync"
)
// NOTE: THIS FILE WAS PRODUCED BY THE
// EMOJICODEMAP CODE GENERATION TOOL (github.com/kyokomi/emoji/cmd/generateEmojiCodeMap)
// DO NOT EDIT
// Mapping from character to concrete escape code.
var emojiCodeMap = map[string]string{
{{range $key, $val := .CodeMap}}":{{$key}}:": {{$val}},
{{end}}
var emojiCodeMap map[string]string
var emojiCodeMapInitOnce = sync.Once{}
func emojiCode() map[string]string {
emojiCodeMapInitOnce.Do(func() {
emojiCodeMap = map[string]string{
{{range $key, $val := .CodeMap}}":{{$key}}:": {{$val}},
{{end}}}
})
return emojiCodeMap
}
var emojiRevCodeMap = map[string][]string{
{{range $key, $val := .RevCodeMap}} {{$key}}: { {{range $val}} ":{{.}}:", {{end}} },
{{end}}
var emojiRevCodeMap map[string][]string
var emojiRevCodeMapInitOnce = sync.Once{}
func emojiRevCode() map[string][]string {
emojiRevCodeMapInitOnce.Do(func() {
emojiRevCodeMap = map[string][]string{
{{range $key, $val := .RevCodeMap}} {{$key}}: { {{range $val}} ":{{.}}:", {{end}} },
{{end}}}
})
return emojiRevCodeMap
}
`

Expand Down
8 changes: 4 additions & 4 deletions emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ var (

// CodeMap gets the underlying map of emoji.
func CodeMap() map[string]string {
return emojiCodeMap
return emojiCode()
}

// RevCodeMap gets the underlying map of emoji.
func RevCodeMap() map[string][]string {
return emojiRevCodeMap
return emojiRevCode()
}

func AliasList(shortCode string) []string {
return emojiRevCodeMap[emojiCodeMap[shortCode]]
return emojiRevCode()[emojiCode()[shortCode]]
}

// HasAlias flags if the given `shortCode` has multiple aliases with other
Expand All @@ -50,7 +50,7 @@ func NormalizeShortCode(shortCode string) string {
var flagRegexp = regexp.MustCompile(":flag-([a-z]{2}):")

func emojize(x string) string {
str, ok := emojiCodeMap[x]
str, ok := emojiCode()[x]
if ok {
return str + ReplacePadding
}
Expand Down
Loading

0 comments on commit a38e5d4

Please sign in to comment.