github.com/ZuluSpl0it/Sia@v1.3.7/build/version.go (about)

     1  package build
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  )
     7  
     8  const (
     9  	// MaxEncodedVersionLength is the maximum length of a version string encoded
    10  	// with the encode package. 100 is much larger than any version number we send
    11  	// now, but it allows us to send additional information in the version string
    12  	// later if we choose. For example appending the version string with the HEAD
    13  	// commit hash.
    14  	MaxEncodedVersionLength = 100
    15  
    16  	// Version is the current version of siad.
    17  	Version = "1.3.3"
    18  )
    19  
    20  // IsVersion returns whether str is a valid version number.
    21  func IsVersion(str string) bool {
    22  	for _, n := range strings.Split(str, ".") {
    23  		if _, err := strconv.Atoi(n); err != nil {
    24  			return false
    25  		}
    26  	}
    27  	return true
    28  }
    29  
    30  // min returns the smaller of two integers.
    31  func min(a, b int) int {
    32  	if a < b {
    33  		return a
    34  	}
    35  	return b
    36  }
    37  
    38  // VersionCmp returns an int indicating the difference between a and b. It
    39  // follows the convention of bytes.Compare and big.Cmp:
    40  //
    41  //   -1 if a <  b
    42  //    0 if a == b
    43  //   +1 if a >  b
    44  //
    45  // One important quirk is that "1.1.0" is considered newer than "1.1", despite
    46  // being numerically equal.
    47  func VersionCmp(a, b string) int {
    48  	aNums := strings.Split(a, ".")
    49  	bNums := strings.Split(b, ".")
    50  	for i := 0; i < min(len(aNums), len(bNums)); i++ {
    51  		// assume that both version strings are valid
    52  		aInt, _ := strconv.Atoi(aNums[i])
    53  		bInt, _ := strconv.Atoi(bNums[i])
    54  		if aInt < bInt {
    55  			return -1
    56  		} else if aInt > bInt {
    57  			return 1
    58  		}
    59  	}
    60  	// all shared digits are equal, but lengths may not be equal
    61  	if len(aNums) < len(bNums) {
    62  		return -1
    63  	} else if len(aNums) > len(bNums) {
    64  		return 1
    65  	}
    66  	// strings are identical
    67  	return 0
    68  }