Skip to content

Commit

Permalink
Optimizing string split
Browse files Browse the repository at this point in the history
  • Loading branch information
ego008 committed Jun 20, 2024
1 parent e651608 commit 7a694b3
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 26 deletions.
2 changes: 1 addition & 1 deletion controller/admin_ratelimit_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ",")
Expand Down
5 changes: 3 additions & 2 deletions controller/base_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/ego008/sdb"
"github.com/valyala/fasthttp"
"goyoubbs/model"
"goyoubbs/util"
"net"
"strconv"
"strings"
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion controller/topic_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions cronjob/job_check_spider_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/ego008/goutils/json"
"github.com/ego008/sdb"
"goyoubbs/model"
"goyoubbs/util"
"log"
"net"
"sort"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cronjob/send_mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions cronjob/topic_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ego008/sdb"
"github.com/valyala/fasthttp"
"goyoubbs/model"
"goyoubbs/util"
"strings"
)

Expand Down Expand Up @@ -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]
}
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions model/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/ego008/sdb"
"goyoubbs/util"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions model/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
"github.com/ego008/sdb"
"goyoubbs/util"
"strings"
)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions model/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 19 additions & 3 deletions util/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions util/content_fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(`<span class="line-number">%d</span>`, i+1))
codes = append(codes, fmt.Sprintf(`<span class="line">%s</span>`, line))
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7a694b3

Please sign in to comment.