summaryrefslogtreecommitdiffstats
path: root/tpl
diff options
context:
space:
mode:
authorCameron Moore <moorereason@gmail.com>2020-11-28 09:56:49 -0600
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-12-01 23:20:04 +0100
commit5862fd2a60b5d16f2437bd8c8b7bac700de5f047 (patch)
tree18cf44e8713cda968fb26d08aa194483b9484655 /tpl
parent64789fb5dcf8326f14f13d69a2576ae3aa2bbbaa (diff)
tpl: Fix substr when length parameter is zero
When length parameter is zero, always return an empty string. Updates #7993
Diffstat (limited to 'tpl')
-rw-r--r--tpl/strings/strings.go7
-rw-r--r--tpl/strings/strings_test.go5
2 files changed, 9 insertions, 3 deletions
diff --git a/tpl/strings/strings.go b/tpl/strings/strings.go
index 9427d8e8a..60cb658ba 100644
--- a/tpl/strings/strings.go
+++ b/tpl/strings/strings.go
@@ -327,9 +327,12 @@ func (ns *Namespace) Substr(a interface{}, nums ...interface{}) (string, error)
end := rlen
- if length < 0 {
+ switch {
+ case length == 0:
+ return "", nil
+ case length < 0:
end += length
- } else if length > 0 {
+ case length > 0:
end = start + length
}
diff --git a/tpl/strings/strings_test.go b/tpl/strings/strings_test.go
index bb90200c0..c09bfaf67 100644
--- a/tpl/strings/strings_test.go
+++ b/tpl/strings/strings_test.go
@@ -441,6 +441,9 @@ func TestSubstr(t *testing.T) {
}{
{"abc", 1, 2, "bc"},
{"abc", 0, 1, "a"},
+ {"abcdef", 0, 0, ""},
+ {"abcdef", 1, 0, ""},
+ {"abcdef", -1, 0, ""},
{"abcdef", -1, 2, "f"},
{"abcdef", -3, 3, "def"},
{"abcdef", -1, nil, "f"},
@@ -488,7 +491,7 @@ func TestSubstr(t *testing.T) {
}
c.Assert(err, qt.IsNil)
- c.Assert(result, qt.Equals, test.expect, qt.Commentf("%v", test))
+ c.Check(result, qt.Equals, test.expect, qt.Commentf("%v", test))
}
_, err = ns.Substr("abcdef")