mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-17 21:01:26 +02:00
tpl: Add limit support to replaceRE
Go stdlib doesn't contain a limited replace in the regexp package, but we can accomplish the same thing with ReplaceAllStringFunc. Fixes #7586
This commit is contained in:
committed by
Bjørn Erik Pedersen
parent
047af7cfe5
commit
cdfd1c99ba
@@ -46,8 +46,9 @@ func (ns *Namespace) FindRE(expr string, content interface{}, limit ...interface
|
||||
}
|
||||
|
||||
// ReplaceRE returns a copy of s, replacing all matches of the regular
|
||||
// expression pattern with the replacement text repl.
|
||||
func (ns *Namespace) ReplaceRE(pattern, repl, s interface{}) (_ string, err error) {
|
||||
// expression pattern with the replacement text repl. The number of replacements
|
||||
// can be limited with an optional fourth parameter.
|
||||
func (ns *Namespace) ReplaceRE(pattern, repl, s interface{}, n ...interface{}) (_ string, err error) {
|
||||
sp, err := cast.ToStringE(pattern)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -63,12 +64,27 @@ func (ns *Namespace) ReplaceRE(pattern, repl, s interface{}) (_ string, err erro
|
||||
return
|
||||
}
|
||||
|
||||
nn := -1
|
||||
if len(n) > 0 {
|
||||
nn, err = cast.ToIntE(n[0])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
re, err := reCache.Get(sp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return re.ReplaceAllString(ss, sr), nil
|
||||
return re.ReplaceAllStringFunc(ss, func(str string) string {
|
||||
if nn == 0 {
|
||||
return str
|
||||
}
|
||||
|
||||
nn -= 1
|
||||
return re.ReplaceAllString(str, sr)
|
||||
}), nil
|
||||
}
|
||||
|
||||
// regexpCache represents a cache of regexp objects protected by a mutex.
|
||||
|
Reference in New Issue
Block a user