@@ -10,19 +10,16 @@ public class SemanticVersion : IComparable
10
10
{
11
11
private readonly Version _version ;
12
12
13
- public static readonly char [ ] Delimiters = { '.' , '-' } ;
13
+ public static readonly char [ ] Delimiters = [ '.' , '-' ] ;
14
14
15
- public static readonly Regex SemanticVersionStrictRegex = new Regex (
15
+ public static readonly Regex SemanticVersionStrictRegex = new (
16
16
@"^(?<Version>([0-9]|[1-9][0-9]*)(\.([0-9]|[1-9][0-9]*)){2,3})" +
17
17
@"(?>\-(?<Prerelease>[0-9A-Za-z\-\.]+))?(?<Metadata>\+[0-9A-Za-z-]+)?$" ,
18
18
RegexOptions . CultureInvariant | RegexOptions . ExplicitCapture | RegexOptions . Compiled ) ;
19
19
20
20
public SemanticVersion ( Version version )
21
21
{
22
- if ( version == null )
23
- {
24
- throw new ArgumentNullException ( nameof ( version ) ) ;
25
- }
22
+ ArgumentNullException . ThrowIfNull ( version ) ;
26
23
27
24
_version = NormalizeVersionValue ( version ) ;
28
25
}
@@ -46,8 +43,7 @@ public SemanticVersion(Version version)
46
43
47
44
public bool IsCompatibleWithBySemVer ( SemanticVersion other )
48
45
{
49
- if ( other == null )
50
- throw new ArgumentNullException ( nameof ( other ) ) ;
46
+ ArgumentNullException . ThrowIfNull ( other ) ;
51
47
52
48
//MAJOR version when you make incompatible API changes,
53
49
var retVal = Major == other . Major ;
@@ -61,8 +57,7 @@ public bool IsCompatibleWithBySemVer(SemanticVersion other)
61
57
62
58
public bool IsCompatibleWith ( SemanticVersion other )
63
59
{
64
- if ( other == null )
65
- throw new ArgumentNullException ( nameof ( other ) ) ;
60
+ ArgumentNullException . ThrowIfNull ( other ) ;
66
61
67
62
var comparisonResult = CompareTo ( other ) ;
68
63
return comparisonResult <= 0 ;
@@ -81,7 +76,7 @@ public static SemanticVersion Parse(string value)
81
76
{
82
77
var normalizedVersion = NormalizeVersionValue ( versionValue ) ;
83
78
var result = new SemanticVersion ( normalizedVersion ) ;
84
- if ( ( ( ICollection < Group > ) match . Groups ) . Any ( x => x . Name . EqualsInvariant ( "Prerelease" ) ) )
79
+ if ( ( ( ICollection < Group > ) match . Groups ) . Any ( x => x . Name . EqualsIgnoreCase ( "Prerelease" ) ) )
85
80
{
86
81
result . Prerelease = match . Groups [ "Prerelease" ] . Value ;
87
82
}
@@ -180,22 +175,27 @@ public override int GetHashCode()
180
175
181
176
public int CompareTo ( object obj )
182
177
{
183
- if ( obj == null )
184
- throw new ArgumentNullException ( nameof ( obj ) ) ;
178
+ ArgumentNullException . ThrowIfNull ( obj ) ;
185
179
186
180
var other = ( SemanticVersion ) obj ;
187
181
188
182
var result = Major . CompareTo ( other . Major ) ;
189
183
if ( result != 0 )
184
+ {
190
185
return result ;
186
+ }
191
187
192
188
result = Minor . CompareTo ( other . Minor ) ;
193
189
if ( result != 0 )
190
+ {
194
191
return result ;
192
+ }
195
193
196
194
result = Patch . CompareTo ( other . Patch ) ;
197
195
if ( result != 0 )
196
+ {
198
197
return result ;
198
+ }
199
199
200
200
return CompareComponent ( Prerelease , other . Prerelease , true ) ;
201
201
}
@@ -205,12 +205,19 @@ private static int CompareComponent(string a, string b, bool nonemptyIsLower = f
205
205
var aEmpty = string . IsNullOrEmpty ( a ) ;
206
206
var bEmpty = string . IsNullOrEmpty ( b ) ;
207
207
if ( aEmpty && bEmpty )
208
+ {
208
209
return 0 ;
210
+ }
209
211
210
212
if ( aEmpty )
213
+ {
211
214
return nonemptyIsLower ? 1 : - 1 ;
215
+ }
216
+
212
217
if ( bEmpty )
218
+ {
213
219
return nonemptyIsLower ? - 1 : 1 ;
220
+ }
214
221
215
222
var aComps = a . Split ( Delimiters ) ;
216
223
var bComps = b . Split ( Delimiters ) ;
@@ -227,17 +234,27 @@ private static int CompareComponent(string a, string b, bool nonemptyIsLower = f
227
234
{
228
235
r = aNum . CompareTo ( bNum ) ;
229
236
if ( r != 0 )
237
+ {
230
238
return r ;
239
+ }
231
240
}
232
241
else
233
242
{
234
243
if ( aIsNum )
244
+ {
235
245
return - 1 ;
246
+ }
247
+
236
248
if ( bIsNum )
249
+ {
237
250
return 1 ;
251
+ }
252
+
238
253
r = string . CompareOrdinal ( ac , bc ) ;
239
254
if ( r != 0 )
255
+ {
240
256
return r ;
257
+ }
241
258
}
242
259
}
243
260
0 commit comments