From 4119c9d7d955757092adc3410da8d75f972bd3c1 Mon Sep 17 00:00:00 2001 From: Phil Estes Date: Mon, 15 Sep 2014 23:30:10 -0400 Subject: [PATCH] Refactor all pre-compiled regexp to package level vars Addresses #8057 Docker-DCO-1.1-Signed-off-by: Phil Estes --- builder/support.go | 2 +- opts/opts.go | 11 +++++++---- pkg/networkfs/resolvconf/resolvconf.go | 11 +++++++---- pkg/units/size.go | 7 +------ registry/registry.go | 4 ++-- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/builder/support.go b/builder/support.go index 16fcea5f19..9ea439b53f 100644 --- a/builder/support.go +++ b/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. diff --git a/opts/opts.go b/opts/opts.go index 0ed0f62bcd..f57ef8b6d1 100644 --- a/opts/opts.go +++ b/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 } diff --git a/pkg/networkfs/resolvconf/resolvconf.go b/pkg/networkfs/resolvconf/resolvconf.go index 38ae56432d..9165caeaaa 100644 --- a/pkg/networkfs/resolvconf/resolvconf.go +++ b/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 } diff --git a/pkg/units/size.go b/pkg/units/size.go index 88d91dd99e..ea39bbddf7 100644 --- a/pkg/units/size.go +++ b/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 diff --git a/registry/registry.go b/registry/registry.go index c773bd57f5..a2e1fbdd1d 100644 --- a/registry/registry.go +++ b/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) }