Ver código fonte

Merge pull request #8059 from estesp/8057-refactor-regexp-to-package-var

Refactor all pre-compiled regexp to package level vars
Tibor Vass 10 anos atrás
pai
commit
28f09f0632

+ 1 - 1
builder/support.go

@@ -6,7 +6,7 @@ import (
 )
 
 var (
-	TOKEN_ENV_INTERPOLATION = regexp.MustCompile("(\\\\\\\\+|[^\\\\]|\\b|\\A)\\$({?)([[:alnum:]_]+)(}?)")
+	TOKEN_ENV_INTERPOLATION = regexp.MustCompile(`(\\\\+|[^\\]|\b|\A)\$({?)([[:alnum:]_]+)(}?)`)
 )
 
 // handle environment replacement. Used in dispatcher.

+ 7 - 4
opts/opts.go

@@ -14,6 +14,11 @@ import (
 	"github.com/docker/docker/pkg/parsers"
 )
 
+var (
+	alphaRegexp  = regexp.MustCompile(`[a-zA-Z]`)
+	domainRegexp = regexp.MustCompile(`^(:?(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]))(:?\.(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])))*)\.?\s*$`)
+)
+
 func ListVar(values *[]string, names []string, usage string) {
 	flag.Var(newListOptsRef(values, nil), names, usage)
 }
@@ -184,12 +189,10 @@ func ValidateDnsSearch(val string) (string, error) {
 }
 
 func validateDomain(val string) (string, error) {
-	alpha := regexp.MustCompile(`[a-zA-Z]`)
-	if alpha.FindString(val) == "" {
+	if alphaRegexp.FindString(val) == "" {
 		return "", fmt.Errorf("%s is not a valid domain", val)
 	}
-	re := regexp.MustCompile(`^(:?(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]))(:?\.(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])))*)\.?\s*$`)
-	ns := re.FindSubmatch([]byte(val))
+	ns := domainRegexp.FindSubmatch([]byte(val))
 	if len(ns) > 0 {
 		return string(ns[1]), nil
 	}

+ 7 - 4
pkg/networkfs/resolvconf/resolvconf.go

@@ -7,6 +7,11 @@ import (
 	"strings"
 )
 
+var (
+	nsRegexp     = regexp.MustCompile(`^\s*nameserver\s*(([0-9]+\.){3}([0-9]+))\s*$`)
+	searchRegexp = regexp.MustCompile(`^\s*search\s*(([^\s]+\s*)*)$`)
+)
+
 func Get() ([]byte, error) {
 	resolv, err := ioutil.ReadFile("/etc/resolv.conf")
 	if err != nil {
@@ -33,9 +38,8 @@ func getLines(input []byte, commentMarker []byte) [][]byte {
 // GetNameservers returns nameservers (if any) listed in /etc/resolv.conf
 func GetNameservers(resolvConf []byte) []string {
 	nameservers := []string{}
-	re := regexp.MustCompile(`^\s*nameserver\s*(([0-9]+\.){3}([0-9]+))\s*$`)
 	for _, line := range getLines(resolvConf, []byte("#")) {
-		var ns = re.FindSubmatch(line)
+		var ns = nsRegexp.FindSubmatch(line)
 		if len(ns) > 0 {
 			nameservers = append(nameservers, string(ns[1]))
 		}
@@ -58,10 +62,9 @@ func GetNameserversAsCIDR(resolvConf []byte) []string {
 // If more than one search line is encountered, only the contents of the last
 // one is returned.
 func GetSearchDomains(resolvConf []byte) []string {
-	re := regexp.MustCompile(`^\s*search\s*(([^\s]+\s*)*)$`)
 	domains := []string{}
 	for _, line := range getLines(resolvConf, []byte("#")) {
-		match := re.FindSubmatch(line)
+		match := searchRegexp.FindSubmatch(line)
 		if match == nil {
 			continue
 		}

+ 1 - 6
pkg/units/size.go

@@ -29,14 +29,9 @@ type unitMap map[string]int64
 var (
 	decimalMap = unitMap{"k": KB, "m": MB, "g": GB, "t": TB, "p": PB}
 	binaryMap  = unitMap{"k": KiB, "m": MiB, "g": GiB, "t": TiB, "p": PiB}
+	sizeRegex  = regexp.MustCompile(`^(\d+)([kKmMgGtTpP])?[bB]?$`)
 )
 
-var sizeRegex *regexp.Regexp
-
-func init() {
-	sizeRegex = regexp.MustCompile("^(\\d+)([kKmMgGtTpP])?[bB]?$")
-}
-
 var unitAbbrs = [...]string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
 
 // HumanSize returns a human-readable approximation of a size

+ 2 - 2
registry/registry.go

@@ -24,6 +24,8 @@ var (
 	ErrInvalidRepositoryName = errors.New("Invalid repository name (ex: \"registry.domain.tld/myrepos\")")
 	errLoginRequired         = errors.New("Authentication is required.")
 	validHex                 = regexp.MustCompile(`^([a-f0-9]{64})$`)
+	validNamespace           = regexp.MustCompile(`^([a-z0-9_]{4,30})$`)
+	validRepo                = regexp.MustCompile(`^([a-z0-9-_.]+)$`)
 )
 
 type TimeoutType uint32
@@ -216,11 +218,9 @@ func validateRepositoryName(repositoryName string) error {
 		namespace = nameParts[0]
 		name = nameParts[1]
 	}
-	validNamespace := regexp.MustCompile(`^([a-z0-9_]{4,30})$`)
 	if !validNamespace.MatchString(namespace) {
 		return fmt.Errorf("Invalid namespace name (%s), only [a-z0-9_] are allowed, size between 4 and 30", namespace)
 	}
-	validRepo := regexp.MustCompile(`^([a-z0-9-_.]+)$`)
 	if !validRepo.MatchString(name) {
 		return fmt.Errorf("Invalid repository name (%s), only [a-z0-9-_.] are allowed", name)
 	}