/
version.go
210 lines (174 loc) · 4.42 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package host
// Copied from https://github.com/knqyf263/go-rpm-version which seems quite abandoned
// original released under MIT license by Teppei Fukuda
import (
"fmt"
"math"
"reflect"
"regexp"
"strconv"
"strings"
"unicode"
)
// alphanumPattern is a regular expression to match all sequences of numeric
// characters or alphanumeric characters.
var alphanumPattern = regexp.MustCompile("([a-zA-Z]+)|([0-9]+)|(~)")
// Version represents a package version.
type Version struct {
epoch int
version string
release string
}
// NewVersion returns a parsed version
func NewVersion(ver string) (version Version) {
var err error
// Parse epoch
splitted := strings.SplitN(ver, ":", 2)
if len(splitted) == 1 {
version.epoch = 0
ver = splitted[0]
} else {
// Trim left space
epoch := strings.TrimLeftFunc(splitted[0], unicode.IsSpace)
version.epoch, err = strconv.Atoi(epoch)
if err != nil {
version.epoch = 0
}
ver = splitted[1]
}
// Parse version and release
index := strings.Index(ver, "-")
if index >= 0 {
version.version = ver[:index]
version.release = ver[index+1:]
} else {
version.version = ver
}
return version
}
// Equal returns whether this version is equal with another version.
func (v1 *Version) Equal(v2 Version) bool {
return v1.Compare(v2) == 0
}
// GreaterThan returns whether this version is greater than another version.
func (v1 *Version) GreaterThan(v2 Version) bool {
return v1.Compare(v2) > 0
}
// LessThan returns whether this version is less than another version.
func (v1 Version) LessThan(v2 Version) bool {
return v1.Compare(v2) < 0
}
// Compare returns an integer comparing two version.
// The result will be 0 if v1==v2, -1 if v1 < v2, and +1 if v1 > v2.
func (v1 Version) Compare(v2 Version) int {
// Equal
if reflect.DeepEqual(v1, v2) {
return 0
}
// Compare epochs
if v1.epoch > v2.epoch {
return 1
} else if v1.epoch < v2.epoch {
return -1
}
// Compare version
ret := rpmvercmp(v1.version, v2.version)
if ret != 0 {
return ret
}
// Compare release
return rpmvercmp(v1.release, v2.release)
}
// String returns the full version string
func (v1 Version) String() string {
version := ""
if v1.epoch > 0 {
version += fmt.Sprintf("%d:", v1.epoch)
}
version += v1.version
if v1.release != "" {
version += fmt.Sprintf("-%s", v1.release)
}
return version
}
// rpmcmpver compares two version or release strings.
// Lifted from https://github.com/cavaliercoder/go-rpm/blob/master/version.go
//
// For the original C implementation, see:
// https://github.com/rpm-software-management/rpm/blob/master/lib/rpmvercmp.c#L16
func rpmvercmp(a, b string) int {
// shortcut for equality
if a == b {
return 0
}
// get alpha/numeric segements
segsa := alphanumPattern.FindAllString(a, -1)
segsb := alphanumPattern.FindAllString(b, -1)
segs := int(math.Min(float64(len(segsa)), float64(len(segsb))))
// compare each segment
for i := 0; i < segs; i++ {
a := segsa[i]
b := segsb[i]
// compare tildes
if []rune(a)[0] == '~' || []rune(b)[0] == '~' {
if []rune(a)[0] != '~' {
return 1
}
if []rune(b)[0] != '~' {
return -1
}
}
if unicode.IsNumber([]rune(a)[0]) {
// numbers are always greater than alphas
if !unicode.IsNumber([]rune(b)[0]) {
// a is numeric, b is alpha
return 1
}
// trim leading zeros
a = strings.TrimLeft(a, "0")
b = strings.TrimLeft(b, "0")
// longest string wins without further comparison
if len(a) > len(b) {
return 1
} else if len(b) > len(a) {
return -1
}
} else if unicode.IsNumber([]rune(b)[0]) {
// a is alpha, b is numeric
return -1
}
// string compare
if a < b {
return -1
} else if a > b {
return 1
}
}
// segments were all the same but separators must have been different
if len(segsa) == len(segsb) {
return 0
}
// If there is a tilde in a segment past the min number of segments, find it.
if len(segsa) > segs && []rune(segsa[segs])[0] == '~' {
return -1
} else if len(segsb) > segs && []rune(segsb[segs])[0] == '~' {
return 1
}
// whoever has the most segments wins
if len(segsa) > len(segsb) {
return 1
}
return -1
}
// Version is a getter method that returns the version.
func (v *Version) Version() string {
return v.version
}
// Release is a getter method that returns the release.
func (v *Version) Release() string {
return v.release
}
// Epoch is a getter method that returns the epoch.
func (v *Version) Epoch() int {
return v.epoch
}