aboutsummaryrefslogtreecommitdiffhomepage
path: root/tpl/strings/strings.go
diff options
context:
space:
mode:
authorCameron Moore <[email protected]>2021-01-03 12:01:42 -0600
committerBjørn Erik Pedersen <[email protected]>2021-01-08 10:13:10 +0100
commit8a26ab0bc5dd9fa34e1362681fc08b0e522cd4ea (patch)
tree40b069293d1edff1ae47bcb6885af3dd3734c7df /tpl/strings/strings.go
parent788e50ad3a55609ed49ce0b7ee98965c181fe9cf (diff)
downloadhugo-8a26ab0bc5dd9fa34e1362681fc08b0e522cd4ea.tar.gz
hugo-8a26ab0bc5dd9fa34e1362681fc08b0e522cd4ea.zip
tpl: Do not return errors in substr for out-of-bounds cases
Most other substr implementations don't error out in out-of-bounds cases but simply return an empty string (or a value that's printed as an empty string). We'll follow their lead and not exit template execution. Allow the user decide what to do with the empty result. Fixes #8113
Diffstat (limited to 'tpl/strings/strings.go')
-rw-r--r--tpl/strings/strings.go5
1 files changed, 2 insertions, 3 deletions
diff --git a/tpl/strings/strings.go b/tpl/strings/strings.go
index 178d17bc0..b2f02d8f5 100644
--- a/tpl/strings/strings.go
+++ b/tpl/strings/strings.go
@@ -16,7 +16,6 @@ package strings
import (
"errors"
- "fmt"
"html/template"
"strings"
"unicode/utf8"
@@ -322,7 +321,7 @@ func (ns *Namespace) Substr(a interface{}, nums ...interface{}) (string, error)
}
if start > rlen-1 {
- return "", fmt.Errorf("start position out of bounds for %d-byte string", rlen)
+ return "", nil
}
end := rlen
@@ -337,7 +336,7 @@ func (ns *Namespace) Substr(a interface{}, nums ...interface{}) (string, error)
}
if start >= end {
- return "", fmt.Errorf("calculated start position greater than end position: %d > %d", start, end)
+ return "", nil
}
if end < 0 {