github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/build/version.go (about)

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