From 5b594f4dd23a9f5f0943adefc142829cf84efa05 Mon Sep 17 00:00:00 2001 From: Christoph Maser Date: Wed, 31 Jul 2019 11:27:52 +0200 Subject: [PATCH] address review on !195 - fix kernel doc link - tune error message - comile regex only once - remove unneccesary string prepend Signed-off-by: Christoph Maser --- zoneinfo.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/zoneinfo.go b/zoneinfo.go index 6cc2d1eb3..e941503d5 100644 --- a/zoneinfo.go +++ b/zoneinfo.go @@ -66,13 +66,15 @@ type Zoneinfo struct { Protection []*int64 } +var nodeZoneRE = regexp.MustCompile(`(\d+), zone\s+(\w+)`) + // Zoneinfo parses an zoneinfo-file (/proc/zoneinfo) and returns a slice of // structs containing the relevant info. More information available here: -// https://github.com/torvalds/linux/blob/master/Documentation/sysctl/vm.txt +// https://www.kernel.org/doc/Documentation/sysctl/vm.txt func (fs FS) Zoneinfo() ([]Zoneinfo, error) { data, err := ioutil.ReadFile(fs.proc.Path("zoneinfo")) if err != nil { - return nil, fmt.Errorf("error parsing zoneinfo %s: %s", fs.proc.Path("zoneinfo"), err) + return nil, fmt.Errorf("error reading zoneinfo %s: %s", fs.proc.Path("zoneinfo"), err) } zoneinfo, err := parseZoneinfo(data) if err != nil { @@ -82,16 +84,13 @@ func (fs FS) Zoneinfo() ([]Zoneinfo, error) { } func parseZoneinfo(zoneinfoData []byte) ([]Zoneinfo, error) { - var nodeZoneRE = regexp.MustCompile(`Node (\d+), zone\s+(\w+)`) zoneinfo := []Zoneinfo{} - cryptoBlocks := bytes.Split(zoneinfoData, []byte("\nNode")) - for _, block := range cryptoBlocks { + zoneinfoBlocks := bytes.Split(zoneinfoData, []byte("\nNode")) + for _, block := range zoneinfoBlocks { var zoneinfoElement Zoneinfo - blockComplete := []byte("Node") - blockComplete = append(blockComplete, block...) - lines := strings.Split(string(blockComplete), "\n") + lines := strings.Split(string(block), "\n") for _, line := range lines { if nodeZone := nodeZoneRE.FindStringSubmatch(line); nodeZone != nil {