Skip to content

Commit e1d29d8

Browse files
committed
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 <[email protected]>
1 parent a6effd3 commit e1d29d8

File tree

1 file changed

+7
-43
lines changed

1 file changed

+7
-43
lines changed

cpuinfo.go

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,8 @@ func (fs FS) CPUInfo() ([]CPUInfo, error) {
7474
func parseCPUInfoX86(info io.Reader) ([]CPUInfo, error) {
7575
scanner := bufio.NewScanner(info)
7676

77-
// find the first "processor" line
78-
firstLine := firstNonEmptyLine(scanner)
79-
if !strings.HasPrefix(firstLine, "processor") || !strings.Contains(firstLine, ":") {
80-
return nil, errors.New("invalid cpuinfo file: " + firstLine)
81-
}
82-
field := strings.SplitN(firstLine, ": ", 2)
83-
v, err := strconv.ParseUint(field[1], 0, 32)
84-
if err != nil {
85-
return nil, err
86-
}
87-
firstcpu := CPUInfo{Processor: uint(v)}
88-
cpuinfo := []CPUInfo{firstcpu}
89-
i := 0
77+
cpuinfo := []CPUInfo{}
78+
i := -1
9079

9180
for scanner.Scan() {
9281
line := scanner.Text()
@@ -239,13 +228,8 @@ func parseCPUInfoARM(info io.Reader) ([]CPUInfo, error) {
239228
func parseCPUInfoS390X(info io.Reader) ([]CPUInfo, error) {
240229
scanner := bufio.NewScanner(info)
241230

242-
firstLine := firstNonEmptyLine(scanner)
243-
if !strings.HasPrefix(firstLine, "vendor_id") || !strings.Contains(firstLine, ":") {
244-
return nil, errors.New("invalid cpuinfo file: " + firstLine)
245-
}
246-
field := strings.SplitN(firstLine, ": ", 2)
247231
cpuinfo := []CPUInfo{}
248-
commonCPUInfo := CPUInfo{VendorID: field[1]}
232+
commonCPUInfo := CPUInfo{}
249233

250234
for scanner.Scan() {
251235
line := scanner.Text()
@@ -254,6 +238,8 @@ func parseCPUInfoS390X(info io.Reader) ([]CPUInfo, error) {
254238
continue
255239
}
256240
switch strings.TrimSpace(field[0]) {
241+
case "vendor_id":
242+
commonCPUInfo.VendorID = field[1]
257243
case "bogomips per cpu":
258244
v, err := strconv.ParseFloat(field[1], 64)
259245
if err != nil {
@@ -307,18 +293,8 @@ func parseCPUInfoS390X(info io.Reader) ([]CPUInfo, error) {
307293
func parseCPUInfoPPC(info io.Reader) ([]CPUInfo, error) {
308294
scanner := bufio.NewScanner(info)
309295

310-
firstLine := firstNonEmptyLine(scanner)
311-
if !strings.HasPrefix(firstLine, "processor") || !strings.Contains(firstLine, ":") {
312-
return nil, errors.New("invalid cpuinfo file: " + firstLine)
313-
}
314-
field := strings.SplitN(firstLine, ": ", 2)
315-
v, err := strconv.ParseUint(field[1], 0, 32)
316-
if err != nil {
317-
return nil, err
318-
}
319-
firstcpu := CPUInfo{Processor: uint(v)}
320-
cpuinfo := []CPUInfo{firstcpu}
321-
i := 0
296+
cpuinfo := []CPUInfo{}
297+
i := -1
322298

323299
for scanner.Scan() {
324300
line := scanner.Text()
@@ -348,15 +324,3 @@ func parseCPUInfoPPC(info io.Reader) ([]CPUInfo, error) {
348324
}
349325
return cpuinfo, scanner.Err()
350326
}
351-
352-
// firstNonEmptyLine advances the scanner to the first non-empty line
353-
// and returns the contents of that line
354-
func firstNonEmptyLine(scanner *bufio.Scanner) string {
355-
for scanner.Scan() {
356-
line := scanner.Text()
357-
if strings.TrimSpace(line) != "" {
358-
return line
359-
}
360-
}
361-
return ""
362-
}

0 commit comments

Comments
 (0)