mirror of
https://github.com/genuinetools/reg.git
synced 2024-09-17 16:11:01 -04:00
ab6c553e6b
Signed-off-by: Jess Frazelle <acidburn@microsoft.com>
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package versions // import "github.com/docker/docker/api/types/versions"
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// compare compares two version strings
|
|
// returns -1 if v1 < v2, 1 if v1 > v2, 0 otherwise.
|
|
func compare(v1, v2 string) int {
|
|
var (
|
|
currTab = strings.Split(v1, ".")
|
|
otherTab = strings.Split(v2, ".")
|
|
)
|
|
|
|
max := len(currTab)
|
|
if len(otherTab) > max {
|
|
max = len(otherTab)
|
|
}
|
|
for i := 0; i < max; i++ {
|
|
var currInt, otherInt int
|
|
|
|
if len(currTab) > i {
|
|
currInt, _ = strconv.Atoi(currTab[i])
|
|
}
|
|
if len(otherTab) > i {
|
|
otherInt, _ = strconv.Atoi(otherTab[i])
|
|
}
|
|
if currInt > otherInt {
|
|
return 1
|
|
}
|
|
if otherInt > currInt {
|
|
return -1
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// LessThan checks if a version is less than another
|
|
func LessThan(v, other string) bool {
|
|
return compare(v, other) == -1
|
|
}
|
|
|
|
// LessThanOrEqualTo checks if a version is less than or equal to another
|
|
func LessThanOrEqualTo(v, other string) bool {
|
|
return compare(v, other) <= 0
|
|
}
|
|
|
|
// GreaterThan checks if a version is greater than another
|
|
func GreaterThan(v, other string) bool {
|
|
return compare(v, other) == 1
|
|
}
|
|
|
|
// GreaterThanOrEqualTo checks if a version is greater than or equal to another
|
|
func GreaterThanOrEqualTo(v, other string) bool {
|
|
return compare(v, other) >= 0
|
|
}
|
|
|
|
// Equal checks if a version is equal to another
|
|
func Equal(v, other string) bool {
|
|
return compare(v, other) == 0
|
|
}
|