forked from masahiro331/go-mvn-version
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
301 lines (259 loc) · 5.48 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package version
import (
"fmt"
"strconv"
"strings"
)
var (
Qualifiers = []string{"alpha", "beta", "milestone", "rc", "snapshot", "", "sp"}
Aliases = map[string]string{"ga": "", "final": "", "release": "", "cr": "rc"}
ReleaseVersionIndex = fmt.Sprint(indexOf("", Qualifiers))
)
type Version struct {
Value string
Items ListItem
}
func NewVersion(v string) (Version, error) {
return Version{
Value: v,
Items: parseVersion(v),
}, nil
}
func (v1 Version) String() string {
return v1.Value
}
func (v1 Version) Compare(v2 Version) int {
return v1.Items.Compare(v2.Items)
}
func (v1 Version) Equal(v2 Version) bool {
return v1.Compare(v2) == 0
}
func (v1 Version) GreaterThan(v2 Version) bool {
return v1.Compare(v2) > 0
}
func (v1 Version) LessThan(v2 Version) bool {
return v1.Compare(v2) < 0
}
func (v1 Version) GreaterThanOrEqual(v2 Version) bool {
return v1.Compare(v2) >= 0
}
func (v1 Version) LessThanOrEqual(v2 Version) bool {
return v1.Compare(v2) <= 0
}
type Item interface {
Compare(v2 Item) int
isNull() bool
}
func parseItem(isDigit bool, item string) Item {
if isDigit {
i, _ := strconv.Atoi(item)
return IntItem(i)
}
return newStringItem(item, false)
}
type IntItem int
func (item1 IntItem) Compare(item2 Item) int {
if item2 == nil {
if item1 == 0 {
return 0
}
return 1 // 1.0 == 1, 1.1 > 1
}
switch t := item2.(type) {
case IntItem:
return compareInt(int(item1), int(t))
case StringItem:
return 1 // 1.1 > 1-sp
case ListItem:
return 1 // 1.1 > 1-1
}
return 0
}
func (item1 IntItem) isNull() bool {
return item1 == 0
}
type StringItem string
func newStringItem(value string, followedByDigit bool) StringItem {
if followedByDigit {
switch value {
case "a":
return "alpha"
case "b":
return "beta"
case "m":
return "milestone"
}
}
v, ok := Aliases[value]
if ok {
return StringItem(v)
}
return StringItem(value)
}
func (item1 StringItem) Compare(item2 Item) int {
if item2 == nil {
// 1-rc < 1, 1-ga > 1
return strings.Compare(item1.comparableQualifier(), ReleaseVersionIndex)
}
switch v := item2.(type) {
case IntItem:
return -1
case StringItem:
return strings.Compare(item1.comparableQualifier(), v.comparableQualifier())
case ListItem:
return -1 // 1.any < 1-1
}
return 0
}
func (item1 StringItem) isNull() bool {
return item1 == ""
}
func (item1 StringItem) comparableQualifier() string {
index := indexOf(string(item1), Qualifiers)
if index == -1 {
return fmt.Sprintf("%d-%s", len(Qualifiers), item1)
}
return fmt.Sprint(index)
}
func indexOf(s string, sa []string) int {
for i, q := range sa {
if q == s {
return i
}
}
return -1
}
type ListItem []Item
func (items1 ListItem) Compare(item2 Item) int {
if item2 == nil {
if len(items1) == 0 {
return 0 // 1-0 = 1- (normalize) = 1
}
// Compare the entire list of items with null - not just the first one, MNG-6964
for _, item := range items1 {
if result := item.Compare(nil); result != 0 {
return result
}
}
return 0
}
switch v := item2.(type) {
case IntItem:
return -1 // 1-1 < 1.0.x
case StringItem:
return 1 // 1-1 > 1-sp
case ListItem:
iter := zip(items1, v)
for tuple := iter(); tuple != nil; tuple = iter() {
l, r := tuple[0], tuple[1]
var result int
if l == nil {
// if this is shorter, then invert the compare and mul with -1
result = -1 * r.Compare(l)
} else {
result = l.Compare(r)
}
if result != 0 {
return result
}
}
return 0
}
return 0
}
func (items1 ListItem) isNull() bool {
return len(items1) == 0
}
func (items1 ListItem) normalize() ListItem {
ret := items1
for i := len(items1) - 1; i >= 0; i-- {
lastItem := items1[i]
if lastItem.isNull() {
ret = ret[:i]
} else if _, ok := lastItem.(ListItem); !ok {
break
}
}
return ret
}
func zip(a, b ListItem) func() []Item {
i := 0
return func() []Item {
var item1, item2 Item
if i < len(a) {
item1 = a[i]
}
if i < len(b) {
item2 = b[i]
}
if item1 == nil && item2 == nil {
return nil
}
i++
return []Item{item1, item2}
}
}
// parseVersion is normalize version string.
func parseVersion(v string) ListItem {
stack := new(ListItemStack)
var list ListItem
isDigit := false
startIndex := 0
str := strings.ToLower(v)
sa := strings.Split(str, "")
for i, c := range sa {
if c == "." {
if i == startIndex {
list = append(list, IntItem(0))
} else {
list = append(list, parseItem(isDigit, str[startIndex:i]))
}
startIndex = i + 1
} else if c == "-" {
if i == startIndex {
list = append(list, IntItem(0))
} else {
list = append(list, parseItem(isDigit, str[startIndex:i]))
}
startIndex = i + 1
stack.Push(list)
list = ListItem{}
} else if _, err := strconv.Atoi(c); err == nil {
if !isDigit && i > startIndex {
list = append(list, newStringItem(str[startIndex:i], true))
startIndex = i
stack.Push(list)
list = ListItem{}
}
isDigit = true
} else {
if isDigit && i > startIndex {
list = append(list, parseItem(true, str[startIndex:i]))
startIndex = i
stack.Push(list)
list = ListItem{}
}
isDigit = false
}
}
if len(v) > startIndex {
list = append(list, parseItem(isDigit, str[startIndex:]))
stack.Push(list)
}
if stack.IsEmpty() {
stack.Push(list)
}
ret := stack.Pop().normalize()
for !stack.IsEmpty() {
ret = append(stack.Pop().normalize(), ret)
}
return ret
}
func compareInt(a, b int) int {
if a == b {
return 0
} else if a > b {
return 1
}
return -1
}