From ea6982fceaffb3e460771bc2a5c5e7f2379de9a5 Mon Sep 17 00:00:00 2001 From: astaxie Date: Tue, 5 Aug 2014 23:52:06 +0800 Subject: [PATCH] beego: template fund when the start> len(bt) --- templatefunc.go | 3 +++ templatefunc_test.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/templatefunc.go b/templatefunc.go index da1ebc56..f72017e1 100644 --- a/templatefunc.go +++ b/templatefunc.go @@ -27,6 +27,9 @@ func Substr(s string, start, length int) string { if start < 0 { start = 0 } + if start > len(bt) { + start = start % len(bt) + } var end int if (start + length) > (len(bt) - 1) { end = len(bt) diff --git a/templatefunc_test.go b/templatefunc_test.go index fabbb431..49807f12 100644 --- a/templatefunc_test.go +++ b/templatefunc_test.go @@ -25,6 +25,9 @@ func TestSubstr(t *testing.T) { if Substr(s, 0, 100) != "012345" { t.Error("should be equal") } + if Substr(s, 12, 100) != "012345" { + t.Error("should be equal") + } } func TestHtml2str(t *testing.T) {