Skip to content

Commit

Permalink
Merge pull request jellyfin#9170 from Bond-009/alphanum
Browse files Browse the repository at this point in the history
  • Loading branch information
Bond-009 authored Jan 26, 2023
2 parents 63b0132 + b7f2c8d commit f3e04ac
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 39 deletions.
43 changes: 4 additions & 39 deletions src/Jellyfin.Extensions/AlphanumericComparator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,47 +86,12 @@ public static int CompareValues(string? s1, string? s2)
{
return 1;
}
else if (span1Len >= 20) // Number is probably too big for a ulong
{
// Trim all the first digits that are the same
int i = 0;
while (i < span1Len && span1[i] == span2[i])
{
i++;
}

// If there are no more digits it's the same number
if (i == span1Len)
{
continue;
}

// Only need to compare the most significant digit
span1 = span1.Slice(i, 1);
span2 = span2.Slice(i, 1);
}

if (!ulong.TryParse(span1, out var num1)
|| !ulong.TryParse(span2, out var num2))
{
return 0;
}
else if (num1 < num2)
{
return -1;
}
else if (num1 > num2)
{
return 1;
}
}
else

int result = span1.CompareTo(span2, StringComparison.InvariantCulture);
if (result != 0)
{
int result = span1.CompareTo(span2, StringComparison.InvariantCulture);
if (result != 0)
{
return result;
}
return result;
}
} while (pos1 < len1 && pos2 < len2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public class AlphanumericComparatorTests
[InlineData("12345678912345678912345678913234567891", "12345678912345678912345678913234567892")]
[InlineData("12345678912345678912345678913234567891a", "12345678912345678912345678913234567891a")]
[InlineData("12345678912345678912345678913234567891a", "12345678912345678912345678913234567891b")]
[InlineData("a5", "a11")]
[InlineData("a05a", "a5b")]
[InlineData("a5a", "a05b")]
[InlineData("6xxx", "007asdf")]
[InlineData("00042Q", "42s")]
public void AlphanumericComparatorTest(params string?[] strings)
{
var copy = strings.Reverse().ToArray();
Expand Down

0 comments on commit f3e04ac

Please sign in to comment.