Skip to content

Commit

Permalink
cpuinfo: rm GetFirstNonEmptyLine, simplify scan
Browse files Browse the repository at this point in the history
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 <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed May 18, 2020
1 parent 78592ae commit 7263f91
Showing 1 changed file with 7 additions and 43 deletions.
50 changes: 7 additions & 43 deletions cpuinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 ""
}

0 comments on commit 7263f91

Please sign in to comment.