save progress
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
// StringVersionComparer.cs
|
||||
// Sprint: SPRINT_20251230_001_BE_backport_resolver (BP-101)
|
||||
// Task: Create fallback string version comparer
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace StellaOps.VersionComparison.Comparers;
|
||||
|
||||
/// <summary>
|
||||
/// Fallback version comparer that uses ordinal string comparison.
|
||||
/// Used when the package ecosystem is unknown or no specific comparator exists.
|
||||
/// </summary>
|
||||
public sealed class StringVersionComparer : IVersionComparator, IComparer<string>
|
||||
{
|
||||
/// <summary>
|
||||
/// Singleton instance.
|
||||
/// </summary>
|
||||
public static StringVersionComparer Instance { get; } = new();
|
||||
|
||||
private StringVersionComparer() { }
|
||||
|
||||
/// <inheritdoc />
|
||||
public ComparatorType ComparatorType => ComparatorType.SemVer;
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Compare(string? x, string? y)
|
||||
{
|
||||
if (ReferenceEquals(x, y)) return 0;
|
||||
if (x is null) return -1;
|
||||
if (y is null) return 1;
|
||||
|
||||
return string.Compare(x, y, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public VersionComparisonResult CompareWithProof(string? left, string? right)
|
||||
{
|
||||
var proofLines = new List<string>();
|
||||
|
||||
if (left is null && right is null)
|
||||
{
|
||||
proofLines.Add("Both versions are null: equal");
|
||||
return new VersionComparisonResult(0, [.. proofLines], ComparatorType.SemVer);
|
||||
}
|
||||
|
||||
if (left is null)
|
||||
{
|
||||
proofLines.Add("Left version is null: less than right");
|
||||
return new VersionComparisonResult(-1, [.. proofLines], ComparatorType.SemVer);
|
||||
}
|
||||
|
||||
if (right is null)
|
||||
{
|
||||
proofLines.Add("Right version is null: left is greater");
|
||||
return new VersionComparisonResult(1, [.. proofLines], ComparatorType.SemVer);
|
||||
}
|
||||
|
||||
var cmp = string.Compare(left, right, StringComparison.Ordinal);
|
||||
var resultStr = cmp < 0 ? "left is older" : cmp > 0 ? "left is newer" : "equal";
|
||||
var symbol = cmp < 0 ? "<" : cmp > 0 ? ">" : "==";
|
||||
|
||||
proofLines.Add($"String comparison (fallback): '{left}' {symbol} '{right}' ({resultStr})");
|
||||
|
||||
return new VersionComparisonResult(cmp, [.. proofLines], ComparatorType.SemVer);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user