-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
version.go
77 lines (66 loc) · 1.41 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package utils
import (
"strconv"
"strings"
"github.com/zeromicro/go-zero/core/mathx"
"github.com/zeromicro/go-zero/core/stringx"
)
var replacer = stringx.NewReplacer(map[string]string{
"V": "",
"v": "",
"-": ".",
})
// CompareVersions returns true if the first field and the third field are equal, otherwise false.
func CompareVersions(v1, op, v2 string) bool {
result := compare(v1, v2)
switch op {
case "=", "==":
return result == 0
case "<":
return result == -1
case ">":
return result == 1
case "<=":
return result == -1 || result == 0
case ">=":
return result == 0 || result == 1
}
return false
}
// return -1 if v1<v2, 0 if they are equal, and 1 if v1>v2
func compare(v1, v2 string) int {
v1 = replacer.Replace(v1)
v2 = replacer.Replace(v2)
fields1 := strings.Split(v1, ".")
fields2 := strings.Split(v2, ".")
ver1 := strsToInts(fields1)
ver2 := strsToInts(fields2)
shorter := mathx.MinInt(len(ver1), len(ver2))
for i := 0; i < shorter; i++ {
if ver1[i] == ver2[i] {
continue
} else if ver1[i] < ver2[i] {
return -1
} else {
return 1
}
}
if len(ver1) < len(ver2) {
return -1
} else if len(ver1) == len(ver2) {
return 0
} else {
return 1
}
}
func strsToInts(strs []string) []int64 {
if len(strs) == 0 {
return nil
}
ret := make([]int64, 0, len(strs))
for _, str := range strs {
i, _ := strconv.ParseInt(str, 10, 64)
ret = append(ret, i)
}
return ret
}