forked from thenonameguy/gosemver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semver.go
55 lines (50 loc) · 1.07 KB
/
semver.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
// Semantic version comparing library.
package gosemver
import (
"strings"
)
var replaceMap = map[string]string{"v": "", "-": "."}
// Compare 2 version formats for which is greater.
// For more information see http://semver.org
// Returns: 0 if equals, 1 if the first version arg is greater,
// 2 if the second, -1 if problem occured.
func Compare(v1, v2 string) int {
//keywords := {"alpha,beta,rc,p"}
for k, v := range replaceMap {
if strings.Contains(v1, k) {
strings.Replace(v1, k, v, -1)
}
if strings.Contains(v2, k) {
strings.Replace(v2, k, v, -1)
}
}
ver1 := strings.Split(v1, ".")
ver2 := strings.Split(v2, ".")
var shorter int
if len(ver1) > len(ver2) {
shorter = len(ver2)
} else {
shorter = len(ver1)
}
for i := 0; i < shorter; i++ {
if ver1[i] == ver2[i] {
if shorter-1 == i {
if len(ver1) == len(ver2) {
return 0
} else {
// todo check for keywords
if len(ver1) > len(ver2) {
return 1
} else {
return 2
}
}
}
} else if ver1[i] > ver2[i] {
return 1
} else {
return 2
}
}
return -1
}