diff --git a/MediaBrowser.Common/Providers/ProviderIdParsers.cs b/MediaBrowser.Common/Providers/ProviderIdParsers.cs index 56e0112dd..bfe61a3f8 100644 --- a/MediaBrowser.Common/Providers/ProviderIdParsers.cs +++ b/MediaBrowser.Common/Providers/ProviderIdParsers.cs @@ -10,6 +10,9 @@ namespace MediaBrowser.Common.Providers /// public static class ProviderIdParsers { + private const int ImdbMinNumbers = 7; + private const int ImdbMaxNumbers = 8; + /// /// Parses an IMDb id from a string. /// @@ -21,7 +24,8 @@ namespace MediaBrowser.Common.Providers var span = text.AsSpan(); var tt = "tt".AsSpan(); - while (true) + // imdb id is at least 9 chars (tt + 7 numbers) + while (span.Length >= 2 + ImdbMinNumbers) { var ttPos = span.IndexOf(tt); if (ttPos == -1) @@ -31,27 +35,28 @@ namespace MediaBrowser.Common.Providers } span = span.Slice(ttPos + tt.Length); - - int i = 0; - // IMDb id has a maximum of 8 digits - int max = span.Length > 8 ? 8 : span.Length; - for (; i < max; i++) + var i = 0; + for (; i < Math.Min(span.Length, ImdbMaxNumbers); i++) { var c = span[i]; - - if (c < '0' || c > '9') + if (!IsDigit(c)) { break; } } - // IMDb id has a minimum of 7 digits - if (i >= 7) + // skip if more than 8 digits + if (i <= ImdbMaxNumbers && i >= ImdbMinNumbers) { imdbId = string.Concat(tt, span.Slice(0, i)); return true; } + + span = span.Slice(i); } + + imdbId = default; + return false; } /// @@ -86,34 +91,39 @@ namespace MediaBrowser.Common.Providers var span = text.AsSpan(); var searchSpan = searchString.AsSpan(); - while (true) + var searchPos = span.IndexOf(searchSpan); + if (searchPos == -1) { - var searchPos = span.IndexOf(searchSpan); - if (searchPos == -1) + providerId = default; + return false; + } + + span = span.Slice(searchPos + searchSpan.Length); + + int i = 0; + for (; i < span.Length; i++) + { + var c = span[i]; + + if (!IsDigit(c)) { - providerId = default; - return false; - } - - span = span.Slice(searchPos + searchSpan.Length); - - int i = 0; - for (; i < span.Length; i++) - { - var c = span[i]; - - if (c < '0' || c > '9') - { - break; - } - } - - if (i >= 1) - { - providerId = span.Slice(0, i).ToString(); - return true; + break; } } + + if (i >= 1) + { + providerId = span.Slice(0, i).ToString(); + return true; + } + + providerId = default; + return false; + } + + private static bool IsDigit(char c) + { + return c >= '0' && c <= '9'; } } } diff --git a/tests/Jellyfin.Common.Tests/Providers/ProviderIdParserTests.cs b/tests/Jellyfin.Common.Tests/Providers/ProviderIdParserTests.cs index a493dda64..cfe1ea86b 100644 --- a/tests/Jellyfin.Common.Tests/Providers/ProviderIdParserTests.cs +++ b/tests/Jellyfin.Common.Tests/Providers/ProviderIdParserTests.cs @@ -17,6 +17,7 @@ namespace Jellyfin.Common.Tests.Providers [InlineData("Jellyfin", false, null)] [InlineData("tt1234567tt7654321", true, "tt1234567")] [InlineData("tt12345678tt7654321", true, "tt12345678")] + [InlineData("tt123456789", true, "tt12345678")] public void Parse_Imdb(string text, bool shouldSucceed, string? imdbId) { var succeeded = ProviderIdParsers.TryParseImdbId(text, out string? parsedId);