Skip to content

Commit

Permalink
Avoid some uses of regexps
Browse files Browse the repository at this point in the history
  • Loading branch information
scop committed Dec 21, 2023
1 parent 108235a commit 11bc5b3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 14 deletions.
11 changes: 4 additions & 7 deletions cpu/cpu_aix_nocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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]) {
Expand Down
3 changes: 1 addition & 2 deletions disk/disk_aix_nocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 2 additions & 5 deletions mem/mem_aix_nocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 11bc5b3

Please sign in to comment.