From c876988ae569fd748ec2ee072cd7a6d1fa36860c 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 --- networkfs/resolvconf/resolvconf.go | 11 +++++++---- units/size.go | 7 +------ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/networkfs/resolvconf/resolvconf.go b/networkfs/resolvconf/resolvconf.go index 38ae564..9165cae 100644 --- a/networkfs/resolvconf/resolvconf.go +++ b/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/units/size.go b/units/size.go index 88d91dd..ea39bbd 100644 --- a/units/size.go +++ b/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