Allow library to be used in parallel

This PR allows concurrent calls to Parse. Further it makes the test be
part of the same package.

Fixes https://github.com/gorhill/cronexpr/issues/26
This commit is contained in:
Alex Dadgar
2017-09-15 11:30:32 -07:00
parent d520615e53
commit 675cac9b2d
3 changed files with 24 additions and 23 deletions

View File

@@ -19,6 +19,7 @@ import (
"regexp"
"sort"
"strings"
"sync"
)
/******************************************************************************/
@@ -194,6 +195,7 @@ var (
fieldFinder = regexp.MustCompile(`\S+`)
entryFinder = regexp.MustCompile(`[^,]+`)
layoutRegexp = make(map[string]*regexp.Regexp)
layoutRegexpLock sync.Mutex
)
/******************************************************************************/
@@ -488,6 +490,9 @@ func genericFieldParse(s string, desc fieldDescriptor) ([]*cronDirective, error)
/******************************************************************************/
func makeLayoutRegexp(layout, value string) *regexp.Regexp {
layoutRegexpLock.Lock()
defer layoutRegexpLock.Unlock()
layout = strings.Replace(layout, `%value%`, value, -1)
re := layoutRegexp[layout]
if re == nil {