fix issue in StrLimit for gstr

This commit is contained in:
John
2019-10-10 00:04:38 +08:00
parent 5f36614dd7
commit 3082c7f761
3 changed files with 36 additions and 1 deletions

View File

@ -0,0 +1,16 @@
package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
)
func main() {
tplContent := `
{{"我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人"| strlimit 10 "..."}}
`
content, err := g.View().ParseContent(tplContent, nil)
fmt.Println(err)
fmt.Println(content)
}

View File

@ -0,0 +1,19 @@
package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
)
func main() {
s := "我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人"
tplContent := `
{{.str | strlimit 10 "..."}}
`
content, err := g.View().ParseContent(tplContent, g.Map{
"str": s,
})
fmt.Println(err)
fmt.Println(content)
}

View File

@ -186,7 +186,7 @@ func SubStr(str string, start int, length ...int) (substr string) {
// then the <suffix> will be appended to the result string.
func StrLimit(str string, length int, suffix ...string) string {
rs := []rune(str)
if len(str) < length {
if len(rs) < length {
return str
}
addStr := "..."