-
Notifications
You must be signed in to change notification settings - Fork 13
/
VersionRange.cs
166 lines (144 loc) · 5.31 KB
/
VersionRange.cs
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
namespace UIShell.OSGi
{
using System;
using System.ComponentModel;
using Utility;
[TypeConverter(typeof(VersionRangeConverter))]
public class VersionRange : IComparable<VersionRange>, IEquatable<VersionRange>, IComparable
{
private const char _includeEqualMaxChar = ']';
private const char _includeEqualMinChar = '[';
private string _versionString;
public bool IsIncludedEqualMaxVersion { get; set; }
public bool IsIncludedEqualMinVersion { get; set; }
public Version MaxVersion { get; set; }
public Version MinVersion { get; set; }
private VersionRange() : this(null)
{
}
public VersionRange(string version)
{
_versionString = version;
if (version != null)
{
version = version.Trim();
}
if (string.IsNullOrEmpty(version))
{
IsIncludedEqualMaxVersion = true;
IsIncludedEqualMinVersion = true;
MinVersion = new Version(0, 0, 0, 0);
MaxVersion = new Version(0x7fffffff, 0, 0, 0);
}
else if (version.IndexOf(',') <= 0)
{
IsIncludedEqualMaxVersion = true;
IsIncludedEqualMinVersion = true;
MinVersion = new Version(version);
MaxVersion = VersionUtility.GetMinNotCompatibleVersion(MinVersion);
IsIncludedEqualMaxVersion = false;
}
else
{
IsIncludedEqualMinVersion = version.IndexOf('[') == 0;
IsIncludedEqualMaxVersion = version.IndexOf(']') == (version.Length - 1);
if (IsIncludedEqualMinVersion)
{
version = version.Substring(1, version.Length - 2);
}
string[] strArray = version.Split(new char[] { ',' });
MinVersion = new Version(strArray[0]);
MaxVersion = new Version(strArray[1]);
}
}
public VersionRange(string minVersion, string maxVersion)
{
MinVersion = new Version(minVersion);
MaxVersion = new Version(maxVersion);
}
public int CompareTo(object obj) =>
CompareTo(obj as VersionRange);
public int CompareTo(VersionRange other)
{
if (Equals(other))
{
return 0;
}
return -1;
}
public override bool Equals(object obj) =>
base.Equals(obj as VersionRange);
public bool Equals(VersionRange other) =>
((((other != null) && (other.IsIncludedEqualMaxVersion == IsIncludedEqualMaxVersion)) && ((other.IsIncludedEqualMinVersion == IsIncludedEqualMinVersion) && (other.MaxVersion == MaxVersion))) && (other.MinVersion == MinVersion));
public override int GetHashCode()
{
int num = 0;
num = 0 | ((MaxVersion.Major & 15) << 0x1c);
num |= (MaxVersion.Minor & 0xff) << 20;
num |= (MaxVersion.Build & 0xff) << 12;
return (num | (MaxVersion.Revision & 0xfff));
}
public bool IsIncluded(Version version)
{
if (version == null)
{
return false;
}
int num = version.CompareTo(MinVersion);
if (IsIncludedEqualMinVersion)
{
if (num < 0)
{
return false;
}
}
else if (num <= 0)
{
return false;
}
num = version.CompareTo(MaxVersion);
if (IsIncludedEqualMaxVersion)
{
if (num > 0)
{
return false;
}
}
else if (num >= 0)
{
return false;
}
return true;
}
public static bool operator ==(VersionRange v1, VersionRange v2)
{
if (ReferenceEquals(v1, null))
{
return ReferenceEquals(v2, null);
}
return v1.Equals(v2);
}
public static bool operator >(VersionRange v1, VersionRange v2) =>
(v2 < v1);
public static bool operator >=(VersionRange v1, VersionRange v2) =>
(v2 <= v1);
public static bool operator !=(VersionRange v1, VersionRange v2) =>
!(v1 == v2);
public static bool operator <(VersionRange v1, VersionRange v2) =>
(v1?.CompareTo(v2) < 0);
public static bool operator <=(VersionRange v1, VersionRange v2) =>
(v1?.CompareTo(v2) <= 0);
public override string ToString()
{
if (!string.IsNullOrEmpty(_versionString))
{
return _versionString;
}
if ((MaxVersion == null) && (MinVersion == null))
{
return "[0.0.0, Maximum Version)";
}
return ("MinVersion:" + MinVersion.ToString() + " MaxVersion:" + MaxVersion.ToString());
}
}
}