From 2226be777a6925095420359e3ea2d1b5c672b02f Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Sun, 28 Mar 2021 19:06:42 +0800 Subject: [PATCH] fix: reduce limitedString memory cost (cherry picked from commit 5b0c693aba387e17c98ba0ecc817baae4eb98b2e) --- coolq/api.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/coolq/api.go b/coolq/api.go index 83a5fea..b8c8955 100644 --- a/coolq/api.go +++ b/coolq/api.go @@ -4,10 +4,6 @@ import ( "crypto/md5" "encoding/hex" "fmt" - "github.com/Mrs4s/MiraiGo/binary" - "github.com/Mrs4s/MiraiGo/client" - "github.com/Mrs4s/MiraiGo/message" - "github.com/tidwall/gjson" "io/ioutil" "math" "os" @@ -17,9 +13,16 @@ import ( "strconv" "strings" "time" + "unicode/utf8" + + "github.com/Mrs4s/MiraiGo/binary" + "github.com/Mrs4s/MiraiGo/client" + "github.com/Mrs4s/MiraiGo/message" + "github.com/Mrs4s/MiraiGo/utils" + log "github.com/sirupsen/logrus" + "github.com/tidwall/gjson" "github.com/Mrs4s/go-cqhttp/global" - log "github.com/sirupsen/logrus" ) // Version go-cqhttp的版本信息,在编译时使用ldflags进行覆盖 @@ -1354,10 +1357,15 @@ func convertGroupMemberInfo(groupID int64, m *client.GroupMemberInfo) MSG { } func limitedString(str string) string { - if strings.Count(str, "") <= 10 { + if utf8.RuneCountInString(str) <= 10 { return str } - limited := []rune(str) - limited = limited[:10] + b := utils.S2B(str) + limited := make([]rune, 0, 10) + for i := 0; i < 10; i++ { + decodeRune, size := utf8.DecodeRune(b) + b = b[size:] + limited = append(limited, decodeRune) + } return string(limited) + " ..." }