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

Refactor all pre-compiled regexp to package level vars
This commit is contained in:
Tibor Vass 2014-09-16 15:04:23 -04:00
commit a6dc61ec4c
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