1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-06 20:13:50 +08:00

fix: reduce limitedString memory cost

(cherry picked from commit 5b0c693aba387e17c98ba0ecc817baae4eb98b2e)
This commit is contained in:
wdvxdr 2021-03-28 19:06:42 +08:00
parent 127d636b8a
commit 2226be777a
No known key found for this signature in database
GPG Key ID: 55FF1414A69CEBA6

View File

@ -4,10 +4,6 @@ import (
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/client"
"github.com/Mrs4s/MiraiGo/message"
"github.com/tidwall/gjson"
"io/ioutil" "io/ioutil"
"math" "math"
"os" "os"
@ -17,9 +13,16 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "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" "github.com/Mrs4s/go-cqhttp/global"
log "github.com/sirupsen/logrus"
) )
// Version go-cqhttp的版本信息在编译时使用ldflags进行覆盖 // Version go-cqhttp的版本信息在编译时使用ldflags进行覆盖
@ -1354,10 +1357,15 @@ func convertGroupMemberInfo(groupID int64, m *client.GroupMemberInfo) MSG {
} }
func limitedString(str string) string { func limitedString(str string) string {
if strings.Count(str, "") <= 10 { if utf8.RuneCountInString(str) <= 10 {
return str return str
} }
limited := []rune(str) b := utils.S2B(str)
limited = limited[:10] 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) + " ..." return string(limited) + " ..."
} }