< Summary - Jellyfin

Information
Class: MediaBrowser.Common.Providers.ProviderIdParsers
Assembly: MediaBrowser.Common
File(s): /srv/git/jellyfin/MediaBrowser.Common/Providers/ProviderIdParsers.cs
Line coverage
100%
Covered lines: 35
Uncovered lines: 0
Coverable lines: 35
Total lines: 123
Line coverage: 100%
Branch coverage
100%
Covered branches: 22
Total branches: 22
Branch coverage: 100%
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
TryFindImdbId(...)100%1212100%
TryFindTmdbMovieId(...)100%11100%
TryFindTmdbSeriesId(...)100%11100%
TryFindTvdbId(...)100%11100%
TryFindProviderId(...)100%88100%
IsDigit(...)100%22100%

File(s)

/srv/git/jellyfin/MediaBrowser.Common/Providers/ProviderIdParsers.cs

#LineLine coverage
 1using System;
 2using System.Diagnostics.CodeAnalysis;
 3
 4namespace MediaBrowser.Common.Providers
 5{
 6    /// <summary>
 7    /// Parsers for provider ids.
 8    /// </summary>
 9    public static class ProviderIdParsers
 10    {
 11        private const int ImdbMinNumbers = 7;
 12        private const int ImdbMaxNumbers = 8;
 13        private const string ImdbPrefix = "tt";
 14
 15        /// <summary>
 16        /// Parses an IMDb id from a string.
 17        /// </summary>
 18        /// <param name="text">The text to parse.</param>
 19        /// <param name="imdbId">The parsed IMDb id.</param>
 20        /// <returns>True if parsing was successful, false otherwise.</returns>
 21        public static bool TryFindImdbId(ReadOnlySpan<char> text, out ReadOnlySpan<char> imdbId)
 22        {
 23            // IMDb id is at least 9 chars (tt + 7 numbers)
 3324            while (text.Length >= 2 + ImdbMinNumbers)
 25            {
 2626                var ttPos = text.IndexOf(ImdbPrefix);
 2627                if (ttPos == -1)
 28                {
 329                    imdbId = default;
 330                    return false;
 31                }
 32
 2333                text = text.Slice(ttPos);
 2334                var i = 2;
 2335                var limit = Math.Min(text.Length, ImdbMaxNumbers + 2);
 21536                for (; i < limit; i++)
 37                {
 10938                    var c = text[i];
 10939                    if (!IsDigit(c))
 40                    {
 41                        break;
 42                    }
 43                }
 44
 45                // Skip if more than 8 digits + 2 chars for tt
 2346                if (i <= ImdbMaxNumbers + 2 && i >= ImdbMinNumbers + 2)
 47                {
 1248                    imdbId = text.Slice(0, i);
 1249                    return true;
 50                }
 51
 1152                text = text.Slice(i);
 53            }
 54
 755            imdbId = default;
 756            return false;
 57        }
 58
 59        /// <summary>
 60        /// Parses an TMDb id from a movie url.
 61        /// </summary>
 62        /// <param name="text">The text with the url to parse.</param>
 63        /// <param name="tmdbId">The parsed TMDb id.</param>
 64        /// <returns>True if parsing was successful, false otherwise.</returns>
 65        public static bool TryFindTmdbMovieId(ReadOnlySpan<char> text, out ReadOnlySpan<char> tmdbId)
 966            => TryFindProviderId(text, "themoviedb.org/movie/", out tmdbId);
 67
 68        /// <summary>
 69        /// Parses an TMDb id from a series url.
 70        /// </summary>
 71        /// <param name="text">The text with the url to parse.</param>
 72        /// <param name="tmdbId">The parsed TMDb id.</param>
 73        /// <returns>True if parsing was successful, false otherwise.</returns>
 74        public static bool TryFindTmdbSeriesId(ReadOnlySpan<char> text, out ReadOnlySpan<char> tmdbId)
 675            => TryFindProviderId(text, "themoviedb.org/tv/", out tmdbId);
 76
 77        /// <summary>
 78        /// Parses an TVDb id from a url.
 79        /// </summary>
 80        /// <param name="text">The text with the url to parse.</param>
 81        /// <param name="tvdbId">The parsed TVDb id.</param>
 82        /// <returns>True if parsing was successful, false otherwise.</returns>
 83        public static bool TryFindTvdbId(ReadOnlySpan<char> text, out ReadOnlySpan<char> tvdbId)
 684            => TryFindProviderId(text, "thetvdb.com/?tab=series&id=", out tvdbId);
 85
 86        private static bool TryFindProviderId(ReadOnlySpan<char> text, ReadOnlySpan<char> searchString, [NotNullWhen(tru
 87        {
 2188            var searchPos = text.IndexOf(searchString);
 2189            if (searchPos == -1)
 90            {
 991                providerId = default;
 992                return false;
 93            }
 94
 1295            text = text.Slice(searchPos + searchString.Length);
 96
 1297            int i = 0;
 10698            for (; i < text.Length; i++)
 99            {
 55100                var c = text[i];
 101
 55102                if (!IsDigit(c))
 103                {
 104                    break;
 105                }
 106            }
 107
 12108            if (i >= 1)
 109            {
 9110                providerId = text.Slice(0, i);
 9111                return true;
 112            }
 113
 3114            providerId = default;
 3115            return false;
 116        }
 117
 118        private static bool IsDigit(char c)
 119        {
 164120            return c >= '0' && c <= '9';
 121        }
 122    }
 123}