parse.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. package cpu
  5. import "strconv"
  6. // parseRelease parses a dot-separated version number. It follows the semver
  7. // syntax, but allows the minor and patch versions to be elided.
  8. //
  9. // This is a copy of the Go runtime's parseRelease from
  10. // https://golang.org/cl/209597.
  11. func parseRelease(rel string) (major, minor, patch int, ok bool) {
  12. // Strip anything after a dash or plus.
  13. for i := 0; i < len(rel); i++ {
  14. if rel[i] == '-' || rel[i] == '+' {
  15. rel = rel[:i]
  16. break
  17. }
  18. }
  19. next := func() (int, bool) {
  20. for i := 0; i < len(rel); i++ {
  21. if rel[i] == '.' {
  22. ver, err := strconv.Atoi(rel[:i])
  23. rel = rel[i+1:]
  24. return ver, err == nil
  25. }
  26. }
  27. ver, err := strconv.Atoi(rel)
  28. rel = ""
  29. return ver, err == nil
  30. }
  31. if major, ok = next(); !ok || rel == "" {
  32. return
  33. }
  34. if minor, ok = next(); !ok || rel == "" {
  35. return
  36. }
  37. patch, ok = next()
  38. return
  39. }