diff options
| author | Bond_009 <bond.009@outlook.com> | 2023-01-24 12:47:59 +0100 |
|---|---|---|
| committer | Bond_009 <bond.009@outlook.com> | 2023-01-24 12:47:59 +0100 |
| commit | b7f2c8de5b36795d8c75b96b89af4843803f04ef (patch) | |
| tree | a53485965f52c71585f08312a683cabcee982b7f | |
| parent | 6b006a576dc5b708ec57615ea8b65586a6183ee2 (diff) | |
Simplify AlphanumericComparator
| -rw-r--r-- | src/Jellyfin.Extensions/AlphanumericComparator.cs | 43 | ||||
| -rw-r--r-- | tests/Jellyfin.Extensions.Tests/AlphanumericComparatorTests.cs | 5 |
2 files changed, 9 insertions, 39 deletions
diff --git a/src/Jellyfin.Extensions/AlphanumericComparator.cs b/src/Jellyfin.Extensions/AlphanumericComparator.cs index 1b19752bb..6e451d40e 100644 --- a/src/Jellyfin.Extensions/AlphanumericComparator.cs +++ b/src/Jellyfin.Extensions/AlphanumericComparator.cs @@ -86,47 +86,12 @@ namespace Jellyfin.Extensions { 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); diff --git a/tests/Jellyfin.Extensions.Tests/AlphanumericComparatorTests.cs b/tests/Jellyfin.Extensions.Tests/AlphanumericComparatorTests.cs index 2a7e8fafd..105e2a52a 100644 --- a/tests/Jellyfin.Extensions.Tests/AlphanumericComparatorTests.cs +++ b/tests/Jellyfin.Extensions.Tests/AlphanumericComparatorTests.cs @@ -19,6 +19,11 @@ namespace Jellyfin.Extensions.Tests [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(); |
