< Summary - Jellyfin

Information
Class: Jellyfin.Extensions.AlphanumericComparator
Assembly: Jellyfin.Extensions
File(s): /srv/git/jellyfin/src/Jellyfin.Extensions/AlphanumericComparator.cs
Line coverage
90%
Covered lines: 37
Uncovered lines: 4
Coverable lines: 41
Total lines: 112
Line coverage: 90.2%
Branch coverage
88%
Covered branches: 32
Total branches: 36
Branch coverage: 88.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CompareValues(...)88.88%37.33690%
Compare(...)100%11100%

File(s)

/srv/git/jellyfin/src/Jellyfin.Extensions/AlphanumericComparator.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3
 4namespace 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        {
 15519            if (s1 is null && s2 is null)
 20            {
 021                return 0;
 22            }
 23
 15524            if (s1 is null)
 25            {
 626                return -1;
 27            }
 28
 14929            if (s2 is null)
 30            {
 031                return 1;
 32            }
 33
 14934            int len1 = s1.Length;
 14935            int len2 = s2.Length;
 36
 37            // Early return for empty strings
 14938            if (len1 == 0 && len2 == 0)
 39            {
 040                return 0;
 41            }
 42
 14943            if (len1 == 0)
 44            {
 545                return -1;
 46            }
 47
 14448            if (len2 == 0)
 49            {
 050                return 1;
 51            }
 52
 14453            int pos1 = 0;
 14454            int pos2 = 0;
 55
 56            do
 57            {
 25258                int start1 = pos1;
 25259                int start2 = pos2;
 60
 25261                bool isNum1 = char.IsDigit(s1[pos1++]);
 25262                bool isNum2 = char.IsDigit(s2[pos2++]);
 63
 173464                while (pos1 < len1 && char.IsDigit(s1[pos1]) == isNum1)
 65                {
 148266                    pos1++;
 67                }
 68
 163769                while (pos2 < len2 && char.IsDigit(s2[pos2]) == isNum2)
 70                {
 138571                    pos2++;
 72                }
 73
 25274                var span1 = s1.AsSpan(start1, pos1 - start1);
 25275                var span2 = s2.AsSpan(start2, pos2 - start2);
 76
 25277                if (isNum1 && isNum2)
 78                {
 79                    // Trim leading zeros so we can compare the length
 80                    // of the strings to find the largest number
 9781                    span1 = span1.TrimStart('0');
 9782                    span2 = span2.TrimStart('0');
 9783                    var span1Len = span1.Length;
 9784                    var span2Len = span2.Length;
 9785                    if (span1Len < span2Len)
 86                    {
 1787                        return -1;
 88                    }
 89
 8090                    if (span1Len > span2Len)
 91                    {
 992                        return 1;
 93                    }
 94                }
 95
 22696                int result = span1.CompareTo(span2, StringComparison.InvariantCulture);
 22697                if (result != 0)
 98                {
 10699                    return result;
 100                }
 120101            } while (pos1 < len1 && pos2 < len2);
 102
 12103            return len1 - len2;
 104        }
 105
 106        /// <inheritdoc />
 107        public int Compare(string? x, string? y)
 108        {
 155109            return CompareValues(x, y);
 110        }
 111    }
 112}