summaryrefslogtreecommitdiff
path: root/funcs/regexp.go
blob: ad5578fbaeca6bc0bfbd8ca75a1bd024701fb4b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package funcs

import (
	"sync"

	"github.com/hairyhenderson/gomplate/conv"
	"github.com/hairyhenderson/gomplate/regexp"
)

var (
	reNS     *ReFuncs
	reNSInit sync.Once
)

// ReNS -
func ReNS() *ReFuncs {
	reNSInit.Do(func() { reNS = &ReFuncs{} })
	return reNS
}

// AddReFuncs -
func AddReFuncs(f map[string]interface{}) {
	f["regexp"] = ReNS
}

// ReFuncs -
type ReFuncs struct{}

// Replace -
func (f *ReFuncs) Replace(re, replacement string, input interface{}) string {
	return regexp.Replace(re, replacement, conv.ToString(input))
}

// Match -
func (f *ReFuncs) Match(re string, input interface{}) bool {
	return regexp.Match(re, conv.ToString(input))
}