ref: 5862fd2a60b5d16f2437bd8c8b7bac700de5f047
parent: 64789fb5dcf8326f14f13d69a2576ae3aa2bbbaa
author: Cameron Moore <moorereason@gmail.com>
date: Sat Nov 28 04:56:49 EST 2020
tpl: Fix substr when length parameter is zero When length parameter is zero, always return an empty string. Updates #7993
--- a/tpl/strings/strings.go
+++ b/tpl/strings/strings.go
@@ -327,9 +327,12 @@
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
}
--- a/tpl/strings/strings_test.go
+++ b/tpl/strings/strings_test.go
@@ -441,6 +441,9 @@
}{
{"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 @@
}
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")