| 1 |
// Copyright 2022 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package cpu |
| 6 |
|
| 7 |
import "strconv" |
| 8 |
|
| 9 |
// parseRelease parses a dot-separated version number from the prefix |
| 10 |
// of rel. It returns ok=true only if at least the major and minor |
| 11 |
// components were successfully parsed; the patch component is |
| 12 |
// best-effort. Trailing vendor or build suffixes such as |
| 13 |
// "-generic", "+", "_hi3535", or "-rc1" are ignored. |
| 14 |
// |
| 15 |
// This is a copy of the Go runtime's parseRelease from |
| 16 |
// https://golang.org/cl/209597, updated in https://golang.org/cl/781800. |
| 17 |
func parseRelease(rel string) (major, minor, patch int, ok bool) { |
| 18 |
// next consumes a run of decimal digits from the front of rel, |
| 19 |
// returning the parsed value. If the digits are followed by a |
| 20 |
// '.', it is consumed and more is set so the caller knows to |
| 21 |
// parse another component; otherwise scanning terminates and |
| 22 |
// the rest of rel is discarded. |
| 23 |
next := func() (n int, more, ok bool) { |
| 24 |
i := 0 |
| 25 |
for i < len(rel) && rel[i] >= '0' && rel[i] <= '9' { |
| 26 |
i++ |
| 27 |
} |
| 28 |
if i == 0 { |
| 29 |
return 0, false, false |
| 30 |
} |
| 31 |
n, err := strconv.Atoi(rel[:i]) |
| 32 |
if err != nil { |
| 33 |
return 0, false, false |
| 34 |
} |
| 35 |
if i < len(rel) && rel[i] == '.' { |
| 36 |
rel = rel[i+1:] |
| 37 |
return n, true, true |
| 38 |
} |
| 39 |
rel = "" |
| 40 |
return n, false, true |
| 41 |
} |
| 42 |
|
| 43 |
var more bool |
| 44 |
if major, more, ok = next(); !ok || !more { |
| 45 |
return 0, 0, 0, false |
| 46 |
} |
| 47 |
if minor, more, ok = next(); !ok { |
| 48 |
return 0, 0, 0, false |
| 49 |
} |
| 50 |
if !more { |
| 51 |
return major, minor, 0, true |
| 52 |
} |
| 53 |
patch, _, _ = next() |
| 54 |
return major, minor, patch, true |
| 55 |
} |