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) {