From 3082c7f761643a8f3e3a1bb18a368d5bcb35175e Mon Sep 17 00:00:00 2001 From: John Date: Thu, 10 Oct 2019 00:04:38 +0800 Subject: [PATCH] fix issue in StrLimit for gstr --- .../os/gview/build_in_funcs/issue359-1.go | 16 ++++++++++++++++ .../os/gview/build_in_funcs/issue359-2.go | 19 +++++++++++++++++++ text/gstr/gstr.go | 2 +- 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 .example/os/gview/build_in_funcs/issue359-1.go create mode 100644 .example/os/gview/build_in_funcs/issue359-2.go diff --git a/.example/os/gview/build_in_funcs/issue359-1.go b/.example/os/gview/build_in_funcs/issue359-1.go new file mode 100644 index 000000000..e43fd267b --- /dev/null +++ b/.example/os/gview/build_in_funcs/issue359-1.go @@ -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) +} diff --git a/.example/os/gview/build_in_funcs/issue359-2.go b/.example/os/gview/build_in_funcs/issue359-2.go new file mode 100644 index 000000000..14d6a6940 --- /dev/null +++ b/.example/os/gview/build_in_funcs/issue359-2.go @@ -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) +} diff --git a/text/gstr/gstr.go b/text/gstr/gstr.go index 51a2c548c..621cb7552 100644 --- a/text/gstr/gstr.go +++ b/text/gstr/gstr.go @@ -186,7 +186,7 @@ func SubStr(str string, start int, length ...int) (substr string) { // then the 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 := "..."