Repository navigation
Expand file tree
/
Copy pathparse.go
More file actions
250 lines (213 loc) · 5.83 KB
/
Copy pathparse.go
File metadata and controls
250 lines (213 loc) · 5.83 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
// SPDX-FileCopyrightText: 2018 The Go Authors
// SPDX-FileCopyrightText: 2025-2026 WoozyMasta
// SPDX-License-Identifier: MIT AND BSD-3-Clause
//
// Contains code adapted from golang.org/x/mod/semver.
// See NOTICE and LICENSES/Go-BSD-3-Clause.txt.
package semver
import "strconv"
// Parse parses a version string into Semver.
// It accepts an optional leading 'v'/'V' and the shorthand forms "MAJOR" and
// "MAJOR.MINOR" (which normalize to ".0.0" and ".0").
// Prerelease/build are only allowed when MAJOR.MINOR.PATCH are all present.
// Numeric components must fit into uint64; otherwise the input is rejected as invalid.
func Parse(s string) (Semver, bool) {
if s == "" {
return Semver{Original: s, Valid: false}, false
}
orig := s
flags := Flags(0)
// Compute raw view (skip optional leading 'v'/'V') via offset, no extra field in struct.
vOffset := 0
if orig[0] == 'v' || orig[0] == 'V' {
flags |= FlagHasV
if len(orig) == 1 {
return Semver{Original: orig, Valid: false}, false
}
vOffset = 1
}
raw := orig[vOffset:]
i := 0
// major (required)
maj, n, ok := parseUint64(raw, i)
if !ok {
return Semver{Original: orig, Valid: false}, false
}
flags |= FlagHasMajor
i = n
minor, patch := uint64(0), uint64(0)
// minor (optional shorthand)
if i < len(raw) && raw[i] == '.' {
i++
mm, n2, ok := parseUint64(raw, i)
if !ok {
return Semver{Original: orig, Valid: false}, false
}
minor = mm
i = n2
flags |= FlagHasMinor
// patch (optional shorthand)
if i < len(raw) && raw[i] == '.' {
i++
pp, n3, ok := parseUint64(raw, i)
if !ok {
return Semver{Original: orig, Valid: false}, false
}
patch = pp
i = n3
flags |= FlagHasPatch
}
}
if i < len(raw) && (raw[i] == '-' || raw[i] == '+') && flags&FlagHasPatch == 0 {
return Semver{Original: orig, Valid: false}, false
}
// prerelease (optional, after '-')
var pre, build string
if i < len(raw) && raw[i] == '-' {
ps, pe, next, ok := parsePrerelease(raw, i+1)
if !ok {
return Semver{Original: orig, Valid: false}, false
}
pre = orig[vOffset+ps : vOffset+pe] // zero-copy slice of Original
i = next
flags |= FlagHasPre
}
// build (optional, after '+')
if i < len(raw) && raw[i] == '+' {
bs, be, next, ok := parseBuild(raw, i+1)
if !ok {
return Semver{Original: orig, Valid: false}, false
}
build = orig[vOffset+bs : vOffset+be] // zero-copy slice of Original
i = next
flags |= FlagHasBuild
}
// nothing must remain
if i != len(raw) {
return Semver{Original: orig, Valid: false}, false
}
v := Semver{
Original: orig,
Major: maj,
Minor: minor,
Patch: patch,
Prerelease: pre,
Build: build,
Flags: flags,
Valid: true,
}
return v, true
}
// ParseStrict parses an exact SemVer 2.0.0 version.
// Unlike Parse, it rejects a leading 'v' or 'V' and requires MINOR and PATCH.
func ParseStrict(s string) (Semver, bool) {
v, ok := Parse(s)
if !ok || !v.IsStrict() {
return Semver{Original: s, Valid: false}, false
}
return v, true
}
// New constructs a complete Semver from its public DTO components.
// It validates prerelease and build metadata, sets all component flags,
// and stores the normalized version without a prefix in Original.
func New(major, minor, patch uint64, prerelease, build string) (Semver, bool) {
raw := strconv.FormatUint(major, 10) + "." + strconv.FormatUint(minor, 10) + "." + strconv.FormatUint(patch, 10)
if prerelease != "" {
raw += "-" + prerelease
}
if build != "" {
raw += "+" + build
}
return Parse(raw)
}
// parseUint64 parses a non-negative uint64 at raw[i:], following SemVer rules
// (no leading zeros for multi-digit components).
// Returns value, next index, ok.
func parseUint64(raw string, i int) (val uint64, next int, ok bool) {
// no digits
if i >= len(raw) || raw[i] < '0' || raw[i] > '9' {
return 0, i, false
}
// scan digits
j := i + 1
for j < len(raw) && raw[j] >= '0' && raw[j] <= '9' {
j++
}
// reject leading zeros in multi-digit numbers
if raw[i] == '0' && j-i > 1 {
return 0, i, false
}
// Accumulate with uint64 overflow check.
n := uint64(0)
for k := i; k < j; k++ {
d := uint64(raw[k] - '0')
if n > (^uint64(0)-d)/10 {
return 0, i, false // overflow
}
n = n*10 + d
}
return n, j, true
}
// parsePrerelease validates prerelease and returns bounds within raw.
// 'start' is index right after '-'. Returns (preStart, preEnd, nextIndex, ok).
func parsePrerelease(raw string, start int) (int, int, int, bool) {
i := start
partStart := start
for i < len(raw) && raw[i] != '+' {
c := raw[i]
if !isIdentChar(c) && c != '.' {
return 0, 0, 0, false
}
if c == '.' {
if partStart == i || isBadNum(raw[partStart:i]) {
return 0, 0, 0, false
}
partStart = i + 1
}
i++
}
if partStart == i || isBadNum(raw[partStart:i]) {
return 0, 0, 0, false
}
return start, i, i, true
}
// parseBuild validates build metadata and returns bounds within raw.
// 'start' is index after '+'. Returns (buildStart, buildEnd, nextIndex, ok).
func parseBuild(raw string, start int) (int, int, int, bool) {
i := start
partStart := start
for i < len(raw) {
c := raw[i]
if !isIdentChar(c) && c != '.' {
return 0, 0, 0, false
}
if c == '.' {
if partStart == i {
return 0, 0, 0, false
}
partStart = i + 1
}
i++
}
if partStart == i {
return 0, 0, 0, false
}
return start, i, i, true
}
// isIdentChar reports whether c is a valid identifier character
// in prerelease/build metadata ([0-9A-Za-z-]).
func isIdentChar(c byte) bool {
return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' || c == '-'
}
// isBadNum reports whether v is a numeric identifier
// with leading zeroes (which are invalid).
func isBadNum(v string) bool {
if v == "" {
return false
}
i := 0
for i < len(v) && '0' <= v[i] && v[i] <= '9' {
i++
}
return i == len(v) && i > 1 && v[0] == '0'
}