Rename methods and optimize allocations
This commit is contained in:
parent
7685569480
commit
78f7fdeacc
|
@ -44,8 +44,8 @@ namespace Emby.Server.Implementations.Library
|
||||||
// for imdbid we also accept pattern matching
|
// for imdbid we also accept pattern matching
|
||||||
if (string.Equals(attribute, "imdbid", StringComparison.OrdinalIgnoreCase))
|
if (string.Equals(attribute, "imdbid", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
var match = ProviderIdParsers.TryParseImdbId(str, out var imdbId);
|
var match = ProviderIdParsers.TryFindImdbId(str, out var imdbId);
|
||||||
return match ? imdbId : null;
|
return match ? imdbId.ToString() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -12,6 +12,7 @@ namespace MediaBrowser.Common.Providers
|
||||||
{
|
{
|
||||||
private const int ImdbMinNumbers = 7;
|
private const int ImdbMinNumbers = 7;
|
||||||
private const int ImdbMaxNumbers = 8;
|
private const int ImdbMaxNumbers = 8;
|
||||||
|
private const string ImdbPrefix = "tt";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Parses an IMDb id from a string.
|
/// Parses an IMDb id from a string.
|
||||||
|
@ -19,9 +20,9 @@ namespace MediaBrowser.Common.Providers
|
||||||
/// <param name="text">The text to parse.</param>
|
/// <param name="text">The text to parse.</param>
|
||||||
/// <param name="imdbId">The parsed IMDb id.</param>
|
/// <param name="imdbId">The parsed IMDb id.</param>
|
||||||
/// <returns>True if parsing was successful, false otherwise.</returns>
|
/// <returns>True if parsing was successful, false otherwise.</returns>
|
||||||
public static bool TryParseImdbId(ReadOnlySpan<char> text, [NotNullWhen(true)] out string? imdbId)
|
public static bool TryFindImdbId(ReadOnlySpan<char> text, [NotNullWhen(true)] out ReadOnlySpan<char> imdbId)
|
||||||
{
|
{
|
||||||
var tt = "tt".AsSpan();
|
var tt = ImdbPrefix.AsSpan();
|
||||||
|
|
||||||
// imdb id is at least 9 chars (tt + 7 numbers)
|
// imdb id is at least 9 chars (tt + 7 numbers)
|
||||||
while (text.Length >= 2 + ImdbMinNumbers)
|
while (text.Length >= 2 + ImdbMinNumbers)
|
||||||
|
@ -33,9 +34,10 @@ namespace MediaBrowser.Common.Providers
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
text = text.Slice(ttPos + tt.Length);
|
text = text.Slice(ttPos);
|
||||||
var i = 0;
|
var i = 2;
|
||||||
for (; i < Math.Min(text.Length, ImdbMaxNumbers); i++)
|
var limit = Math.Min(text.Length, ImdbMaxNumbers + 2);
|
||||||
|
for (; i < limit; i++)
|
||||||
{
|
{
|
||||||
var c = text[i];
|
var c = text[i];
|
||||||
if (!IsDigit(c))
|
if (!IsDigit(c))
|
||||||
|
@ -44,10 +46,10 @@ namespace MediaBrowser.Common.Providers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// skip if more than 8 digits
|
// skip if more than 8 digits + 2 chars for tt
|
||||||
if (i <= ImdbMaxNumbers && i >= ImdbMinNumbers)
|
if (i <= ImdbMaxNumbers + 2 && i >= ImdbMinNumbers + 2)
|
||||||
{
|
{
|
||||||
imdbId = string.Concat(tt, text.Slice(0, i));
|
imdbId = text.Slice(0, i);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,8 +66,8 @@ namespace MediaBrowser.Common.Providers
|
||||||
/// <param name="text">The text with the url to parse.</param>
|
/// <param name="text">The text with the url to parse.</param>
|
||||||
/// <param name="tmdbId">The parsed TMDb id.</param>
|
/// <param name="tmdbId">The parsed TMDb id.</param>
|
||||||
/// <returns>True if parsing was successful, false otherwise.</returns>
|
/// <returns>True if parsing was successful, false otherwise.</returns>
|
||||||
public static bool TryParseTmdbMovieId(ReadOnlySpan<char> text, [NotNullWhen(true)] out string? tmdbId)
|
public static bool TryFindTmdbMovieId(ReadOnlySpan<char> text, [NotNullWhen(true)] out ReadOnlySpan<char> tmdbId)
|
||||||
=> TryParseProviderId(text, "themoviedb.org/movie/", out tmdbId);
|
=> TryFindProviderId(text, "themoviedb.org/movie/", out tmdbId);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Parses an TMDb id from a series url.
|
/// Parses an TMDb id from a series url.
|
||||||
|
@ -73,8 +75,8 @@ namespace MediaBrowser.Common.Providers
|
||||||
/// <param name="text">The text with the url to parse.</param>
|
/// <param name="text">The text with the url to parse.</param>
|
||||||
/// <param name="tmdbId">The parsed TMDb id.</param>
|
/// <param name="tmdbId">The parsed TMDb id.</param>
|
||||||
/// <returns>True if parsing was successful, false otherwise.</returns>
|
/// <returns>True if parsing was successful, false otherwise.</returns>
|
||||||
public static bool TryParseTmdbSeriesId(ReadOnlySpan<char> text, [NotNullWhen(true)] out string? tmdbId)
|
public static bool TryFindTmdbSeriesId(ReadOnlySpan<char> text, [NotNullWhen(true)] out ReadOnlySpan<char> tmdbId)
|
||||||
=> TryParseProviderId(text, "themoviedb.org/tv/", out tmdbId);
|
=> TryFindProviderId(text, "themoviedb.org/tv/", out tmdbId);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Parses an TVDb id from a url.
|
/// Parses an TVDb id from a url.
|
||||||
|
@ -82,10 +84,10 @@ namespace MediaBrowser.Common.Providers
|
||||||
/// <param name="text">The text with the url to parse.</param>
|
/// <param name="text">The text with the url to parse.</param>
|
||||||
/// <param name="tvdbId">The parsed TVDb id.</param>
|
/// <param name="tvdbId">The parsed TVDb id.</param>
|
||||||
/// <returns>True if parsing was successful, false otherwise.</returns>
|
/// <returns>True if parsing was successful, false otherwise.</returns>
|
||||||
public static bool TryParseTvdbId(ReadOnlySpan<char> text, [NotNullWhen(true)] out string? tvdbId)
|
public static bool TryFindTvdbId(ReadOnlySpan<char> text, [NotNullWhen(true)] out ReadOnlySpan<char> tvdbId)
|
||||||
=> TryParseProviderId(text, "thetvdb.com/?tab=series&id=", out tvdbId);
|
=> TryFindProviderId(text, "thetvdb.com/?tab=series&id=", out tvdbId);
|
||||||
|
|
||||||
private static bool TryParseProviderId(ReadOnlySpan<char> text, ReadOnlySpan<char> searchString, [NotNullWhen(true)] out string? providerId)
|
private static bool TryFindProviderId(ReadOnlySpan<char> text, ReadOnlySpan<char> searchString, [NotNullWhen(true)] out ReadOnlySpan<char> providerId)
|
||||||
{
|
{
|
||||||
var searchPos = text.IndexOf(searchString);
|
var searchPos = text.IndexOf(searchString);
|
||||||
if (searchPos == -1)
|
if (searchPos == -1)
|
||||||
|
@ -109,7 +111,7 @@ namespace MediaBrowser.Common.Providers
|
||||||
|
|
||||||
if (i >= 1)
|
if (i >= 1)
|
||||||
{
|
{
|
||||||
providerId = text.Slice(0, i).ToString();
|
providerId = text.Slice(0, i);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -219,29 +219,29 @@ namespace MediaBrowser.XbmcMetadata.Parsers
|
||||||
|
|
||||||
protected void ParseProviderLinks(T item, string xml)
|
protected void ParseProviderLinks(T item, string xml)
|
||||||
{
|
{
|
||||||
if (ProviderIdParsers.TryParseImdbId(xml, out var imdbId))
|
if (ProviderIdParsers.TryFindImdbId(xml, out var imdbId))
|
||||||
{
|
{
|
||||||
item.SetProviderId(MetadataProvider.Imdb, imdbId);
|
item.SetProviderId(MetadataProvider.Imdb, imdbId.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item is Movie)
|
if (item is Movie)
|
||||||
{
|
{
|
||||||
if (ProviderIdParsers.TryParseTmdbMovieId(xml, out var tmdbId))
|
if (ProviderIdParsers.TryFindTmdbMovieId(xml, out var tmdbId))
|
||||||
{
|
{
|
||||||
item.SetProviderId(MetadataProvider.Tmdb, tmdbId);
|
item.SetProviderId(MetadataProvider.Tmdb, tmdbId.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item is Series)
|
if (item is Series)
|
||||||
{
|
{
|
||||||
if (ProviderIdParsers.TryParseTmdbSeriesId(xml, out var tmdbId))
|
if (ProviderIdParsers.TryFindTmdbSeriesId(xml, out var tmdbId))
|
||||||
{
|
{
|
||||||
item.SetProviderId(MetadataProvider.Tmdb, tmdbId);
|
item.SetProviderId(MetadataProvider.Tmdb, tmdbId.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ProviderIdParsers.TryParseTvdbId(xml, out var tvdbId))
|
if (ProviderIdParsers.TryFindTvdbId(xml, out var tvdbId))
|
||||||
{
|
{
|
||||||
item.SetProviderId(MetadataProvider.Tvdb, tvdbId);
|
item.SetProviderId(MetadataProvider.Tvdb, tvdbId.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using MediaBrowser.Common.Providers;
|
using System;
|
||||||
|
using MediaBrowser.Common.Providers;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Jellyfin.Common.Tests.Providers
|
namespace Jellyfin.Common.Tests.Providers
|
||||||
|
@ -20,9 +21,9 @@ namespace Jellyfin.Common.Tests.Providers
|
||||||
[InlineData("tt123456789", 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.TryFindImdbId(text, out ReadOnlySpan<char> parsedId);
|
||||||
Assert.Equal(shouldSucceed, succeeded);
|
Assert.Equal(shouldSucceed, succeeded);
|
||||||
Assert.Equal(imdbId, parsedId);
|
Assert.Equal(imdbId ?? Span<char>.Empty.ToString(), parsedId.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
@ -32,9 +33,9 @@ namespace Jellyfin.Common.Tests.Providers
|
||||||
[InlineData("https://www.themoviedb.org/tv/1668-friends", false, null)]
|
[InlineData("https://www.themoviedb.org/tv/1668-friends", false, null)]
|
||||||
public void Parse_TmdbMovie(string text, bool shouldSucceed, string? tmdbId)
|
public void Parse_TmdbMovie(string text, bool shouldSucceed, string? tmdbId)
|
||||||
{
|
{
|
||||||
var succeeded = ProviderIdParsers.TryParseTmdbMovieId(text, out string? parsedId);
|
var succeeded = ProviderIdParsers.TryFindTmdbMovieId(text, out ReadOnlySpan<char> parsedId);
|
||||||
Assert.Equal(shouldSucceed, succeeded);
|
Assert.Equal(shouldSucceed, succeeded);
|
||||||
Assert.Equal(tmdbId, parsedId);
|
Assert.Equal(tmdbId ?? Span<char>.Empty.ToString(), parsedId.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
@ -44,9 +45,9 @@ namespace Jellyfin.Common.Tests.Providers
|
||||||
[InlineData("https://www.themoviedb.org/movie/30287-fallo", false, null)]
|
[InlineData("https://www.themoviedb.org/movie/30287-fallo", false, null)]
|
||||||
public void Parse_TmdbSeries(string text, bool shouldSucceed, string? tmdbId)
|
public void Parse_TmdbSeries(string text, bool shouldSucceed, string? tmdbId)
|
||||||
{
|
{
|
||||||
var succeeded = ProviderIdParsers.TryParseTmdbSeriesId(text, out string? parsedId);
|
var succeeded = ProviderIdParsers.TryFindTmdbSeriesId(text, out ReadOnlySpan<char> parsedId);
|
||||||
Assert.Equal(shouldSucceed, succeeded);
|
Assert.Equal(shouldSucceed, succeeded);
|
||||||
Assert.Equal(tmdbId, parsedId);
|
Assert.Equal(tmdbId ?? Span<char>.Empty.ToString(), parsedId.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
|
@ -56,9 +57,9 @@ namespace Jellyfin.Common.Tests.Providers
|
||||||
[InlineData("https://www.themoviedb.org/tv/1668-friends", false, null)]
|
[InlineData("https://www.themoviedb.org/tv/1668-friends", false, null)]
|
||||||
public void Parse_Tvdb(string text, bool shouldSucceed, string? tvdbId)
|
public void Parse_Tvdb(string text, bool shouldSucceed, string? tvdbId)
|
||||||
{
|
{
|
||||||
var succeeded = ProviderIdParsers.TryParseTvdbId(text, out string? parsedId);
|
var succeeded = ProviderIdParsers.TryFindTvdbId(text, out ReadOnlySpan<char> parsedId);
|
||||||
Assert.Equal(shouldSucceed, succeeded);
|
Assert.Equal(shouldSucceed, succeeded);
|
||||||
Assert.Equal(tvdbId, parsedId);
|
Assert.Equal(tvdbId ?? Span<char>.Empty.ToString(), parsedId.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user