| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | |
| | | 4 | | namespace Jellyfin.Extensions |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Alphanumeric <see cref="IComparer{T}" />. |
| | | 8 | | /// </summary> |
| | | 9 | | public class AlphanumericComparator : IComparer<string?> |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="s1">The first object to compare.</param> |
| | | 15 | | /// <param name="s2">The second object to compare.</param> |
| | | 16 | | /// <returns>A signed integer that indicates the relative values of <c>x</c> and <c>y</c>.</returns> |
| | | 17 | | public static int CompareValues(string? s1, string? s2) |
| | | 18 | | { |
| | 155 | 19 | | if (s1 is null && s2 is null) |
| | | 20 | | { |
| | 0 | 21 | | return 0; |
| | | 22 | | } |
| | | 23 | | |
| | 155 | 24 | | if (s1 is null) |
| | | 25 | | { |
| | 6 | 26 | | return -1; |
| | | 27 | | } |
| | | 28 | | |
| | 149 | 29 | | if (s2 is null) |
| | | 30 | | { |
| | 0 | 31 | | return 1; |
| | | 32 | | } |
| | | 33 | | |
| | 149 | 34 | | int len1 = s1.Length; |
| | 149 | 35 | | int len2 = s2.Length; |
| | | 36 | | |
| | | 37 | | // Early return for empty strings |
| | 149 | 38 | | if (len1 == 0 && len2 == 0) |
| | | 39 | | { |
| | 0 | 40 | | return 0; |
| | | 41 | | } |
| | | 42 | | |
| | 149 | 43 | | if (len1 == 0) |
| | | 44 | | { |
| | 5 | 45 | | return -1; |
| | | 46 | | } |
| | | 47 | | |
| | 144 | 48 | | if (len2 == 0) |
| | | 49 | | { |
| | 0 | 50 | | return 1; |
| | | 51 | | } |
| | | 52 | | |
| | 144 | 53 | | int pos1 = 0; |
| | 144 | 54 | | int pos2 = 0; |
| | | 55 | | |
| | | 56 | | do |
| | | 57 | | { |
| | 252 | 58 | | int start1 = pos1; |
| | 252 | 59 | | int start2 = pos2; |
| | | 60 | | |
| | 252 | 61 | | bool isNum1 = char.IsDigit(s1[pos1++]); |
| | 252 | 62 | | bool isNum2 = char.IsDigit(s2[pos2++]); |
| | | 63 | | |
| | 1734 | 64 | | while (pos1 < len1 && char.IsDigit(s1[pos1]) == isNum1) |
| | | 65 | | { |
| | 1482 | 66 | | pos1++; |
| | | 67 | | } |
| | | 68 | | |
| | 1637 | 69 | | while (pos2 < len2 && char.IsDigit(s2[pos2]) == isNum2) |
| | | 70 | | { |
| | 1385 | 71 | | pos2++; |
| | | 72 | | } |
| | | 73 | | |
| | 252 | 74 | | var span1 = s1.AsSpan(start1, pos1 - start1); |
| | 252 | 75 | | var span2 = s2.AsSpan(start2, pos2 - start2); |
| | | 76 | | |
| | 252 | 77 | | if (isNum1 && isNum2) |
| | | 78 | | { |
| | | 79 | | // Trim leading zeros so we can compare the length |
| | | 80 | | // of the strings to find the largest number |
| | 97 | 81 | | span1 = span1.TrimStart('0'); |
| | 97 | 82 | | span2 = span2.TrimStart('0'); |
| | 97 | 83 | | var span1Len = span1.Length; |
| | 97 | 84 | | var span2Len = span2.Length; |
| | 97 | 85 | | if (span1Len < span2Len) |
| | | 86 | | { |
| | 17 | 87 | | return -1; |
| | | 88 | | } |
| | | 89 | | |
| | 80 | 90 | | if (span1Len > span2Len) |
| | | 91 | | { |
| | 9 | 92 | | return 1; |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | |
| | 226 | 96 | | int result = span1.CompareTo(span2, StringComparison.InvariantCulture); |
| | 226 | 97 | | if (result != 0) |
| | | 98 | | { |
| | 106 | 99 | | return result; |
| | | 100 | | } |
| | 120 | 101 | | } while (pos1 < len1 && pos2 < len2); |
| | | 102 | | |
| | 12 | 103 | | return len1 - len2; |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <inheritdoc /> |
| | | 107 | | public int Compare(string? x, string? y) |
| | | 108 | | { |
| | 155 | 109 | | return CompareValues(x, y); |
| | | 110 | | } |
| | | 111 | | } |
| | | 112 | | } |