tpl: Add strings.Repeat

This commit is contained in:
David E. Wheeler
2018-06-03 02:55:37 -04:00
committed by Bjørn Erik Pedersen
parent 07b96d16e8
commit 13435a6f60
5 changed files with 109 additions and 0 deletions

View File

@@ -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
}