strings.Split 不能使用换行符切割字符串?
- Dominant language
- Go
- Stars
- 2.1k
- Forks
- 281
- PR merge metrics
- No merged PRs in 30d
Description
`package main
import (
"fmt"
"github.com/bilibili/gengine/builder"
"github.com/bilibili/gengine/context"
"github.com/bilibili/gengine/engine"
"github.com/sirupsen/logrus"
"strings"
"time"
)
const rule1 = `
rule "name test" "i can" salience 0
begin
if FreeData == ""{
println("Error:data is null")
}else{
rows = split(FreeData,"\n")
println(rows[1])
}
end
`
func main() {
dataContext := context.NewDataContext()
freeData := `total used free shared buff/cache available
Mem: 131910844 41687584 24113760 4251248 66109500 84970700
Swap: 0 0 0
`
//这里可以成功切割出来
fmt.Println(strings.Split(freeData,"\n")[1])
//注入初始化的结构体
dataContext.Add("FreeData", freeData)
dataContext.Add("split", strings.Split)
dataContext.Add("println", fmt.Println)
//init rule engine
ruleBuilder := builder.NewRuleBuilder(dataContext)
start1 := time.Now().UnixNano()
//构建规则
err := ruleBuilder.BuildRuleFromString(rule1) //string(bs)
end1 := time.Now().UnixNano()
logrus.Infof("rules num:%d, load rules cost time:%d", len(ruleBuilder.Kc.RuleEntities), end1-start1 )
if err != nil{
logrus.Errorf("err:%s ", err)
}else{
eng := engine.NewGengine()
start := time.Now().UnixNano()
//执行规则
err := eng.Execute(ruleBuilder,true)
end := time.Now().UnixNano()
if err != nil{
logrus.Errorf("execute rule error: %v", err)
}
logrus.Infof("execute rule cost %d ns",end-start)
}
}
`
第19行会报错:
ERRO[0000] execute rule error: [rule: "name test" executed, error:
line 8, column 3, code: println(rows[1]), reflect: slice index out of range
输出:
Mem: 131910844 41687584 24113760 4251248 66109500 84970700
INFO[0000] rules num:1, load rules cost time:16206000
ERRO[0000] execute rule error: [rule: "name test" executed, error:
line 8, column 3, code: println(rows[1]), reflect: slice index out of range
goroutine 1 [running]:
Contributor guide
No contributing guide indexed for this repository
Research direction
Reproduce the report through builder.BuildRuleFromString and engine.Execute, comparing the direct strings.Split call with the same split function invoked inside rule1. Trace how the rule expression passes the "\n" argument and verify that the fix makes rows[1] available without changing the reported rule behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100