From 11bc5b3970859642148dbd0fa3157c4945498a5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 21 Dec 2023 09:59:41 +0200 Subject: [PATCH] Avoid some uses of regexps --- cpu/cpu_aix_nocgo.go | 11 ++++------- disk/disk_aix_nocgo.go | 3 +-- mem/mem_aix_nocgo.go | 7 ++----- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/cpu/cpu_aix_nocgo.go b/cpu/cpu_aix_nocgo.go index 1a291532a..a77b4dbb7 100644 --- a/cpu/cpu_aix_nocgo.go +++ b/cpu/cpu_aix_nocgo.go @@ -5,15 +5,12 @@ package cpu import ( "context" - "regexp" "strconv" "strings" "github.com/shirou/gopsutil/v3/internal/common" ) -var whiteSpaces = regexp.MustCompile(`\s+`) - func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { if percpu { return []TimesStat{}, common.ErrNotImplementedError @@ -28,8 +25,8 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { } ret := TimesStat{CPU: "cpu-total"} - h := whiteSpaces.Split(lines[len(lines)-3], -1) // headers - v := whiteSpaces.Split(lines[len(lines)-2], -1) // values + h := strings.Fields(lines[len(lines)-3]) // headers + v := strings.Fields(lines[len(lines)-2]) // values for i, header := range h { if t, err := strconv.ParseFloat(v[i], 64); err == nil { switch header { @@ -58,14 +55,14 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) { ret := InfoStat{} for _, line := range strings.Split(string(out), "\n") { if strings.HasPrefix(line, "Number Of Processors:") { - p := whiteSpaces.Split(line, 4) + p := strings.Fields(line) if len(p) > 3 { if t, err := strconv.ParseUint(p[3], 10, 64); err == nil { ret.Cores = int32(t) } } } else if strings.HasPrefix(line, "Processor Clock Speed:") { - p := whiteSpaces.Split(line, 5) + p := strings.Fields(line) if len(p) > 4 { if t, err := strconv.ParseFloat(p[3], 64); err == nil { switch strings.ToUpper(p[4]) { diff --git a/disk/disk_aix_nocgo.go b/disk/disk_aix_nocgo.go index 4f93c7522..17e2b9c84 100644 --- a/disk/disk_aix_nocgo.go +++ b/disk/disk_aix_nocgo.go @@ -12,7 +12,6 @@ import ( "golang.org/x/sys/unix" ) -var whiteSpaces = regexp.MustCompile(`\s+`) var startBlank = regexp.MustCompile(`^\s+`) var ignoreFSType = map[string]bool{"procfs": true} @@ -60,7 +59,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro if startBlank.MatchString(line) { line = "localhost" + line } - p := whiteSpaces.Split(lines[idx], 6) + p := strings.Fields(lines[idx]) if len(p) < 5 || ignoreFSType[p[colidx["vfs"]]] { continue } diff --git a/mem/mem_aix_nocgo.go b/mem/mem_aix_nocgo.go index fc9e49222..cc6a76d2f 100644 --- a/mem/mem_aix_nocgo.go +++ b/mem/mem_aix_nocgo.go @@ -5,15 +5,12 @@ package mem import ( "context" - "regexp" "strconv" "strings" "github.com/shirou/gopsutil/v3/internal/common" ) -var whiteSpaces = regexp.MustCompile(`\s+`) - func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { vmem, swap, err := callSVMon(ctx) if err != nil { @@ -49,7 +46,7 @@ func callSVMon(ctx context.Context) (*VirtualMemoryStat, *SwapMemoryStat, error) swap := &SwapMemoryStat{} for _, line := range strings.Split(string(out), "\n") { if strings.HasPrefix(line, "memory") { - p := whiteSpaces.Split(line, 7) + p := strings.Fields(line) if len(p) > 2 { if t, err := strconv.ParseUint(p[1], 10, 64); err == nil { vmem.Total = t * pagesize @@ -65,7 +62,7 @@ func callSVMon(ctx context.Context) (*VirtualMemoryStat, *SwapMemoryStat, error) } } } else if strings.HasPrefix(line, "pg space") { - p := whiteSpaces.Split(line, 4) + p := strings.Fields(line) if len(p) > 3 { if t, err := strconv.ParseUint(p[2], 10, 64); err == nil { swap.Total = t * pagesize