-
Notifications
You must be signed in to change notification settings - Fork 0
/
range.go
335 lines (305 loc) · 8.44 KB
/
range.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
package semver
import (
"fmt"
"math"
"regexp"
"strconv"
"strings"
)
var rangedVersionRe = regexp.MustCompile(
fmt.Sprintf(
// Major
`\s*[=v]*(?P<major>(\d+|\*|[xX]))`+
// Followed by an optional .Minor.Patch-preRelease+build
`(\.`+
// Minor
`(?P<minor>(\d+|\*|[xX]))`+
// Followed by an optional .Patch-preRelease+build
`(\.`+
// Patch
`(?P<patch>(\d+|\*|[xX]))`+
// -preRelease
`(-?(?P<preRelease>%s))?`+
// build
`(\+(?P<build>%s))?`+
`)?`+
`)?`, idStr, idStr,
),
)
var simpleRangeOperators = []string{`\^`, `~>`, `~`, `=`, `\<=`, `\>=`, `\<`, `\>`, ``}
var simpleRangeExpr = regexp.MustCompile(fmt.Sprintf(
`((?P<rangeOp>%s)\s*(?P<version1>%s))`,
strings.Join(simpleRangeOperators, `|`), rangedVersionRe.String()))
var rangeExpr = regexp.MustCompile(fmt.Sprintf(
`(?P<range>%s|%s)`,
hyphenRangeExpr.String(), simpleRangeExpr.String()))
var hyphenRangeExpr = regexp.MustCompile(fmt.Sprintf(
`((?P<version1>%s)\s+(?P<rangeOp>\-)\s+(?P<version2>%s))`,
rangedVersionRe.String(), rangedVersionRe.String()))
var versionExpr = regexp.MustCompile(fmt.Sprintf(
`(?P<version1>%s)`,
rangedVersionRe.String()))
// GlobVersion defines a version supporting x-range elements
type GlobVersion struct {
*Version
anyMajor bool
anyMinor bool
anyPatch bool
}
// IsFixed returns true if the x-range does not contain any 'x' elements
func (v *GlobVersion) IsFixed() bool {
return !v.anyMajor && !v.anyMinor && !v.anyPatch
}
func newGlobVersion(major int64, minor int64, patch int64) *GlobVersion {
return &GlobVersion{
anyMajor: major < 0,
anyMinor: minor < 0,
anyPatch: patch < 0,
Version: NewVersion(major, minor, patch),
}
}
// TODO: This does not really works with GlobVersion
// Next returns the next version
func (v *GlobVersion) next() *GlobVersion {
ver := v.Version.next()
return newGlobVersion(ver.Major, ver.Minor, ver.Patch)
}
// MustParseGlobVersion parses x-range semver from str
// Pnics if it cannot be parsed
func MustParseGlobVersion(str string) *GlobVersion {
if v, err := ParseGlobVersion(str); err != nil {
panic(err)
} else {
return v
}
}
// ParseGlobVersion parses x-range semver from str
// Returns an error if it cannot be parsed
func ParseGlobVersion(str string) (*GlobVersion, error) {
isGlob := func(str string) bool {
globs := map[string]bool{"*": true, "x": true, "X": true, "": true}
_, ok := globs[str]
return ok
}
mapping, err := parseVersion(str, rangedVersionRe)
if err != nil {
return nil, err
}
var major, minor, patch int64
var anyMajor, anyMinor, anyPatch = false, false, false
if isGlob(mapping["major"]) {
anyMajor = true
major = -1
} else {
major = toInt(mapping["major"])
}
if isGlob(mapping["minor"]) || (mapping["minor"] == "" && major == -1) {
anyMinor = true
minor = -1
} else {
minor = toInt(mapping["minor"])
minor = toInt(mapping["minor"])
}
if isGlob(mapping["patch"]) || (mapping["patch"] == "" && minor == -1) {
anyPatch = true
patch = -1
} else {
patch = toInt(mapping["patch"])
}
return &GlobVersion{
anyMajor: anyMajor,
anyMinor: anyMinor,
anyPatch: anyPatch,
Version: &Version{
Major: major,
Minor: minor,
Patch: patch,
PreRelease: mapping["preRelease"],
Build: mapping["build"],
majorPresent: mapping["major"] != "",
minorPresent: mapping["minor"] != "",
patchPresent: mapping["patch"] != "",
},
}, nil
}
// Range defines a semver range
type Range struct {
MinVersion *GlobVersion
AllowMinEquality bool
MaxVersion *GlobVersion
AllowMaxEquality bool
}
var infinity = int64(math.Inf(1))
// RegExp returns a pair of regexps corresponding to the lower and higher limits
// of the range. If a version string matches both regexps, is contained in the range.
func (r *Range) RegExp() []*regexp.Regexp {
result := make([]*regexp.Regexp, 2)
if v1 := r.MinVersion; v1 != nil {
list := []string{
fmt.Sprintf(`%d\.%d\.%s`, v1.Major, v1.Minor, gt(int(v1.Patch))),
fmt.Sprintf(`%d\.%s\.\d+`, v1.Major, gt(int(v1.Minor))),
fmt.Sprintf(`%s\.\d+\.\d+`, gt(int(v1.Major))),
}
if r.AllowMinEquality {
eqlStrComp := []string{}
for _, c := range []int64{v1.Major, v1.Minor, v1.Patch} {
if c < 0 {
eqlStrComp = append(eqlStrComp, `\d*`)
} else {
eqlStrComp = append(eqlStrComp, strconv.Itoa(int(c)))
}
}
list = append(list, strings.Join(eqlStrComp, `.`))
}
result[0] = regexp.MustCompile(fmt.Sprintf(`(?:(%s))`, strings.Join(list, `|`)))
} else {
result[0] = regexp.MustCompile(`.*`)
}
if v2 := r.MaxVersion; v2 != nil {
list := []string{}
if v2.Patch > 0 {
list = append(list, fmt.Sprintf(`%d\.%d\.%s`, v2.Major, v2.Minor, lt(int(v2.Patch))))
}
if v2.Minor > 0 {
list = append(list, fmt.Sprintf(`%d\.%s\.\d+`, v2.Major, lt(int(v2.Minor))))
}
if v2.Major > 0 {
list = append(list, fmt.Sprintf(`%s\.\d+\.\d+`, lt(int(v2.Major))))
} else if v2.Major < 0 {
// *.\d.\d is in-matcheable
list = append(list, fmt.Sprintf(`x\.x\.x`))
}
if r.AllowMaxEquality {
eqlStrComp := []string{}
for _, c := range []int64{v2.Major, v2.Minor, v2.Patch} {
if c < 0 {
eqlStrComp = append(eqlStrComp, `\d*`)
} else {
eqlStrComp = append(eqlStrComp, strconv.Itoa(int(c)))
}
}
list = append(list, strings.Join(eqlStrComp, `.`))
}
result[1] = regexp.MustCompile(fmt.Sprintf(`(?:(%s))`, strings.Join(list, `|`)))
} else {
result[1] = regexp.MustCompile(`.*`)
}
return result
}
// UpperLimit returns a version describing the upper limit of the range
func (r *Range) UpperLimit() *GlobVersion {
mVersion := r.MaxVersion
if mVersion == nil {
return newGlobVersion(infinity, infinity, infinity)
}
if r.AllowMaxEquality {
return mVersion
}
return newGlobVersion(mVersion.Major, mVersion.Minor-1, -infinity)
}
// LowerLimit returns a version describing the lower limit of the range
func (r *Range) LowerLimit() *GlobVersion {
minVersion := r.MinVersion
if minVersion == nil {
return newGlobVersion(-infinity, -infinity, -infinity)
}
if r.AllowMinEquality {
return minVersion
}
return newGlobVersion(minVersion.Major, minVersion.Minor+1, -infinity)
}
// MustParseRange creates a Range from a semver string
// It panics in case of error
func MustParseRange(str string) *Range {
r, err := ParseRange(str)
if err != nil {
panic(err)
}
return r
}
// ParseRange creates a Range from a semver string
// It will return a non-nil error if it fails
func ParseRange(str string) (*Range, error) {
matched, mapping := namedReEvaluate(rangeExpr, str)
if !matched {
return nil, fmt.Errorf("Malformed range expression %q", str)
}
v := MustParseGlobVersion(mapping["version1"])
operator := mapping["rangeOp"]
op := &Range{}
var maxVersion, minVersion *GlobVersion
minVersion = v
op.AllowMinEquality = true
op.AllowMaxEquality = false
switch operator {
case `-`:
v2 := MustParseGlobVersion(mapping["version2"])
op.AllowMinEquality = true
if v2.patchPresent {
maxVersion = v2
op.AllowMaxEquality = true
} else {
maxVersion = v2.next()
op.AllowMaxEquality = false
}
case `^`:
d := make([]int64, 3)
for i, c := range v.split() {
if c != 0 {
d[i] = int64(c) + 1
break
}
}
maxVersion = newGlobVersion(d[0], d[1], d[2])
op.AllowMaxEquality = false
case `>`:
minVersion = v
op.AllowMinEquality = false
maxVersion = nil
case `>=`:
maxVersion = nil
case `<`:
minVersion = nil
op.AllowMaxEquality = false
maxVersion = v
case `<=`:
minVersion = nil
op.AllowMaxEquality = true
case `=`:
fallthrough
case ``:
minVersion = v
maxVersion = v
op.AllowMaxEquality = true
op.AllowMinEquality = true
case `~>`:
fallthrough
case `~`:
switch {
case v.minorPresent:
maxVersion = newGlobVersion(v.Major, v.Minor+1, 0)
default:
maxVersion = newGlobVersion(v.Major+1, 0, 0)
}
default:
return nil, fmt.Errorf(`Unknown range operator ` + operator)
}
op.MaxVersion = maxVersion
op.MinVersion = minVersion
return op, nil
}
// Contains checks if the provided version v is contained by the Range
func (r *Range) Contains(v *Version) bool {
if (v.Greater(r.MinVersion) || (r.AllowMinEquality && v.Equal(r.MinVersion))) &&
(v.Less(r.MaxVersion) || (r.AllowMaxEquality && v.Equal(r.MaxVersion))) {
return true
}
return false
}
// Matches is equivalent to Contains and is provided to satisfy the Expression interface (a range is a simple expression)
func (r *Range) Matches(v *Version) bool {
return r.Contains(v)
}
func (r *Range) evaluate(v *Version) bool {
return r.Contains(v)
}