-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.go
313 lines (293 loc) · 7.33 KB
/
parser.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
//
// 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
import (
"fmt"
)
// MustParse parse a version string and panic if the parsing fails
func MustParse(inVersion string) *Version {
res, err := Parse(inVersion)
if err != nil {
panic(err)
}
return res
}
// Parse parse a version string
func Parse(inVersion string) (*Version, error) {
result := &Version{
raw: inVersion,
bytes: []byte(inVersion),
}
if err := parse(result); err != nil {
return nil, err
}
return result, nil
}
func parse(result *Version) error {
// Setup parsing harness
in := result.bytes
inLen := len(in)
currIdx := -1
var curr byte
next := func() bool {
currIdx = currIdx + 1
if currIdx == inLen {
return false
}
curr = in[currIdx]
return true
}
// 2. A normal version number MUST take the form X.Y.Z where X, Y, and Z
// are non-negative integers, and MUST NOT contain leading zeroes. X is
// the major version, Y is the minor version, and Z is the patch version.
// Each element MUST increase numerically.
// For instance: 1.9.0 -> 1.10.0 -> 1.11.0.
// Parse major
if !next() {
return nil // empty version
}
if !numeric[curr] {
return fmt.Errorf("no major version found")
}
if curr == '0' {
result.major = 1
if !next() {
result.minor = 1
result.patch = 1
result.prerelease = 1
result.build = 1
return nil
}
if numeric[curr] {
return fmt.Errorf("major version must not be prefixed with zero")
}
if !versionSeparator[curr] {
return fmt.Errorf("invalid major version separator '%c'", curr)
}
// Fallthrough and parse next element
} else {
for {
if !next() {
result.major = currIdx
result.minor = currIdx
result.patch = currIdx
result.prerelease = currIdx
result.build = currIdx
return nil
}
if numeric[curr] {
continue
}
if versionSeparator[curr] {
result.major = currIdx
result.minor = currIdx
result.patch = currIdx
result.prerelease = currIdx
result.build = currIdx
break
}
return fmt.Errorf("invalid major version separator '%c'", curr)
}
}
// Parse minor
if curr == '.' {
if !next() || !numeric[curr] {
return fmt.Errorf("no minor version found")
}
if curr == '0' {
result.minor = currIdx + 1
if !next() {
result.patch = currIdx
result.prerelease = currIdx
result.build = currIdx
return nil
}
if numeric[curr] {
return fmt.Errorf("minor version must not be prefixed with zero")
}
if !versionSeparator[curr] {
return fmt.Errorf("invalid minor version separator '%c'", curr)
}
// Fallthrough and parse next element
} else {
for {
if !next() {
result.minor = currIdx
result.patch = currIdx
result.prerelease = currIdx
result.build = currIdx
return nil
}
if numeric[curr] {
continue
}
if versionSeparator[curr] {
result.minor = currIdx
result.patch = currIdx
result.prerelease = currIdx
result.build = currIdx
break
}
return fmt.Errorf("invalid minor version separator '%c'", curr)
}
}
} else {
result.minor = currIdx
}
// Parse patch
if curr == '.' {
if !next() || !numeric[curr] {
return fmt.Errorf("no patch version found")
}
if curr == '0' {
result.patch = currIdx + 1
if !next() {
result.prerelease = currIdx
result.build = currIdx
return nil
}
if numeric[curr] {
return fmt.Errorf("patch version must not be prefixed with zero")
}
if !versionSeparator[curr] {
return fmt.Errorf("invalid patch version separator '%c'", curr)
}
// Fallthrough and parse next element
} else {
for {
if !next() {
result.patch = currIdx
result.prerelease = currIdx
result.build = currIdx
return nil
}
if numeric[curr] {
continue
}
if curr == '-' || curr == '+' {
result.patch = currIdx
result.prerelease = currIdx
result.build = currIdx
break
}
return fmt.Errorf("invalid patch version separator '%c'", curr)
}
}
} else {
result.patch = currIdx
}
// 9. A pre-release version MAY be denoted by appending a hyphen and a series
// of dot separated identifiers immediately following the patch version.
// Identifiers MUST comprise only ASCII alphanumerics and hyphen [0-9A-Za-z-].
// Identifiers MUST NOT be empty.
// Numeric identifiers MUST NOT include leading zeroes.
// Pre-release versions have a lower precedence than the associated normal
// version. A pre-release version indicates that the version is unstable and
// might not satisfy the intended compatibility requirements as denoted by
// its associated normal version.
// Examples: 1.0.0-alpha, 1.0.0-alpha.1, 1.0.0-0.3.7, 1.0.0-x.7.z.92.
if curr == '-' {
// Pre-release parsing
prereleaseIdx := currIdx + 1
zeroPrefix := false
alphaIdentifier := false
for {
if hasNext := next(); !hasNext || curr == '.' || curr == '+' {
if prereleaseIdx == currIdx {
return fmt.Errorf("empty prerelease not allowed")
}
if zeroPrefix && !alphaIdentifier && currIdx-prereleaseIdx > 1 {
return fmt.Errorf("numeric prerelease must not be prefixed with zero")
}
result.prerelease = currIdx
if !hasNext {
result.build = currIdx
return nil
}
if curr == '+' {
break
}
// Multiple prerelease
prereleaseIdx = currIdx + 1
zeroPrefix = false
alphaIdentifier = false
continue
}
if prereleaseIdx == currIdx && curr == '0' {
zeroPrefix = true
continue
}
if numeric[curr] {
continue
}
if identifier[curr] {
alphaIdentifier = true
continue
}
return fmt.Errorf("invalid prerelease separator: '%c'", curr)
}
} else {
result.prerelease = currIdx
}
// 10. Build metadata MAY be denoted by appending a plus sign and a series of
// dot separated identifiers immediately following the patch or pre-release
// version.
// Identifiers MUST comprise only ASCII alphanumerics and hyphen [0-9A-Za-z-].
// Identifiers MUST NOT be empty.
// Build metadata SHOULD be ignored when determining version precedence. Thus
// two versions that differ only in the build metadata, have the same precedence.
// Examples: 1.0.0-alpha+001, 1.0.0+20130313144700, 1.0.0-beta+exp.sha.5114f85.
// Builds parsing
buildIdx := currIdx + 1
if curr == '+' {
for {
if hasNext := next(); !hasNext || curr == '.' {
if buildIdx == currIdx {
return fmt.Errorf("empty build tag not allowed")
}
result.build = currIdx
if !hasNext {
return nil
}
// Multiple builds
buildIdx = currIdx + 1
continue
}
if identifier[curr] {
continue
}
return fmt.Errorf("invalid separator for builds: '%c'", curr)
}
}
return fmt.Errorf("invalid separator: '%c'", curr)
}
func (v *Version) majorString() string {
return v.raw[:v.major]
}
func (v *Version) minorString() string {
if v.minor > v.major {
return v.raw[v.major+1 : v.minor]
}
return ""
}
func (v *Version) patchString() string {
if v.patch > v.minor {
return v.raw[v.minor+1 : v.patch]
}
return ""
}
func (v *Version) prereleaseString() string {
if v.prerelease > v.patch {
return v.raw[v.patch+1 : v.prerelease]
}
return ""
}
func (v *Version) buildString() string {
if v.build > v.prerelease {
return v.raw[v.prerelease+1 : v.build]
}
return ""
}