-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersionRange.cs
More file actions
120 lines (111 loc) · 3.82 KB
/
VersionRange.cs
File metadata and controls
120 lines (111 loc) · 3.82 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Spectrum;
public struct VersionRange
{
public Version MinVersion { get; set; }
public Version MaxVersion { get; set; }
public bool IncludeMinVersion { get; set; }
public bool IncludeMaxVersion { get; set; }
public VersionRange(Version minVersion, Version maxVersion, bool includeMinVersion = true, bool includeMaxVersion = false)
{
MinVersion = minVersion;
MaxVersion = maxVersion;
IncludeMinVersion = includeMinVersion;
IncludeMaxVersion = includeMaxVersion;
}
public readonly bool Contains(Version version)
{
return IncludeMinVersion && version == MinVersion || (IncludeMaxVersion && version == MaxVersion || version > MinVersion && version < MaxVersion);
}
public static VersionRange Parse(string input)
{
Version minVersion = Version.Min;
Version maxVersion = Version.Max;
bool includeMinVersion = true;
bool includeMaxVersion = true;
if (!(input.StartsWith('[') || input.StartsWith('(')) && !(input.EndsWith(']') || input.EndsWith(')')))
{
if (input.StartsWith(">="))
{
minVersion = Version.Parse(input[2..]);
}
else if (input.StartsWith('>'))
{
minVersion = Version.Parse(input[1..]);
includeMinVersion = false;
}
else if (input.StartsWith("<="))
{
maxVersion = Version.Parse(input[2..]);
}
else if (input.StartsWith('<'))
{
maxVersion = Version.Parse(input[1..]);
includeMaxVersion = false;
}
else
{
minVersion = Version.Parse(input);
maxVersion = minVersion;
includeMinVersion = true;
includeMaxVersion = true;
}
}
else
{
if (input.StartsWith('['))
includeMinVersion = true;
else if (input.StartsWith('('))
includeMinVersion = false;
if (input.EndsWith(']'))
includeMaxVersion = true;
else if (input.EndsWith(')'))
includeMaxVersion = false;
var versions = input[1..^1].Split(',');
if (versions.Length != 2)
{
throw new FormatException("Invalid version range format.");
}
minVersion = Version.Parse(versions[0]);
maxVersion = Version.Parse(versions[1]);
}
return new VersionRange(minVersion, maxVersion, includeMinVersion, includeMaxVersion);
}
public static bool TryParse(string input, out VersionRange range)
{
try
{
range = Parse(input);
return true;
}
catch
{
range = default;
return false;
}
}
public override readonly bool Equals(object? obj)
{
return obj is VersionRange range && MinVersion == range.MinVersion && MaxVersion == range.MaxVersion && IncludeMinVersion == range.IncludeMinVersion && IncludeMaxVersion == range.IncludeMaxVersion;
}
public override readonly int GetHashCode()
{
return HashCode.Combine(MinVersion, MaxVersion, IncludeMinVersion, IncludeMaxVersion);
}
public override readonly string ToString()
{
return $"{(IncludeMinVersion ? "[" : "(")}{MinVersion}, {MaxVersion}{(IncludeMaxVersion ? "]" : ")")}";
}
public static bool operator ==(VersionRange left, VersionRange right)
{
return left.Equals(right);
}
public static bool operator !=(VersionRange left, VersionRange right)
{
return !(left == right);
}
}