-
Notifications
You must be signed in to change notification settings - Fork 2
/
version.go
418 lines (392 loc) · 10 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//
// Copyright 2018-2023 Cristian Maglie. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
package semver
// Version contains the results of parsed version string
type Version struct {
raw string
bytes []byte
major int
minor int
patch int
prerelease int
build int
}
func (v *Version) String() string {
if v == nil {
return ""
}
return v.raw
}
// NormalizedString is a datatype to be used in maps and other places where the
// version is used as a key.
type NormalizedString string
// NormalizedString return a string representation of the version that is
// normalized to always have a major, minor and patch version. This is useful
// to be used in maps and other places where the version is used as a key.
func (v *Version) NormalizedString() NormalizedString {
if v == nil {
return ""
}
if v.major == 0 {
return NormalizedString("0.0.0")
} else if v.minor == v.major {
return NormalizedString(v.raw[0:v.major] + ".0.0" + v.raw[v.major:])
} else if v.patch == v.minor {
return NormalizedString(v.raw[0:v.minor] + ".0" + v.raw[v.minor:])
} else {
return NormalizedString(v.raw)
}
}
// Normalize transforms a truncated semver version in a strictly compliant semver
// version by adding minor and patch versions. For example:
// "1" is trasformed to "1.0.0" or "2.5-dev" to "2.5.0-dev"
func (v *Version) Normalize() {
if v.major == 0 {
v.raw = "0.0.0" + v.raw
v.major = 1
v.minor = 3
v.patch = 5
v.prerelease += 5
v.build += 5
} else if v.minor == v.major {
v.raw = v.raw[0:v.major] + ".0.0" + v.raw[v.major:]
v.minor = v.major + 2
v.patch = v.major + 4
v.prerelease += 4
v.build += 4
} else if v.patch == v.minor {
v.raw = v.raw[0:v.minor] + ".0" + v.raw[v.minor:]
v.patch = v.minor + 2
v.prerelease += 2
v.build += 2
}
}
func compareNumber(a, b []byte) int {
la := len(a)
lb := len(b)
if la == lb {
for i := range a {
if a[i] == b[i] {
continue
}
if a[i] > b[i] {
return 1
}
return -1
}
return 0
}
if la > lb {
return 1
}
return -1
}
func compareAlpha(a, b []byte) int {
if string(a) > string(b) {
return 1
}
if string(a) < string(b) {
return -1
}
return 0
}
var zero = []byte("0")
// CompareTo compares the Version with the one passed as parameter.
// Returns -1, 0 or 1 if the version is respectively less than, equal
// or greater than the compared Version
func (v *Version) CompareTo(u *Version) int {
// 11. Precedence refers to how versions are compared to each other when ordered.
// Precedence MUST be calculated by separating the version into cmp, minor,
// patch and pre-release identifiers in that order (Build metadata does not
// figure into precedence). Precedence is determined by the first difference when
// comparing each of these identifiers from left to right as follows: Major, minor,
// and patch versions are always compared numerically.
// Example: 1.0.0 < 2.0.0 < 2.1.0 < 2.1.1.
vIdx := 0
uIdx := 0
vMajor := v.major
uMajor := u.major
{
if vMajor == uMajor {
for vIdx < vMajor {
if v.bytes[vIdx] == u.bytes[uIdx] {
vIdx++
uIdx++
continue
}
if v.bytes[vIdx] > u.bytes[uIdx] {
return 1
}
return -1
}
} else if vMajor == 0 && u.bytes[uIdx] == '0' {
return 0
} else if uMajor == 0 && v.bytes[vIdx] == '0' {
return 0
} else if vMajor > uMajor {
return 1
} else {
return -1
}
}
vMinor := v.minor
uMinor := u.minor
vIdx = vMajor + 1
uIdx = uMajor + 1
{
la := vMinor - vMajor - 1
lb := uMinor - uMajor - 1
if la == lb {
for vIdx < vMinor {
if v.bytes[vIdx] == u.bytes[uIdx] {
vIdx++
uIdx++
continue
}
if v.bytes[vIdx] > u.bytes[uIdx] {
return 1
}
return -1
}
} else if vMinor == vMajor && u.bytes[uIdx] == '0' {
return 0
} else if uMinor == uMajor && v.bytes[vIdx] == '0' {
return 0
} else if la > lb {
return 1
} else {
return -1
}
}
vPatch := v.patch
uPatch := u.patch
vIdx = vMinor + 1
uIdx = uMinor + 1
{
la := vPatch - vMinor - 1
lb := uPatch - uMinor - 1
if la == lb {
for vIdx < vPatch {
if v.bytes[vIdx] == u.bytes[uIdx] {
vIdx++
uIdx++
continue
}
if v.bytes[vIdx] > u.bytes[uIdx] {
return 1
}
return -1
}
} else if vPatch == vMinor && u.bytes[uIdx] == '0' {
return 0
} else if uPatch == uMinor && v.bytes[vIdx] == '0' {
return 0
} else if la > lb {
return 1
} else {
return -1
}
}
// if both versions have no pre-release, they are equal
if v.prerelease == vPatch && u.prerelease == uPatch {
return 0
}
// When major, minor, and patch are equal, a pre-release version has lower
// precedence than a normal version.
// Example: 1.0.0-alpha < 1.0.0.
// if v has no pre-release, it's greater than u
if v.prerelease == vPatch {
return 1
}
// if u has no pre-release, it's greater than v
if u.prerelease == uPatch {
return -1
}
// Precedence for two pre-release versions with the same major, minor, and patch
// version MUST be determined by comparing each dot separated identifier from left
// to right until a difference is found as follows:
// - identifiers consisting of only digits are compared numerically
// - identifiers with letters or hyphens are compared lexically in ASCII sort order.
// Numeric identifiers always have lower precedence than non-numeric identifiers.
// A larger set of pre-release fields has a higher precedence than a smaller set,
// if all of the preceding identifiers are equal.
// Example: 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta <
// < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0.
vIdx = vPatch + 1
uIdx = uPatch + 1
vLast := v.prerelease
uLast := u.prerelease
vIsAlpha := false
uIsAlpha := false
vIsLonger := false
uIsLonger := false
cmp := 0
for {
var vCurr byte
var uCurr byte
if vIdx != vLast {
vCurr = v.raw[vIdx]
}
if uIdx != uLast {
uCurr = u.raw[uIdx]
}
if vIdx == vLast || vCurr == '.' {
if uIdx != uLast && uCurr != '.' {
if !uIsAlpha && !(uCurr >= '0' && uCurr <= '9') {
uIsAlpha = true
}
uIsLonger = true
uIdx++
continue
}
} else if uIdx == uLast || uCurr == '.' {
if vIdx != vLast && vCurr != '.' {
if !vIsAlpha && !(vCurr >= '0' && vCurr <= '9') {
vIsAlpha = true
}
vIsLonger = true
vIdx++
continue
}
} else {
if cmp == 0 {
if vCurr > uCurr {
cmp = 1
} else if vCurr < uCurr {
cmp = -1
}
}
if !vIsAlpha && !(vCurr >= '0' && vCurr <= '9') {
vIsAlpha = true
}
if !uIsAlpha && !(uCurr >= '0' && uCurr <= '9') {
uIsAlpha = true
}
vIdx++
uIdx++
continue
}
// Numeric identifiers always have lower precedence than non-numeric identifiers.
if vIsAlpha && uIsAlpha {
if cmp != 0 {
// alphanumeric vs alphanumeric, sorting has priority
return cmp
} else if vIsLonger {
// alphanumeric vs alphanumeric, v is longer, return >
return 1
} else if uIsLonger {
// alphanumeric vs alphanumeric, u is longer, return <
return -1
}
// Both alphanumeric, if comparison is equal, move on the next field
} else if vIsAlpha && !uIsAlpha {
// alphanumeric vs numeric, return >
return 1
} else if !vIsAlpha && uIsAlpha {
// numeric vs alphanumeric, return <
return -1
} else {
if vIsLonger {
// numeric vs numeric, v is longer, return >
return 1
} else if uIsLonger {
// numeric vs numeric, u is longer, return <
return -1
} else if cmp != 0 {
// numeric vs numeric, return cmp if not equal
return cmp
}
// Both numeric, if comparison is equal, move on the next field
}
// A larger set of pre-release fields has a higher precedence than a smaller set,
// if all of the preceding identifiers are equal.
if vIdx == vLast && uIdx == uLast {
// No more field, proceed with build metadata
break
}
if vIdx != vLast && uIdx == uLast {
// v has more fields, return >
return 1
}
if vIdx == vLast && uIdx != uLast {
// u has more fields, return <
return -1
}
// Move on the next field
vIsAlpha = false
uIsAlpha = false
vIsLonger = false
uIsLonger = false
vIdx++
uIdx++
}
return 0
}
// LessThan returns true if the Version is less than the Version passed as parameter
func (v *Version) LessThan(u *Version) bool {
return v.CompareTo(u) < 0
}
// LessThanOrEqual returns true if the Version is less than or equal to the Version passed as parameter
func (v *Version) LessThanOrEqual(u *Version) bool {
return v.CompareTo(u) <= 0
}
// Equal returns true if the Version is equal to the Version passed as parameter
func (v *Version) Equal(u *Version) bool {
return v.CompareTo(u) == 0
}
// GreaterThan returns true if the Version is greater than the Version passed as parameter
func (v *Version) GreaterThan(u *Version) bool {
return v.CompareTo(u) > 0
}
// GreaterThanOrEqual returns true if the Version is greater than or equal to the Version passed as parameter
func (v *Version) GreaterThanOrEqual(u *Version) bool {
return v.CompareTo(u) >= 0
}
// CompatibleWith returns true if the Version is compatible with the version passed as paramater
func (v *Version) CompatibleWith(u *Version) bool {
if !u.GreaterThanOrEqual(v) {
return false
}
vMajor := zero[:]
if v.major > 0 {
vMajor = v.bytes[:v.major]
}
uMajor := zero[:]
if u.major > 0 {
uMajor = u.bytes[:u.major]
}
majorEquals := compareNumber(vMajor, uMajor) == 0
if v.major > 0 && v.bytes[0] != '0' {
return majorEquals
}
if !majorEquals {
return false
}
vMinor := zero[:]
if v.minor > v.major {
vMinor = v.bytes[v.major+1 : v.minor]
}
uMinor := zero[:]
if u.minor > u.major {
uMinor = u.bytes[u.major+1 : u.minor]
}
minorEquals := compareNumber(vMinor, uMinor) == 0
if vMinor[0] != '0' {
return minorEquals
}
if !minorEquals {
return false
}
vPatch := zero[:]
if v.patch > v.minor {
vPatch = v.bytes[v.minor+1 : v.patch]
}
uPatch := zero[:]
if u.patch > u.minor {
uPatch = u.bytes[u.minor+1 : u.patch]
}
return compareNumber(vPatch, uPatch) == 0
}