@@ -235,12 +235,22 @@ func Make(s string) (Version, error) {
235
235
236
236
// ParseTolerant allows for certain version specifications that do not strictly adhere to semver
237
237
// specs to be parsed by this library. It does so by normalizing versions before passing them to
238
- // Parse(). It currently trims spaces, removes a "v" prefix, adds a 0 patch number to versions
239
- // with only major and minor components specified, and removes leading 0s .
238
+ // Parse(). It currently trims spaces, removes a "v" prefix, adds 0s to missing minor and patch versions,
239
+ // and removes leading 0s. PreRelease/Build meta data is preserved .
240
240
func ParseTolerant (s string ) (Version , error ) {
241
241
s = strings .TrimSpace (s )
242
242
s = strings .TrimPrefix (s , "v" )
243
243
244
+ //Extract PreRelease/Build meta data if any
245
+ index := strings .IndexRune (s , '-' )
246
+ if buildIndex := strings .IndexRune (s , '+' ); buildIndex != - 1 && (buildIndex < index || index == - 1 ) {
247
+ index = buildIndex
248
+ }
249
+ var meta string
250
+ if index != - 1 {
251
+ meta = s [index :]
252
+ s = s [:index ]
253
+ }
244
254
// Split into major.minor.(patch+pr+meta)
245
255
parts := strings .SplitN (s , "." , 3 )
246
256
// Remove leading zeros.
@@ -254,15 +264,11 @@ func ParseTolerant(s string) (Version, error) {
254
264
}
255
265
}
256
266
// Fill up shortened versions.
257
- if len (parts ) < 3 {
258
- if strings .ContainsAny (parts [len (parts )- 1 ], "+-" ) {
259
- return Version {}, errors .New ("Short version cannot contain PreRelease/Build meta data" )
260
- }
261
- for len (parts ) < 3 {
262
- parts = append (parts , "0" )
263
- }
267
+ for len (parts ) < 3 {
268
+ parts = append (parts , "0" )
264
269
}
265
- s = strings .Join (parts , "." )
270
+ // Reconstruct the normalized version string
271
+ s = strings .Join (parts , "." ) + meta
266
272
267
273
return Parse (s )
268
274
}
0 commit comments