github.com/System-Glitch/goyave/v2@v2.10.3-0.20200819142921-51011e75d504/validation/regex.go (about)

     1  package validation
     2  
     3  import (
     4  	"regexp"
     5  	"sync"
     6  )
     7  
     8  const (
     9  	patternAlpha        string = "^[\\pL\\pM]+$"
    10  	patternAlphaDash    string = "^[\\pL\\pM0-9_-]+$"
    11  	patternAlphaNumeric string = "^[\\pL\\pM0-9]+$"
    12  	patternDigits       string = "[^0-9]"
    13  	patternEmail        string = "^[^@\\r\\n\\t]{1,64}@[^\\s]+$"
    14  )
    15  
    16  var (
    17  	regexCache = make(map[string]*regexp.Regexp, 5)
    18  	mu         = sync.RWMutex{}
    19  )
    20  
    21  func getRegex(pattern string) *regexp.Regexp {
    22  	mu.RLock()
    23  	regex, exists := regexCache[pattern]
    24  	mu.RUnlock()
    25  	if !exists {
    26  		regex = regexp.MustCompile(pattern)
    27  		mu.Lock()
    28  		regexCache[pattern] = regex
    29  		mu.Unlock()
    30  	}
    31  	return regex
    32  }
    33  
    34  // ClearRegexCache empties the validation regex cache.
    35  // Note that if validation.Validate is subsequently called, regex will need
    36  // to be recompiled.
    37  func ClearRegexCache() {
    38  	regexCache = make(map[string]*regexp.Regexp, 5)
    39  }