Skip to content

Commit

Permalink
fix: reduce limitedString memory cost
Browse files Browse the repository at this point in the history
(cherry picked from commit 5b0c693)
  • Loading branch information
wdvxdr1123 committed Apr 30, 2021
1 parent 127d636 commit 2226be7
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions coolq/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -17,9 +13,16 @@ import (
"strconv"
"strings"
"time"
"unicode/utf8"

"github.com/Mrs4s/go-cqhttp/global"
"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"
)

// Version go-cqhttp的版本信息,在编译时使用ldflags进行覆盖
Expand Down Expand Up @@ -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) + " ..."
}

0 comments on commit 2226be7

Please sign in to comment.