-
Notifications
You must be signed in to change notification settings - Fork 1
/
comparators.go
63 lines (56 loc) · 1.13 KB
/
comparators.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
package semver
// Compare accepts a Version and compares itself against it, returning >0 for greater than, 0 for equals and <0 for less than.
func (v *Version) Compare(other *Version) int {
if v.major != other.major {
if v.major > other.major {
return 1
}
return -1
}
if v.minor != other.minor {
if v.minor > other.minor {
return 1
}
return -1
}
if v.patch != other.patch {
if v.patch > other.patch {
return 1
}
return -1
}
if v.prerelease == nil {
if other.prerelease == nil {
return 0
}
return 1
}
if other.prerelease == nil {
return -1
}
return v.prerelease.compare(other.prerelease)
}
func (p *prereleases) compare(other *prereleases) int {
for i := 0; i < len(p.values) && i < len(other.values); i++ {
if p.values[i] != other.values[i] {
if val1 := p.numbers[i]; val1 > 0 {
if val2 := other.numbers[i]; val2 > 0 {
if val1 > val2 {
return 1
}
return -1
}
}
if p.values[i] > other.values[i] {
return 1
}
return -1
}
}
if len(p.values) == len(other.values) {
return 0
} else if len(p.values) < len(other.values) {
return -1
}
return 1
}