From cda672883336b19c4c18a8a2574ec2a75d01734d Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 18 May 2020 15:26:03 -0700 Subject: [PATCH] cpuinfo: rm GetFirstNonEmptyLine, simplify scan There is no sense to skip empty lines at the beginning of the file since there are none (there were in test data but it is fixed by an earlier commit). Logic is simpler now. Signed-off-by: Kir Kolyshkin --- cpuinfo.go | 50 +++++++------------------------------------------- 1 file changed, 7 insertions(+), 43 deletions(-) diff --git a/cpuinfo.go b/cpuinfo.go index f48601db2..e49d186de 100644 --- a/cpuinfo.go +++ b/cpuinfo.go @@ -74,19 +74,8 @@ func (fs FS) CPUInfo() ([]CPUInfo, error) { func parseCPUInfoX86(info io.Reader) ([]CPUInfo, error) { scanner := bufio.NewScanner(info) - // find the first "processor" line - firstLine := firstNonEmptyLine(scanner) - if !strings.HasPrefix(firstLine, "processor") || !strings.Contains(firstLine, ":") { - return nil, errors.New("invalid cpuinfo file: " + firstLine) - } - field := strings.SplitN(firstLine, ": ", 2) - v, err := strconv.ParseUint(field[1], 0, 32) - if err != nil { - return nil, err - } - firstcpu := CPUInfo{Processor: uint(v)} - cpuinfo := []CPUInfo{firstcpu} - i := 0 + cpuinfo := []CPUInfo{} + i := -1 for scanner.Scan() { line := scanner.Text() @@ -236,13 +225,8 @@ func parseCPUInfoARM(info io.Reader) ([]CPUInfo, error) { func parseCPUInfoS390X(info io.Reader) ([]CPUInfo, error) { scanner := bufio.NewScanner(info) - firstLine := firstNonEmptyLine(scanner) - if !strings.HasPrefix(firstLine, "vendor_id") || !strings.Contains(firstLine, ":") { - return nil, errors.New("invalid cpuinfo file: " + firstLine) - } - field := strings.SplitN(firstLine, ": ", 2) cpuinfo := []CPUInfo{} - commonCPUInfo := CPUInfo{VendorID: field[1]} + commonCPUInfo := CPUInfo{} for scanner.Scan() { line := scanner.Text() @@ -251,6 +235,8 @@ func parseCPUInfoS390X(info io.Reader) ([]CPUInfo, error) { continue } switch strings.TrimSpace(field[0]) { + case "vendor_id": + commonCPUInfo.VendorID = field[1] case "bogomips per cpu": v, err := strconv.ParseFloat(field[1], 64) if err != nil { @@ -304,18 +290,8 @@ func parseCPUInfoS390X(info io.Reader) ([]CPUInfo, error) { func parseCPUInfoPPC(info io.Reader) ([]CPUInfo, error) { scanner := bufio.NewScanner(info) - firstLine := firstNonEmptyLine(scanner) - if !strings.HasPrefix(firstLine, "processor") || !strings.Contains(firstLine, ":") { - return nil, errors.New("invalid cpuinfo file: " + firstLine) - } - field := strings.SplitN(firstLine, ": ", 2) - v, err := strconv.ParseUint(field[1], 0, 32) - if err != nil { - return nil, err - } - firstcpu := CPUInfo{Processor: uint(v)} - cpuinfo := []CPUInfo{firstcpu} - i := 0 + cpuinfo := []CPUInfo{} + i := -1 for scanner.Scan() { line := scanner.Text() @@ -345,15 +321,3 @@ func parseCPUInfoPPC(info io.Reader) ([]CPUInfo, error) { } return cpuinfo, scanner.Err() } - -// firstNonEmptyLine advances the scanner to the first non-empty line -// and returns the contents of that line -func firstNonEmptyLine(scanner *bufio.Scanner) string { - for scanner.Scan() { - line := scanner.Text() - if strings.TrimSpace(line) != "" { - return line - } - } - return "" -}