From 7a694b396244e3d572ef93d14f600a9fa7449928 Mon Sep 17 00:00:00 2001 From: God Date: Thu, 20 Jun 2024 20:37:48 +0800 Subject: [PATCH] Optimizing string split --- controller/admin_ratelimit_setting.go | 2 +- controller/base_controller.go | 5 +++-- controller/topic_detail.go | 2 +- cronjob/job_check_spider_ip.go | 5 +++-- cronjob/send_mail.go | 4 ++-- cronjob/topic_tag.go | 7 ++++--- model/comment.go | 3 +-- model/setting.go | 7 ++++--- model/topic.go | 8 ++++---- util/common.go | 22 +++++++++++++++++++--- util/content_fmt.go | 6 +++--- 11 files changed, 45 insertions(+), 26 deletions(-) diff --git a/controller/admin_ratelimit_setting.go b/controller/admin_ratelimit_setting.go index 68acbde..d186fe3 100644 --- a/controller/admin_ratelimit_setting.go +++ b/controller/admin_ratelimit_setting.go @@ -59,7 +59,7 @@ func (h *BaseHandler) AdminRateLimitSettingPost(ctx *fasthttp.RequestCtx) { // reset stValue if v == model.SettingKeyAllowIp || v == model.SettingKeyBadIp { var lis []string - for _, ip := range strings.Split(stValue, ",") { + for _, ip := range util.StringSplit(stValue, ",") { lis = append(lis, util.IpTrimRightDot(ip)) } stValue = strings.Join(lis, ",") diff --git a/controller/base_controller.go b/controller/base_controller.go index df3b432..87e4656 100644 --- a/controller/base_controller.go +++ b/controller/base_controller.go @@ -6,6 +6,7 @@ import ( "github.com/ego008/sdb" "github.com/valyala/fasthttp" "goyoubbs/model" + "goyoubbs/util" "net" "strconv" "strings" @@ -28,7 +29,7 @@ func ReadUserIP(ctx *fasthttp.RequestCtx) string { } ips := string(ctx.Request.Header.Peek(fasthttp.HeaderXForwardedFor)) - splitIps := strings.Split(ips, ",") + splitIps := util.StringSplit(ips, ",") for _, ip := range splitIps { netIP = net.ParseIP(ip) if netIP != nil { @@ -37,7 +38,7 @@ func ReadUserIP(ctx *fasthttp.RequestCtx) string { } ips = string(ctx.Request.Header.Peek("X-FORWARDED-FOR")) - splitIps = strings.Split(ips, ",") + splitIps = util.StringSplit(ips, ",") for _, ip := range splitIps { netIP = net.ParseIP(ip) if netIP != nil { diff --git a/controller/topic_detail.go b/controller/topic_detail.go index d2057bc..6ee2117 100644 --- a/controller/topic_detail.go +++ b/controller/topic_detail.go @@ -162,7 +162,7 @@ func (h *BaseHandler) TopicDetailPage(ctx *fasthttp.RequestCtx) { evn.DefaultNode = node evn.OldTopic, evn.NewTopic = model.ArticleGetNearby(db, topic.ID) if len(topic.Tags) > 0 { - for _, v := range strings.Split(topic.Tags, ",") { + for _, v := range util.StringSplit(topic.Tags, ",") { evn.TagLst = append(evn.TagLst, model.TagFontSize{ Name: v, Size: 0, diff --git a/cronjob/job_check_spider_ip.go b/cronjob/job_check_spider_ip.go index bd41224..045d2d9 100644 --- a/cronjob/job_check_spider_ip.go +++ b/cronjob/job_check_spider_ip.go @@ -4,6 +4,7 @@ import ( "github.com/ego008/goutils/json" "github.com/ego008/sdb" "goyoubbs/model" + "goyoubbs/util" "log" "net" "sort" @@ -79,7 +80,7 @@ func spiderIpCheck(db *sdb.DB) { var prefix string if isGood { - ss := strings.Split(obj.Ip, ".") + ss := util.StringSplit(obj.Ip, ".") prefix = strings.Join(ss[:2], ".") // get two part lastPn, _ := strconv.Atoi(ss[1]) if lastPn < 26 { @@ -93,7 +94,7 @@ func spiderIpCheck(db *sdb.DB) { if len(prefix) > 0 { // auto add prefix to AllowIpPrefixLst - ips := strings.Split(db.Hget(model.TbnSetting, wkeyB).String(), ",") + ips := util.StringSplit(db.Hget(model.TbnSetting, wkeyB).String(), ",") for _, ip := range ips { if strings.HasPrefix(ip, prefix) { addPrefixToWhite = false diff --git a/cronjob/send_mail.go b/cronjob/send_mail.go index 4db4b38..30d8990 100644 --- a/cronjob/send_mail.go +++ b/cronjob/send_mail.go @@ -6,10 +6,10 @@ import ( "github.com/ego008/goutils/json" "github.com/ego008/sdb" "goyoubbs/model" + "goyoubbs/util" "log" "net" "net/smtp" - "strings" ) func sendMail(db *sdb.DB, scf *model.SiteConf) { @@ -39,7 +39,7 @@ func sendMail(db *sdb.DB, scf *model.SiteConf) { header := make(map[string]string) - fromName := strings.Split(email, "@")[0] + fromName := util.StringSplit(email, "@")[0] header["From"] = fromName + "<" + email + ">" header["To"] = toEmail header["Subject"] = subject diff --git a/cronjob/topic_tag.go b/cronjob/topic_tag.go index 56eb4da..28b59aa 100644 --- a/cronjob/topic_tag.go +++ b/cronjob/topic_tag.go @@ -7,6 +7,7 @@ import ( "github.com/ego008/sdb" "github.com/valyala/fasthttp" "goyoubbs/model" + "goyoubbs/util" "strings" ) @@ -79,7 +80,7 @@ func getTagFromTitle(db *sdb.DB, apiUrl string) { // log.Println(t.Code, t.Tag) if t.Code == 200 { if len(t.Tag) > 0 { - tags := strings.Split(t.Tag, ",") + tags := util.StringSplit(t.Tag, ",") if len(tags) > 5 { tags = tags[:5] } @@ -120,8 +121,8 @@ func setArticleTag(mc *fastcache.Cache, db *sdb.DB) { //log.Println("aid", info.Id) // set tag - oldTag := strings.Split(info.OldTags, ",") - newTag := strings.Split(info.NewTags, ",") + oldTag := util.StringSplit(info.OldTags, ",") + newTag := util.StringSplit(info.NewTags, ",") // remove for _, tag1 := range oldTag { diff --git a/model/comment.go b/model/comment.go index 06177a4..0c9942f 100644 --- a/model/comment.go +++ b/model/comment.go @@ -6,7 +6,6 @@ import ( "github.com/ego008/sdb" "goyoubbs/util" "strconv" - "strings" "time" ) @@ -193,7 +192,7 @@ func CommentGetRecent(mc *fastcache.Cache, db *sdb.DB, limit int) (objLst []Comm commentMap := map[string]Comment{} // tidCid:Comment topicCommentMap := map[string][][]byte{} // topicIdStr: [][]byte(commentId) // 无序 db.Hrscan("recent_comment", nil, limit).KvEach(func(key, value sdb.BS) { - tidStr := strings.Split(key.String(), "_")[1] + tidStr := util.StringSplit(key.String(), "_")[1] k := CommentTbName + tidStr tidCid := tidStr + "_" + strconv.FormatUint(sdb.B2i(value.Bytes()), 10) sortKeyLst = append(sortKeyLst, tidCid) diff --git a/model/setting.go b/model/setting.go index 60f1b8d..8e00a4a 100644 --- a/model/setting.go +++ b/model/setting.go @@ -2,6 +2,7 @@ package model import ( "github.com/ego008/sdb" + "goyoubbs/util" "strings" ) @@ -50,7 +51,7 @@ func UpdateBadBotName(db *sdb.DB) { // BadBotNameMap if rs := db.Hget(TbnSetting, sdb.S2b(SettingKeyBadBot)); rs.OK() { curMap := Map{} - for _, line := range strings.Split(string(rs.Data[0]), ",") { + for _, line := range util.StringSplit(string(rs.Data[0]), ",") { line = strings.TrimSpace(line) if len(line) == 0 { continue @@ -69,7 +70,7 @@ func UpdateBadIpPrefix(db *sdb.DB) { if rs := db.Hget(TbnSetting, sdb.S2b(SettingKeyBadIp)); rs.OK() { var tmpLst []string kMap := map[string]struct{}{} - for _, line := range strings.Split(string(rs.Data[0]), ",") { + for _, line := range util.StringSplit(string(rs.Data[0]), ",") { line = strings.TrimSpace(line) if len(line) == 0 { continue @@ -88,7 +89,7 @@ func UpdateAllowIpPrefix(db *sdb.DB) { if rs := db.Hget(TbnSetting, sdb.S2b(SettingKeyAllowIp)); rs.OK() { var tmpLst []string kMap := map[string]struct{}{} - for _, line := range strings.Split(string(rs.Data[0]), ",") { + for _, line := range util.StringSplit(string(rs.Data[0]), ",") { line = strings.TrimSpace(line) if len(line) == 0 { continue diff --git a/model/topic.go b/model/topic.go index 23fbe6f..678f548 100644 --- a/model/topic.go +++ b/model/topic.go @@ -170,7 +170,7 @@ func TopicDel(mc *fastcache.Cache, db *sdb.DB, obj Topic) { // 标签 if len(obj.Tags) > 0 { // 删除标签,参见 cronjob/topic_tag - for _, tag := range strings.Split(obj.Tags, ",") { + for _, tag := range util.StringSplit(obj.Tags, ",") { tagLower := strings.ToLower(tag) tagLowerB := sdb.S2b(tagLower) _ = db.Hdel("tag:"+tagLower, sdb.I2b(obj.ID)) @@ -239,7 +239,7 @@ func TopicGetRelative(mc *fastcache.Cache, db *sdb.DB, aid uint64, tags string) aidCount := map[uint64]int{} - for _, tag := range strings.Split(tagsLow, ",") { + for _, tag := range util.StringSplit(tagsLow, ",") { rs := db.Hrscan("tag:"+tag, nil, scanMax) if rs.KvLen() > 0 { for i := 0; i < len(rs.Data)-1; i += 2 { @@ -478,7 +478,7 @@ func GetTopicListArchives(db *sdb.DB, cmd, tb, key string, limit int) TopicPageI addYearMap := map[string]struct{}{} for _, article := range aitems { - addTimeLst := strings.Split(util.TimeFmt(article.AddTime, "2006 Jan 02"), " ") + addTimeLst := util.StringSplit(util.TimeFmt(article.AddTime, "2006 Jan 02"), " ") item := TopicLstLi{ Topic: article, FirstCon: util.GetDesc(article.Content), @@ -605,7 +605,7 @@ func SearchTopicList(mc *fastcache.Cache, db *sdb.DB, q string, limit int) (tInf addYearMap := map[string]struct{}{} for _, article := range aitems { - addTimeLst := strings.Split(util.TimeFmt(article.AddTime, "2006 Jan 02"), " ") + addTimeLst := util.StringSplit(util.TimeFmt(article.AddTime, "2006 Jan 02"), " ") article.Comments, _ = commentsMap[article.ID] item := TopicLstLi{ Topic: article, diff --git a/util/common.go b/util/common.go index 9073db5..65c726f 100644 --- a/util/common.go +++ b/util/common.go @@ -25,7 +25,7 @@ func Xxhash(s []byte) uint64 { // https://stackoverflow.com/questions/tagged/go?tab=newest&page=2922&pagesize=15 // -> https://stackoverflow.com stackoverflow.com func GetDomainFromURL(fullURL string) (bsURL, host string) { - urls := strings.Split(fullURL, "/") + urls := StringSplit(fullURL, "/") if len(urls) > 2 { host = urls[2] } else { @@ -40,7 +40,7 @@ func SliceUniqStr(s, sep string) string { if len(sep) == 0 { sep = "," } - ss := strings.Split(s, sep) + ss := StringSplit(s, sep) seen := make(map[string]struct{}, len(ss)) j := 0 for _, v := range ss { @@ -73,7 +73,7 @@ func IpTrimRightDot(s string) string { } s = s[:len(s)-1] - ss := strings.Split(s, ".") + ss := StringSplit(s, ".") lastPn, _ := strconv.Atoi(ss[len(ss)-1]) if lastPn > 25 { return s @@ -102,3 +102,19 @@ func TenTo62(id uint64) string { } return string(shortUrl) } + +// StringSplit same as strings.Split +func StringSplit(str string, sep string) []string { + var words []string + var eoc int + for eoc != -1 { + eoc = strings.Index(str, sep) + if eoc == -1 { + words = append(words, str) + break + } + words = append(words, str[:eoc]) + str = str[eoc+len(sep):] + } + return words +} diff --git a/util/content_fmt.go b/util/content_fmt.go index 8904c9e..06b38d8 100644 --- a/util/content_fmt.go +++ b/util/content_fmt.go @@ -59,7 +59,7 @@ func tableCode(text, lang string) string { text = strings.TrimSpace(text) var codes []string var lines []string - for i, line := range strings.Split(text, "\n") { + for i, line := range StringSplit(text, "\n") { lines = append(lines, fmt.Sprintf(`%d`, i+1)) codes = append(codes, fmt.Sprintf(`%s`, line)) } @@ -105,7 +105,7 @@ func ContentFmt(input string) string { input = codeBlockRegexp.ReplaceAllStringFunc(input, func(s string) string { s = strings.TrimSpace(s) // important // 获取并代码头部信息及处理代码高亮 html 代码 - lines := strings.Split(s, "\n") + lines := StringSplit(s, "\n") // 至少 3 行 if len(lines) >= 3 { caption := "" // title @@ -213,7 +213,7 @@ func GetDesc(input string) (des string) { return } - firstBrCon := strings.Split(input, "\n")[0] + firstBrCon := StringSplit(input, "\n")[0] if len(firstBrCon) > limit { runeCon := []rune(firstBrCon)