github.com/getong/docker@v1.13.1/api/types/versions/compare.go (about)

     1  package versions
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  )
     7  
     8  // compare compares two version strings
     9  // returns -1 if v1 < v2, 1 if v1 > v2, 0 otherwise.
    10  func compare(v1, v2 string) int {
    11  	var (
    12  		currTab  = strings.Split(v1, ".")
    13  		otherTab = strings.Split(v2, ".")
    14  	)
    15  
    16  	max := len(currTab)
    17  	if len(otherTab) > max {
    18  		max = len(otherTab)
    19  	}
    20  	for i := 0; i < max; i++ {
    21  		var currInt, otherInt int
    22  
    23  		if len(currTab) > i {
    24  			currInt, _ = strconv.Atoi(currTab[i])
    25  		}
    26  		if len(otherTab) > i {
    27  			otherInt, _ = strconv.Atoi(otherTab[i])
    28  		}
    29  		if currInt > otherInt {
    30  			return 1
    31  		}
    32  		if otherInt > currInt {
    33  			return -1
    34  		}
    35  	}
    36  	return 0
    37  }
    38  
    39  // LessThan checks if a version is less than another
    40  func LessThan(v, other string) bool {
    41  	return compare(v, other) == -1
    42  }
    43  
    44  // LessThanOrEqualTo checks if a version is less than or equal to another
    45  func LessThanOrEqualTo(v, other string) bool {
    46  	return compare(v, other) <= 0
    47  }
    48  
    49  // GreaterThan checks if a version is greater than another
    50  func GreaterThan(v, other string) bool {
    51  	return compare(v, other) == 1
    52  }
    53  
    54  // GreaterThanOrEqualTo checks if a version is greater than or equal to another
    55  func GreaterThanOrEqualTo(v, other string) bool {
    56  	return compare(v, other) >= 0
    57  }
    58  
    59  // Equal checks if a version is equal to another
    60  func Equal(v, other string) bool {
    61  	return compare(v, other) == 0
    62  }