Apply suggestions from code review

This commit is contained in:
David 2021-03-18 11:25:58 +01:00
parent 14cbd22fbe
commit 840eeff2af
2 changed files with 45 additions and 34 deletions

View File

@ -10,6 +10,9 @@ namespace MediaBrowser.Common.Providers
/// </summary> /// </summary>
public static class ProviderIdParsers public static class ProviderIdParsers
{ {
private const int ImdbMinNumbers = 7;
private const int ImdbMaxNumbers = 8;
/// <summary> /// <summary>
/// Parses an IMDb id from a string. /// Parses an IMDb id from a string.
/// </summary> /// </summary>
@ -21,7 +24,8 @@ namespace MediaBrowser.Common.Providers
var span = text.AsSpan(); var span = text.AsSpan();
var tt = "tt".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); var ttPos = span.IndexOf(tt);
if (ttPos == -1) if (ttPos == -1)
@ -31,27 +35,28 @@ namespace MediaBrowser.Common.Providers
} }
span = span.Slice(ttPos + tt.Length); span = span.Slice(ttPos + tt.Length);
var i = 0;
int i = 0; for (; i < Math.Min(span.Length, ImdbMaxNumbers); i++)
// IMDb id has a maximum of 8 digits
int max = span.Length > 8 ? 8 : span.Length;
for (; i < max; i++)
{ {
var c = span[i]; var c = span[i];
if (!IsDigit(c))
if (c < '0' || c > '9')
{ {
break; break;
} }
} }
// IMDb id has a minimum of 7 digits // skip if more than 8 digits
if (i >= 7) if (i <= ImdbMaxNumbers && i >= ImdbMinNumbers)
{ {
imdbId = string.Concat(tt, span.Slice(0, i)); imdbId = string.Concat(tt, span.Slice(0, i));
return true; return true;
} }
span = span.Slice(i);
} }
imdbId = default;
return false;
} }
/// <summary> /// <summary>
@ -86,34 +91,39 @@ namespace MediaBrowser.Common.Providers
var span = text.AsSpan(); var span = text.AsSpan();
var searchSpan = searchString.AsSpan(); var searchSpan = searchString.AsSpan();
while (true) var searchPos = span.IndexOf(searchSpan);
if (searchPos == -1)
{ {
var searchPos = span.IndexOf(searchSpan); providerId = default;
if (searchPos == -1) 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; break;
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;
} }
} }
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';
} }
} }
} }

View File

@ -17,6 +17,7 @@ namespace Jellyfin.Common.Tests.Providers
[InlineData("Jellyfin", false, null)] [InlineData("Jellyfin", false, null)]
[InlineData("tt1234567tt7654321", true, "tt1234567")] [InlineData("tt1234567tt7654321", true, "tt1234567")]
[InlineData("tt12345678tt7654321", true, "tt12345678")] [InlineData("tt12345678tt7654321", true, "tt12345678")]
[InlineData("tt123456789", true, "tt12345678")]
public void Parse_Imdb(string text, bool shouldSucceed, string? imdbId) public void Parse_Imdb(string text, bool shouldSucceed, string? imdbId)
{ {
var succeeded = ProviderIdParsers.TryParseImdbId(text, out string? parsedId); var succeeded = ProviderIdParsers.TryParseImdbId(text, out string? parsedId);