mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2025-09-28 04:51:45 +02:00
Improve GetLongFromString and ParseFields
2700X faster
This commit is contained in:
@@ -52,5 +52,16 @@ namespace NzbDrone.Core.Test.ParserTests
|
|||||||
{
|
{
|
||||||
ParseUtil.CoerceDouble(original).Should().Be(parsedInt);
|
ParseUtil.CoerceDouble(original).Should().Be(parsedInt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestCase(null, null)]
|
||||||
|
[TestCase("", null)]
|
||||||
|
[TestCase("1", 1)]
|
||||||
|
[TestCase("1000 grabs", 1000)]
|
||||||
|
[TestCase("asdf123asdf", 123)]
|
||||||
|
[TestCase("asdf123asdf456asdf", 123)]
|
||||||
|
public void should_parse_long_from_string(string original, long? parsedInt)
|
||||||
|
{
|
||||||
|
ParseUtil.GetLongFromString(original).Should().Be(parsedInt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -541,38 +541,23 @@ namespace NzbDrone.Core.Indexers.Cardigann
|
|||||||
value = release.ImdbId.ToString();
|
value = release.ImdbId.ToString();
|
||||||
break;
|
break;
|
||||||
case "tmdbid":
|
case "tmdbid":
|
||||||
var tmdbIdRegex = new Regex(@"(\d+)", RegexOptions.Compiled);
|
release.TmdbId = (int)ParseUtil.GetLongFromString(value);
|
||||||
var tmdbIdMatch = tmdbIdRegex.Match(value);
|
|
||||||
var tmdbId = tmdbIdMatch.Groups[1].Value;
|
|
||||||
release.TmdbId = (int)ParseUtil.CoerceLong(tmdbId);
|
|
||||||
value = release.TmdbId.ToString();
|
value = release.TmdbId.ToString();
|
||||||
break;
|
break;
|
||||||
case "rageid":
|
case "rageid":
|
||||||
var rageIdRegex = new Regex(@"(\d+)", RegexOptions.Compiled);
|
release.TvRageId = (int)ParseUtil.GetLongFromString(value);
|
||||||
var rageIdMatch = rageIdRegex.Match(value);
|
|
||||||
var rageId = rageIdMatch.Groups[1].Value;
|
|
||||||
release.TvRageId = (int)ParseUtil.CoerceLong(rageId);
|
|
||||||
value = release.TvRageId.ToString();
|
value = release.TvRageId.ToString();
|
||||||
break;
|
break;
|
||||||
case "traktid":
|
case "traktid":
|
||||||
var traktIdRegex = new Regex(@"(\d+)", RegexOptions.Compiled);
|
release.TraktId = (int)ParseUtil.GetLongFromString(value);
|
||||||
var traktIdMatch = traktIdRegex.Match(value);
|
|
||||||
var traktId = traktIdMatch.Groups[1].Value;
|
|
||||||
release.TraktId = (int)ParseUtil.CoerceLong(traktId);
|
|
||||||
value = release.TraktId.ToString();
|
value = release.TraktId.ToString();
|
||||||
break;
|
break;
|
||||||
case "tvdbid":
|
case "tvdbid":
|
||||||
var tvdbIdRegex = new Regex(@"(\d+)", RegexOptions.Compiled);
|
release.TvdbId = (int)ParseUtil.GetLongFromString(value);
|
||||||
var tvdbIdMatch = tvdbIdRegex.Match(value);
|
|
||||||
var tvdbId = tvdbIdMatch.Groups[1].Value;
|
|
||||||
release.TvdbId = (int)ParseUtil.CoerceLong(tvdbId);
|
|
||||||
value = release.TvdbId.ToString();
|
value = release.TvdbId.ToString();
|
||||||
break;
|
break;
|
||||||
case "doubanid":
|
case "doubanid":
|
||||||
var doubanIdRegex = new Regex(@"(\d+)", RegexOptions.Compiled);
|
release.DoubanId = (int)ParseUtil.GetLongFromString(value);
|
||||||
var doubanIdMatch = doubanIdRegex.Match(value);
|
|
||||||
var doubanId = doubanIdMatch.Groups[1].Value;
|
|
||||||
release.DoubanId = (int)ParseUtil.CoerceLong(doubanId);
|
|
||||||
value = release.DoubanId.ToString();
|
value = release.DoubanId.ToString();
|
||||||
break;
|
break;
|
||||||
case "poster":
|
case "poster":
|
||||||
|
@@ -2,6 +2,7 @@ using System.Globalization;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using Microsoft.AspNetCore.WebUtilities;
|
using Microsoft.AspNetCore.WebUtilities;
|
||||||
|
using NzbDrone.Common.Extensions;
|
||||||
|
|
||||||
namespace NzbDrone.Core.Parser
|
namespace NzbDrone.Core.Parser
|
||||||
{
|
{
|
||||||
@@ -65,20 +66,29 @@ namespace NzbDrone.Core.Parser
|
|||||||
|
|
||||||
public static long? GetLongFromString(string str)
|
public static long? GetLongFromString(string str)
|
||||||
{
|
{
|
||||||
if (str == null)
|
if (str.IsNullOrWhiteSpace())
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var idRegEx = new Regex(@"(\d+)", RegexOptions.Compiled);
|
var extractedLong = string.Empty;
|
||||||
var idMatch = idRegEx.Match(str);
|
|
||||||
if (!idMatch.Success)
|
foreach (var c in str)
|
||||||
{
|
{
|
||||||
return null;
|
if (c < '0' || c > '9')
|
||||||
|
{
|
||||||
|
if (extractedLong.Length > 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
extractedLong += c;
|
||||||
}
|
}
|
||||||
|
|
||||||
var id = idMatch.Groups[1].Value;
|
return CoerceLong(extractedLong);
|
||||||
return CoerceLong(id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int? GetImdbID(string imdbstr)
|
public static int? GetImdbID(string imdbstr)
|
||||||
|
24
src/Prowlarr.Benchmark.Test/ParserTests/ParseUtilFixture.cs
Normal file
24
src/Prowlarr.Benchmark.Test/ParserTests/ParseUtilFixture.cs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using BenchmarkDotNet.Attributes;
|
||||||
|
using NzbDrone.Core.Parser;
|
||||||
|
|
||||||
|
namespace NzbDrone.Benchmark.Test.ParserTests
|
||||||
|
{
|
||||||
|
[InProcess]
|
||||||
|
public class ParseUtilFixture
|
||||||
|
{
|
||||||
|
[Benchmark]
|
||||||
|
[Arguments("123456789")]
|
||||||
|
[Arguments("")]
|
||||||
|
[Arguments("asd8f7asdf")]
|
||||||
|
[Arguments("sdf")]
|
||||||
|
public void parse_long_from_string(string dateInput)
|
||||||
|
{
|
||||||
|
ParseUtil.GetLongFromString(dateInput);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user