From fa8dddd3150126ac23079b488f03e491e92a4d59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= <10476982+li-xunhuan@users.noreply.github.com> Date: Mon, 10 May 2021 23:39:34 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8D=20emoji=20=E8=BD=AC?= =?UTF-8?q?=E5=8C=96=E5=90=8E=E4=B8=8D=E6=AD=A3=E5=B8=B8=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E7=9A=84=20BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- emoji.go | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/emoji.go b/emoji.go index 470bb30..e533c51 100644 --- a/emoji.go +++ b/emoji.go @@ -284,19 +284,25 @@ var Emoji = struct { } func FormatEmoji(text string) string { - if result := emojiRegexp.FindAllStringSubmatch(text, -1); len(result) != 0 { - for _, item := range result { - value := item[0] - var builder strings.Builder - builder.WriteString(`\U`) - times := 10 - len(item[1]) - 2 - for i := 0; i < times; i++ { - builder.WriteString("0") - } - u := strings.ToUpper(item[1]) - builder.WriteString(u) - text = strings.ReplaceAll(text, value, builder.String()) - } + result := emojiRegexp.FindAllStringSubmatch(text, -1) + if len(result) == 0 { + return text } + + for _, item := range result { + if len(item) != 2 { + continue + } + value := item[0] + emojiCodeStr := item[1] + emojiCode, err := strconv.ParseInt(emojiCodeStr, 16, 64) + if err != nil { + continue + } + + emojiStr := html.UnescapeString("&#" + strconv.FormatInt(emojiCode, 10) + ";") + text = strings.Replace(text, value, emojiStr, -1) + } + return text }