diff --git a/src/NzbDrone.Core.Test/ParserTests/ParseUtilFixture.cs b/src/NzbDrone.Core.Test/ParserTests/ParseUtilFixture.cs index 88ad41303..5766c0b40 100644 --- a/src/NzbDrone.Core.Test/ParserTests/ParseUtilFixture.cs +++ b/src/NzbDrone.Core.Test/ParserTests/ParseUtilFixture.cs @@ -20,5 +20,19 @@ namespace NzbDrone.Core.Test.ParserTests { ParseUtil.GetBytes(stringSize).Should().Be(size); } + + [TestCase(" some string ", "some string")] + public void should_normalize_multiple_spaces(string original, string newString) + { + ParseUtil.NormalizeMultiSpaces(original).Should().Be(newString); + } + + [TestCase("1", 1)] + [TestCase("11", 11)] + [TestCase("1000 grabs", 1000)] + public void should_parse_int_from_string(string original, int parsedInt) + { + ParseUtil.CoerceInt(original).Should().Be(parsedInt); + } } } diff --git a/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs b/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs index 8a4d0268b..9ea8a57d9 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/Cardigann/CardigannBase.cs @@ -220,7 +220,7 @@ namespace NzbDrone.Core.Indexers.Cardigann value = selection.TextContent; } - return ApplyFilters(ParseUtil.NormalizeSpace(value), selector.Filters, variables); + return ApplyFilters(value.Trim(), selector.Filters, variables); } protected string HandleJsonSelector(SelectorBlock selector, JToken parentObj, Dictionary variables = null, bool required = true) @@ -271,7 +271,7 @@ namespace NzbDrone.Core.Indexers.Cardigann } } - return ApplyFilters(ParseUtil.NormalizeSpace(value), selector.Filters, variables); + return ApplyFilters(value.Trim(), selector.Filters, variables); } protected Dictionary GetBaseTemplateVariables() diff --git a/src/NzbDrone.Core/Parser/DateTimeUtil.cs b/src/NzbDrone.Core/Parser/DateTimeUtil.cs index 4d94c412a..a9fb063b9 100644 --- a/src/NzbDrone.Core/Parser/DateTimeUtil.cs +++ b/src/NzbDrone.Core/Parser/DateTimeUtil.cs @@ -121,7 +121,7 @@ namespace NzbDrone.Core.Parser { try { - str = ParseUtil.NormalizeSpace(str); + str = str.Trim(); if (str.ToLower().Contains("now")) { return DateTime.UtcNow; @@ -254,7 +254,7 @@ namespace NzbDrone.Core.Parser // converts a date/time string to a DateTime object using a GoLang layout public static DateTime ParseDateTimeGoLang(string date, string layout) { - date = ParseUtil.NormalizeSpace(date); + date = date.Trim(); var pattern = layout; // year diff --git a/src/NzbDrone.Core/Parser/ParseUtil.cs b/src/NzbDrone.Core/Parser/ParseUtil.cs index 7868ba102..e1f7fe8c6 100644 --- a/src/NzbDrone.Core/Parser/ParseUtil.cs +++ b/src/NzbDrone.Core/Parser/ParseUtil.cs @@ -13,18 +13,16 @@ namespace NzbDrone.Core.Parser RegexOptions.Compiled); private static readonly Regex ImdbId = new Regex(@"^(?:tt)?(\d{1,8})$", RegexOptions.Compiled); - public static string NormalizeSpace(string s) => s.Trim(); - public static string NormalizeMultiSpaces(string s) => - new Regex(@"\s+").Replace(NormalizeSpace(s), " "); + new Regex(@"\s+").Replace(s.Trim(), " "); - public static string NormalizeNumber(string s) + private static string NormalizeNumber(string s) { var valStr = new string(s.Where(c => char.IsDigit(c) || c == '.' || c == ',').ToArray()); valStr = (valStr.Length == 0) ? "0" : valStr.Replace(",", "."); - valStr = NormalizeSpace(valStr).Replace("-", "0"); + valStr = valStr.Trim().Replace("-", "0"); if (valStr.Count(c => c == '.') > 1) { @@ -120,7 +118,7 @@ namespace NzbDrone.Core.Parser return GetBytes(unit, val); } - public static long GetBytes(string unit, float value) + private static long GetBytes(string unit, float value) { unit = unit.Replace("i", "").ToLowerInvariant(); if (unit.Contains("kb")) @@ -146,12 +144,12 @@ namespace NzbDrone.Core.Parser return (long)value; } - public static long BytesFromTB(float tb) => BytesFromGB(tb * 1024f); + private static long BytesFromTB(float tb) => BytesFromGB(tb * 1024f); - public static long BytesFromGB(float gb) => BytesFromMB(gb * 1024f); + private static long BytesFromGB(float gb) => BytesFromMB(gb * 1024f); - public static long BytesFromMB(float mb) => BytesFromKB(mb * 1024f); + private static long BytesFromMB(float mb) => BytesFromKB(mb * 1024f); - public static long BytesFromKB(float kb) => (long)(kb * 1024f); + private static long BytesFromKB(float kb) => (long)(kb * 1024f); } }