Avoid some uses of regexps

pull/1570/head
Ville Skyttä 1 year ago
parent 108235a098
commit 11bc5b3970

@ -5,15 +5,12 @@ package cpu
import ( import (
"context" "context"
"regexp"
"strconv" "strconv"
"strings" "strings"
"github.com/shirou/gopsutil/v3/internal/common" "github.com/shirou/gopsutil/v3/internal/common"
) )
var whiteSpaces = regexp.MustCompile(`\s+`)
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
if percpu { if percpu {
return []TimesStat{}, common.ErrNotImplementedError return []TimesStat{}, common.ErrNotImplementedError
@ -28,8 +25,8 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
} }
ret := TimesStat{CPU: "cpu-total"} ret := TimesStat{CPU: "cpu-total"}
h := whiteSpaces.Split(lines[len(lines)-3], -1) // headers h := strings.Fields(lines[len(lines)-3]) // headers
v := whiteSpaces.Split(lines[len(lines)-2], -1) // values v := strings.Fields(lines[len(lines)-2]) // values
for i, header := range h { for i, header := range h {
if t, err := strconv.ParseFloat(v[i], 64); err == nil { if t, err := strconv.ParseFloat(v[i], 64); err == nil {
switch header { switch header {
@ -58,14 +55,14 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
ret := InfoStat{} ret := InfoStat{}
for _, line := range strings.Split(string(out), "\n") { for _, line := range strings.Split(string(out), "\n") {
if strings.HasPrefix(line, "Number Of Processors:") { if strings.HasPrefix(line, "Number Of Processors:") {
p := whiteSpaces.Split(line, 4) p := strings.Fields(line)
if len(p) > 3 { if len(p) > 3 {
if t, err := strconv.ParseUint(p[3], 10, 64); err == nil { if t, err := strconv.ParseUint(p[3], 10, 64); err == nil {
ret.Cores = int32(t) ret.Cores = int32(t)
} }
} }
} else if strings.HasPrefix(line, "Processor Clock Speed:") { } else if strings.HasPrefix(line, "Processor Clock Speed:") {
p := whiteSpaces.Split(line, 5) p := strings.Fields(line)
if len(p) > 4 { if len(p) > 4 {
if t, err := strconv.ParseFloat(p[3], 64); err == nil { if t, err := strconv.ParseFloat(p[3], 64); err == nil {
switch strings.ToUpper(p[4]) { switch strings.ToUpper(p[4]) {

@ -12,7 +12,6 @@ import (
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
var whiteSpaces = regexp.MustCompile(`\s+`)
var startBlank = regexp.MustCompile(`^\s+`) var startBlank = regexp.MustCompile(`^\s+`)
var ignoreFSType = map[string]bool{"procfs": true} var ignoreFSType = map[string]bool{"procfs": true}
@ -60,7 +59,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
if startBlank.MatchString(line) { if startBlank.MatchString(line) {
line = "localhost" + line line = "localhost" + line
} }
p := whiteSpaces.Split(lines[idx], 6) p := strings.Fields(lines[idx])
if len(p) < 5 || ignoreFSType[p[colidx["vfs"]]] { if len(p) < 5 || ignoreFSType[p[colidx["vfs"]]] {
continue continue
} }

@ -5,15 +5,12 @@ package mem
import ( import (
"context" "context"
"regexp"
"strconv" "strconv"
"strings" "strings"
"github.com/shirou/gopsutil/v3/internal/common" "github.com/shirou/gopsutil/v3/internal/common"
) )
var whiteSpaces = regexp.MustCompile(`\s+`)
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
vmem, swap, err := callSVMon(ctx) vmem, swap, err := callSVMon(ctx)
if err != nil { if err != nil {
@ -49,7 +46,7 @@ func callSVMon(ctx context.Context) (*VirtualMemoryStat, *SwapMemoryStat, error)
swap := &SwapMemoryStat{} swap := &SwapMemoryStat{}
for _, line := range strings.Split(string(out), "\n") { for _, line := range strings.Split(string(out), "\n") {
if strings.HasPrefix(line, "memory") { if strings.HasPrefix(line, "memory") {
p := whiteSpaces.Split(line, 7) p := strings.Fields(line)
if len(p) > 2 { if len(p) > 2 {
if t, err := strconv.ParseUint(p[1], 10, 64); err == nil { if t, err := strconv.ParseUint(p[1], 10, 64); err == nil {
vmem.Total = t * pagesize vmem.Total = t * pagesize
@ -65,7 +62,7 @@ func callSVMon(ctx context.Context) (*VirtualMemoryStat, *SwapMemoryStat, error)
} }
} }
} else if strings.HasPrefix(line, "pg space") { } else if strings.HasPrefix(line, "pg space") {
p := whiteSpaces.Split(line, 4) p := strings.Fields(line)
if len(p) > 3 { if len(p) > 3 {
if t, err := strconv.ParseUint(p[2], 10, 64); err == nil { if t, err := strconv.ParseUint(p[2], 10, 64); err == nil {
swap.Total = t * pagesize swap.Total = t * pagesize

Loading…
Cancel
Save