Skip to content

Commit

Permalink
Memoize recurse regex
Browse files Browse the repository at this point in the history
  • Loading branch information
mpchadwick authored and dmgk committed Nov 14, 2020
1 parent bb8d74c commit 31d9f7f
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion faker.go
Expand Up @@ -66,6 +66,8 @@ var (
// Locale holds the default locale.
var Locale = locales.En

var recurseRegex = regexp.MustCompile(`#\{([A-Za-z]+\.[^\}]+)\}`)

// RandomInt returns random int in [min, max] range.
func RandomInt(min, max int) int {
if max <= min {
Expand Down Expand Up @@ -265,7 +267,7 @@ func Fetch(path string) string {
}

// recursively substitute #{...} value references
for _, sm := range regexp.MustCompile(`#\{([A-Za-z]+\.[^\}]+)\}`).FindAllStringSubmatch(res, -1) {
for _, sm := range recurseRegex.FindAllStringSubmatch(res, -1) {
path := strings.ToLower(sm[1])
res = strings.Replace(res, sm[0], Fetch(path), 1)
}
Expand Down

0 comments on commit 31d9f7f

Please sign in to comment.