mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-18 21:11:19 +02:00
tpl: Add strings.Repeat
This commit is contained in:
committed by
Bjørn Erik Pedersen
parent
07b96d16e8
commit
13435a6f60
@@ -17,6 +17,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"math"
|
||||
_strings "strings"
|
||||
"unicode/utf8"
|
||||
|
||||
@@ -417,3 +418,23 @@ func (ns *Namespace) TrimSuffix(suffix, s interface{}) (string, error) {
|
||||
|
||||
return _strings.TrimSuffix(ss, sx), nil
|
||||
}
|
||||
|
||||
// Repeat returns a new string consisting of count copies of the string s.
|
||||
// The count is limited to an in16 value (up to 32767).
|
||||
func (ns *Namespace) Repeat(n, s interface{}) (string, error) {
|
||||
ss, err := cast.ToStringE(s)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
sn, err := cast.ToIntE(n)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if sn > math.MaxInt16 {
|
||||
return "", fmt.Errorf("Cannot repeat string more than %d times", math.MaxInt16)
|
||||
}
|
||||
|
||||
return _strings.Repeat(ss, sn), nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user