diff options
author | mrsdizzie <info@mrsdizzie.com> | 2020-07-25 09:40:04 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-25 16:40:04 +0300 |
commit | ea1ed802a308698f6b30dd695d0b97251d552775 (patch) | |
tree | fca2988d50e47cb37a7fc48771174e277d19d3bb /modules/emoji/emoji.go | |
parent | 8baf5ca22814878334844c435bf8dd59349f5a69 (diff) | |
download | gitea-ea1ed802a308698f6b30dd695d0b97251d552775.tar.gz gitea-ea1ed802a308698f6b30dd695d0b97251d552775.zip |
Fix emoji detection in certain cases (#12320)
* Fix emoji detection certain cases
Previous tests weren't complicated enough so there were some situations where emojis were't detected properly. Find the earliest occurance in addition to checking for the longest combination.
Fixes #12312
* ok spell bot
Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'modules/emoji/emoji.go')
-rw-r--r-- | modules/emoji/emoji.go | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/modules/emoji/emoji.go b/modules/emoji/emoji.go index e2c3d202e2..169ee0a182 100644 --- a/modules/emoji/emoji.go +++ b/modules/emoji/emoji.go @@ -130,6 +130,8 @@ func ReplaceAliases(s string) string { // FindEmojiSubmatchIndex returns index pair of longest emoji in a string func FindEmojiSubmatchIndex(s string) []int { loadMap() + found := make(map[int]int) + keys := make([]int, 0) //see if there are any emoji in string before looking for position of specific ones //no performance difference when there is a match but 10x faster when there are not @@ -137,11 +139,26 @@ func FindEmojiSubmatchIndex(s string) []int { return nil } + // get index of first emoji occurrence while also checking for longest combination for j := range GemojiData { i := strings.Index(s, GemojiData[j].Emoji) if i != -1 { - return []int{i, i + len(GemojiData[j].Emoji)} + if _, ok := found[i]; !ok { + if len(keys) == 0 || i < keys[0] { + found[i] = j + keys = []int{i} + } + if i == 0 { + break + } + } } } + + if len(keys) > 0 { + index := keys[0] + return []int{index, index + len(GemojiData[found[index]].Emoji)} + } + return nil } |