golang.org/x/sys@v0.20.1-0.20240517151509-673e0f94c16d/cpu/parse.go (about)

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