【rich function】VersionCompare replace
This commit is contained in:
@@ -5,37 +5,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// returns -1 if the first version is lower than the second, 0 if they are equal, and 1 if the second is lower.
|
||||
func CompareVersions(a, b string) int {
|
||||
as := strings.Split(a, ".")
|
||||
bs := strings.Split(b, ".")
|
||||
var loop int
|
||||
if len(as) > len(bs) {
|
||||
loop = len(as)
|
||||
} else {
|
||||
loop = len(bs)
|
||||
}
|
||||
|
||||
for i := 0; i < loop; i++ {
|
||||
var x, y string
|
||||
if len(as) > i {
|
||||
x = as[i]
|
||||
}
|
||||
if len(bs) > i {
|
||||
y = bs[i]
|
||||
}
|
||||
xi, _ := strconv.Atoi(x)
|
||||
yi, _ := strconv.Atoi(y)
|
||||
if xi > yi {
|
||||
return 1
|
||||
} else if xi < yi {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
//return 0 if they are equal,and 1 if v1>2,and 2 if v1<v2
|
||||
func Compare(v1, v2 string) int {
|
||||
replaceMap := map[string]string{"V": "", "v": "", "-": "."}
|
||||
@@ -95,8 +64,8 @@ func strSlice2IntSlice(strs []string) []int64 {
|
||||
return retInt
|
||||
}
|
||||
|
||||
//custom operator compare
|
||||
func CustomCompareVersions(v1, v2, operator string) bool {
|
||||
//operator compare returns true if the first field and the second field are equal else false
|
||||
func CompareVersions(v1, v2, operator string) bool {
|
||||
com := Compare(v1, v2)
|
||||
switch operator {
|
||||
case "==":
|
||||
|
||||
Reference in New Issue
Block a user