Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public async Task<SortedDictionary<string, IList<PythonProjectRelease>>> GetRele
{
try
{
var parsedVersion = new PythonVersion(release.Key);
var parsedVersion = PythonVersion.Create(release.Key);
if (release.Value != null && release.Value.Count > 0 &&
parsedVersion.Valid && parsedVersion.IsReleasedPackage &&
PythonVersionUtilities.VersionValidForSpec(release.Key, spec.DependencySpecifiers))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ public class PythonVersion : IComparable<PythonVersion>

private static readonly Dictionary<string, int> PreReleaseMapping = new Dictionary<string, int> { { "a", 0 }, { "alpha", 0 }, { "b", 1 }, { "beta", 1 }, { "c", 2 }, { "rc", 2 }, { "pre", 2 }, { "preview", 2 } };

private static readonly Dictionary<string, PythonVersion> Cache = new();

private readonly Match match;

public PythonVersion(string version)
private PythonVersion(string version)
{
var toOperate = version;
if (version.EndsWith(".*"))
Expand Down Expand Up @@ -105,6 +107,23 @@ public PythonVersion(string version)

public static bool operator <=(PythonVersion operand1, PythonVersion operand2) => operand1.CompareTo(operand2) <= 0;

public static PythonVersion Create(string version)
{
if (version == null)
{
throw new ArgumentNullException(nameof(version));
}

if (Cache.TryGetValue(version, out var cachedVersion))
{
return cachedVersion;
}

var pythonVersion = new PythonVersion(version);
Cache.Add(version, pythonVersion);
return pythonVersion;
}

public int CompareTo(PythonVersion other)
{
if (other == null || !other.Valid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ public class PythonVersionComparer : IComparer<string>
{
public int Compare(string x, string y)
{
var xVer = new PythonVersion(x);
var yVer = new PythonVersion(y);
var xVer = PythonVersion.Create(x);
var yVer = PythonVersion.Create(y);

return xVer.CompareTo(yVer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ private static bool VersionValidForSpec(string version, string spec)

var op = spec[..i];

var targetVer = new PythonVersion(version);
var specVer = new PythonVersion(spec[i..]);
var targetVer = PythonVersion.Create(version);
var specVer = PythonVersion.Create(spec[i..]);

if (!targetVer.Valid)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class PythonVersionTests
[TestMethod]
public void TestBasicVersionConstruction()
{
var pythonVersion = new PythonVersion("4!3.2.1.1rc2.post99.dev2");
var pythonVersion = PythonVersion.Create("4!3.2.1.1rc2.post99.dev2");

Assert.AreEqual(4, pythonVersion.Epoch);
Assert.AreEqual("3.2.1.1", pythonVersion.Release);
Expand All @@ -27,7 +27,7 @@ public void TestBasicVersionConstruction()
[TestMethod]
public void TestDefaultDevVersionConstruction()
{
var pythonVersion = new PythonVersion("4!3.2.1.1rc2.post90.dev");
var pythonVersion = PythonVersion.Create("4!3.2.1.1rc2.post90.dev");

Assert.AreEqual(4, pythonVersion.Epoch);
Assert.AreEqual("3.2.1.1", pythonVersion.Release);
Expand Down Expand Up @@ -69,7 +69,7 @@ public void TestPythonVersionComplexComparisons()
"2.10.0.dev1",
"v2.10.0.dev2",
"v2.10.0",
}.Select(x => new { Raw = x, Version = new PythonVersion(x) }).ToList();
}.Select(x => new { Raw = x, Version = PythonVersion.Create(x) }).ToList();

for (var i = 1; i < versionItems.Count; i++)
{
Expand Down