Skip to content

Commit 7263f91

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 78592ae commit 7263f91

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()
@@ -236,13 +225,8 @@ func parseCPUInfoARM(info io.Reader) ([]CPUInfo, error) {
236225
func parseCPUInfoS390X(info io.Reader) ([]CPUInfo, error) {
237226
scanner := bufio.NewScanner(info)
238227

239-
firstLine := firstNonEmptyLine(scanner)
240-
if !strings.HasPrefix(firstLine, "vendor_id") || !strings.Contains(firstLine, ":") {
241-
return nil, errors.New("invalid cpuinfo file: " + firstLine)
242-
}
243-
field := strings.SplitN(firstLine, ": ", 2)
244228
cpuinfo := []CPUInfo{}
245-
commonCPUInfo := CPUInfo{VendorID: field[1]}
229+
commonCPUInfo := CPUInfo{}
246230

247231
for scanner.Scan() {
248232
line := scanner.Text()
@@ -251,6 +235,8 @@ func parseCPUInfoS390X(info io.Reader) ([]CPUInfo, error) {
251235
continue
252236
}
253237
switch strings.TrimSpace(field[0]) {
238+
case "vendor_id":
239+
commonCPUInfo.VendorID = field[1]
254240
case "bogomips per cpu":
255241
v, err := strconv.ParseFloat(field[1], 64)
256242
if err != nil {
@@ -304,18 +290,8 @@ func parseCPUInfoS390X(info io.Reader) ([]CPUInfo, error) {
304290
func parseCPUInfoPPC(info io.Reader) ([]CPUInfo, error) {
305291
scanner := bufio.NewScanner(info)
306292

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

320296
for scanner.Scan() {
321297
line := scanner.Text()
@@ -345,15 +321,3 @@ func parseCPUInfoPPC(info io.Reader) ([]CPUInfo, error) {
345321
}
346322
return cpuinfo, scanner.Err()
347323
}
348-
349-
// firstNonEmptyLine advances the scanner to the first non-empty line
350-
// and returns the contents of that line
351-
func firstNonEmptyLine(scanner *bufio.Scanner) string {
352-
for scanner.Scan() {
353-
line := scanner.Text()
354-
if strings.TrimSpace(line) != "" {
355-
return line
356-
}
357-
}
358-
return ""
359-
}

0 commit comments

Comments
 (0)