1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/pkg/version/version.go
lalyos 58ef92f89e Fix equal short-long version number comparison
Signed-off-by: Lajos Papp <lajos.papp@sequenceiq.com>
2014-08-11 23:40:35 +02:00

57 lines
1 KiB
Go

package version
import (
"strconv"
"strings"
)
type Version string
func (me Version) compareTo(other Version) int {
var (
meTab = strings.Split(string(me), ".")
otherTab = strings.Split(string(other), ".")
)
max := len(meTab)
if len(otherTab) > max {
max = len(otherTab)
}
for i := 0; i < max; i++ {
var meInt, otherInt int
if len(meTab) > i {
meInt, _ = strconv.Atoi(meTab[i])
}
if len(otherTab) > i {
otherInt, _ = strconv.Atoi(otherTab[i])
}
if meInt > otherInt {
return 1
}
if otherInt > meInt {
return -1
}
}
return 0
}
func (me Version) LessThan(other Version) bool {
return me.compareTo(other) == -1
}
func (me Version) LessThanOrEqualTo(other Version) bool {
return me.compareTo(other) <= 0
}
func (me Version) GreaterThan(other Version) bool {
return me.compareTo(other) == 1
}
func (me Version) GreaterThanOrEqualTo(other Version) bool {
return me.compareTo(other) >= 0
}
func (me Version) Equal(other Version) bool {
return me.compareTo(other) == 0
}