-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpypi.go
More file actions
311 lines (283 loc) · 6.32 KB
/
Copy pathpypi.go
File metadata and controls
311 lines (283 loc) · 6.32 KB
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
package vers
import (
"regexp"
"strings"
)
// PEP 440 version parsing and comparison.
// https://peps.python.org/pep-0440/
//
// Version form: [N!]N(.N)*[{a|b|rc}N][.postN][.devN][+local]
// Ordering: .devN < aN < bN < rcN < (release) < .postN
// pep440Regex is derived from the canonical regex in PEP 440 Appendix B,
// simplified to the parts we need for ordering.
var pep440Regex = regexp.MustCompile(`(?i)^\s*v?` +
`(?:(\d+)!)?` + // 1: epoch
`(\d+(?:\.\d+)*)` + // 2: release
`(?:[-_.]?(alpha|beta|preview|pre|rc|a|b|c)[-_.]?(\d*))?` + // 3,4: pre
`(?:(?:[-_.]?(post|rev|r)[-_.]?(\d*))|(?:-(\d+)))?` + // 5,6,7: post (or -N implicit post)
`(?:[-_.]?(dev)[-_.]?(\d*))?` + // 8,9: dev
`(?:\+([a-z0-9]+(?:[-_.][a-z0-9]+)*))?` + // 10: local
`\s*$`)
//nolint:goconst,mnd
var pep440PreTags = map[string]int{
"a": 0, qualifierAlpha: 0,
"b": 1, qualifierBeta: 1,
"c": 2, "rc": 2, "pre": 2, "preview": 2,
}
// Numeric components in PEP 440 are unbounded non-negative integers, so
// they are stored as digit strings and compared with cmpNumStr rather than
// converted to int.
type pep440Version struct {
epoch string
release []string
hasPre bool
preTag int
preNum string
hasPost bool
post string
hasDev bool
dev string
local []pep440LocalPart
}
type pep440LocalPart struct {
s string
isNum bool
}
func parsePEP440(s string) (pep440Version, bool) {
m := pep440Regex.FindStringSubmatch(s)
if m == nil {
return pep440Version{}, false
}
v := pep440Version{epoch: m[1]}
v.release = strings.Split(m[2], ".")
if m[3] != "" {
v.hasPre = true
v.preTag = pep440PreTags[strings.ToLower(m[3])]
v.preNum = m[4]
}
if m[5] != "" || m[7] != "" {
v.hasPost = true
if m[7] != "" {
v.post = m[7]
} else {
v.post = m[6]
}
}
if m[8] != "" {
v.hasDev = true
v.dev = m[9]
}
if m[10] != "" {
v.local = parsePEP440Local(m[10])
}
return v, true
}
// pep440CompatibleUpper returns the exclusive upper bound for a ~= clause.
// ~= X.Y -> < (X+1)
// ~= X.Y.Z -> < X.(Y+1)
// The bound is derived from release segments only and preserves the epoch.
func pep440CompatibleUpper(version string) (string, bool) {
v, ok := parsePEP440(version)
if !ok || len(v.release) < 2 { //nolint:mnd
return "", false
}
head := make([]string, len(v.release)-1)
copy(head, v.release[:len(v.release)-1])
head[len(head)-1] = incNumStr(head[len(head)-1])
upper := strings.Join(head, ".")
if cmpNumStr(v.epoch, "0") != 0 {
upper = trimLeadingZeros(v.epoch) + "!" + upper
}
return upper, true
}
// incNumStr returns the decimal string s+1 for a non-negative integer string.
func incNumStr(s string) string {
s = trimLeadingZeros(s)
b := []byte(s)
for i := len(b) - 1; i >= 0; i-- {
if b[i] < '9' {
b[i]++
return string(b)
}
b[i] = '0'
}
return "1" + string(b)
}
func parsePEP440Local(s string) []pep440LocalPart {
raw := strings.FieldsFunc(strings.ToLower(s), func(r rune) bool {
return r == '.' || r == '-' || r == '_'
})
parts := make([]pep440LocalPart, 0, len(raw))
for _, p := range raw {
parts = append(parts, pep440LocalPart{s: p, isNum: isDigits(p)})
}
return parts
}
func isDigits(s string) bool {
if s == "" {
return false
}
for i := 0; i < len(s); i++ {
if s[i] < '0' || s[i] > '9' {
return false
}
}
return true
}
// comparePyPI compares two PEP 440 version strings.
// Falls back to generic comparison if either side is not valid PEP 440.
func comparePyPI(a, b string) int {
va, okA := parsePEP440(a)
vb, okB := parsePEP440(b)
if !okA || !okB {
return CompareVersions(a, b)
}
if c := cmpNumStr(va.epoch, vb.epoch); c != 0 {
return c
}
if c := cmpNumStrSlice(va.release, vb.release); c != 0 {
return c
}
if c := cmpPEP440Pre(va, vb); c != 0 {
return c
}
if c := cmpPEP440Post(va, vb); c != 0 {
return c
}
if c := cmpPEP440Dev(va, vb); c != 0 {
return c
}
return cmpPEP440Local(va.local, vb.local)
}
// cmpPEP440Pre orders the pre-release slot. A dev-only version (no pre, no
// post) sorts before all pre-releases; a version with no pre otherwise sorts
// after all pre-releases.
func cmpPEP440Pre(a, b pep440Version) int {
ra, rb := pep440PreRank(a), pep440PreRank(b)
if ra != rb {
return cmpInt(ra, rb)
}
if !a.hasPre {
return 0
}
if c := cmpInt(a.preTag, b.preTag); c != 0 {
return c
}
return cmpNumStr(a.preNum, b.preNum)
}
func pep440PreRank(v pep440Version) int {
if !v.hasPre && !v.hasPost && v.hasDev {
return -1
}
if !v.hasPre {
return 1
}
return 0
}
// cmpPEP440Post orders the post-release slot. Absence sorts before presence.
func cmpPEP440Post(a, b pep440Version) int {
if a.hasPost != b.hasPost {
if a.hasPost {
return 1
}
return -1
}
if !a.hasPost {
return 0
}
return cmpNumStr(a.post, b.post)
}
// cmpPEP440Dev orders the dev-release slot. Absence sorts after presence.
func cmpPEP440Dev(a, b pep440Version) int {
if a.hasDev != b.hasDev {
if a.hasDev {
return -1
}
return 1
}
if !a.hasDev {
return 0
}
return cmpNumStr(a.dev, b.dev)
}
// cmpNumStr compares two non-negative integer strings without converting
// to a fixed-width type. Empty is treated as zero. Leading zeros are ignored.
func cmpNumStr(a, b string) int {
a = trimLeadingZeros(a)
b = trimLeadingZeros(b)
if len(a) != len(b) {
return cmpInt(len(a), len(b))
}
return cmpString(a, b)
}
func trimLeadingZeros(s string) string {
if s == "" {
return "0"
}
i := 0
for i < len(s)-1 && s[i] == '0' {
i++
}
return s[i:]
}
func cmpNumStrSlice(a, b []string) int {
n := len(a)
if len(b) > n {
n = len(b)
}
for i := 0; i < n; i++ {
var x, y string
if i < len(a) {
x = a[i]
}
if i < len(b) {
y = b[i]
}
if c := cmpNumStr(x, y); c != 0 {
return c
}
}
return 0
}
func cmpPEP440Local(a, b []pep440LocalPart) int {
// No local sorts before any local.
if len(a) == 0 && len(b) == 0 {
return 0
}
if len(a) == 0 {
return -1
}
if len(b) == 0 {
return 1
}
n := len(a)
if len(b) > n {
n = len(b)
}
for i := 0; i < n; i++ {
if i >= len(a) {
return -1
}
if i >= len(b) {
return 1
}
pa, pb := a[i], b[i]
// Numeric segments sort after strings; within kind, natural order.
if pa.isNum != pb.isNum {
if pa.isNum {
return 1
}
return -1
}
if pa.isNum {
if c := cmpNumStr(pa.s, pb.s); c != 0 {
return c
}
} else {
if c := cmpString(pa.s, pb.s); c != 0 {
return c
}
}
}
return 0
}