Refactor all pre-compiled regexp to package level vars

Addresses #8057

Docker-DCO-1.1-Signed-off-by: Phil Estes <estesp@linux.vnet.ibm.com>
This commit is contained in:
Phil Estes 2014-09-15 23:30:10 -04:00
parent ed3d01687d
commit c876988ae5
2 changed files with 8 additions and 10 deletions

View file

@ -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
}

View file

@ -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