【rich function】VersionCompare replace

This commit is contained in:
sunwei
2020-08-09 22:07:30 +08:00
committed by Kevin Wan
parent 945d59a980
commit e18ca9aac1
2 changed files with 3 additions and 57 deletions

View File

@@ -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 "==":