mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2025-09-28 21:12:43 +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);
|
||||
}
|
||||
|
||||
[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();
|
||||
break;
|
||||
case "tmdbid":
|
||||
var tmdbIdRegex = new Regex(@"(\d+)", RegexOptions.Compiled);
|
||||
var tmdbIdMatch = tmdbIdRegex.Match(value);
|
||||
var tmdbId = tmdbIdMatch.Groups[1].Value;
|
||||
release.TmdbId = (int)ParseUtil.CoerceLong(tmdbId);
|
||||
release.TmdbId = (int)ParseUtil.GetLongFromString(value);
|
||||
value = release.TmdbId.ToString();
|
||||
break;
|
||||
case "rageid":
|
||||
var rageIdRegex = new Regex(@"(\d+)", RegexOptions.Compiled);
|
||||
var rageIdMatch = rageIdRegex.Match(value);
|
||||
var rageId = rageIdMatch.Groups[1].Value;
|
||||
release.TvRageId = (int)ParseUtil.CoerceLong(rageId);
|
||||
release.TvRageId = (int)ParseUtil.GetLongFromString(value);
|
||||
value = release.TvRageId.ToString();
|
||||
break;
|
||||
case "traktid":
|
||||
var traktIdRegex = new Regex(@"(\d+)", RegexOptions.Compiled);
|
||||
var traktIdMatch = traktIdRegex.Match(value);
|
||||
var traktId = traktIdMatch.Groups[1].Value;
|
||||
release.TraktId = (int)ParseUtil.CoerceLong(traktId);
|
||||
release.TraktId = (int)ParseUtil.GetLongFromString(value);
|
||||
value = release.TraktId.ToString();
|
||||
break;
|
||||
case "tvdbid":
|
||||
var tvdbIdRegex = new Regex(@"(\d+)", RegexOptions.Compiled);
|
||||
var tvdbIdMatch = tvdbIdRegex.Match(value);
|
||||
var tvdbId = tvdbIdMatch.Groups[1].Value;
|
||||
release.TvdbId = (int)ParseUtil.CoerceLong(tvdbId);
|
||||
release.TvdbId = (int)ParseUtil.GetLongFromString(value);
|
||||
value = release.TvdbId.ToString();
|
||||
break;
|
||||
case "doubanid":
|
||||
var doubanIdRegex = new Regex(@"(\d+)", RegexOptions.Compiled);
|
||||
var doubanIdMatch = doubanIdRegex.Match(value);
|
||||
var doubanId = doubanIdMatch.Groups[1].Value;
|
||||
release.DoubanId = (int)ParseUtil.CoerceLong(doubanId);
|
||||
release.DoubanId = (int)ParseUtil.GetLongFromString(value);
|
||||
value = release.DoubanId.ToString();
|
||||
break;
|
||||
case "poster":
|
||||
|
@@ -2,6 +2,7 @@ using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.AspNetCore.WebUtilities;
|
||||
using NzbDrone.Common.Extensions;
|
||||
|
||||
namespace NzbDrone.Core.Parser
|
||||
{
|
||||
@@ -65,20 +66,29 @@ namespace NzbDrone.Core.Parser
|
||||
|
||||
public static long? GetLongFromString(string str)
|
||||
{
|
||||
if (str == null)
|
||||
if (str.IsNullOrWhiteSpace())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var idRegEx = new Regex(@"(\d+)", RegexOptions.Compiled);
|
||||
var idMatch = idRegEx.Match(str);
|
||||
if (!idMatch.Success)
|
||||
var extractedLong = string.Empty;
|
||||
|
||||
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(id);
|
||||
return CoerceLong(extractedLong);
|
||||
}
|
||||
|
||||
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