mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-17 21:01:26 +02:00
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
This commit is contained in:
committed by
Bjørn Erik Pedersen
parent
788e50ad3a
commit
8a26ab0bc5
@@ -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 {
|
||||
|
Reference in New Issue
Block a user