ParseUtil Cleanup

This commit is contained in:
Qstick
2021-11-28 16:47:16 -06:00
parent 9b9d2f2798
commit 2e103d6dba
4 changed files with 26 additions and 14 deletions

View File

@@ -20,5 +20,19 @@ namespace NzbDrone.Core.Test.ParserTests
{ {
ParseUtil.GetBytes(stringSize).Should().Be(size); 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);
}
} }
} }

View File

@@ -220,7 +220,7 @@ namespace NzbDrone.Core.Indexers.Cardigann
value = selection.TextContent; 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<string, object> variables = null, bool required = true) protected string HandleJsonSelector(SelectorBlock selector, JToken parentObj, Dictionary<string, object> 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<string, object> GetBaseTemplateVariables() protected Dictionary<string, object> GetBaseTemplateVariables()

View File

@@ -121,7 +121,7 @@ namespace NzbDrone.Core.Parser
{ {
try try
{ {
str = ParseUtil.NormalizeSpace(str); str = str.Trim();
if (str.ToLower().Contains("now")) if (str.ToLower().Contains("now"))
{ {
return DateTime.UtcNow; return DateTime.UtcNow;
@@ -254,7 +254,7 @@ namespace NzbDrone.Core.Parser
// converts a date/time string to a DateTime object using a GoLang layout // converts a date/time string to a DateTime object using a GoLang layout
public static DateTime ParseDateTimeGoLang(string date, string layout) public static DateTime ParseDateTimeGoLang(string date, string layout)
{ {
date = ParseUtil.NormalizeSpace(date); date = date.Trim();
var pattern = layout; var pattern = layout;
// year // year

View File

@@ -13,18 +13,16 @@ namespace NzbDrone.Core.Parser
RegexOptions.Compiled); RegexOptions.Compiled);
private static readonly Regex ImdbId = new Regex(@"^(?:tt)?(\d{1,8})$", 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) => 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()); var valStr = new string(s.Where(c => char.IsDigit(c) || c == '.' || c == ',').ToArray());
valStr = (valStr.Length == 0) ? "0" : valStr.Replace(",", "."); valStr = (valStr.Length == 0) ? "0" : valStr.Replace(",", ".");
valStr = NormalizeSpace(valStr).Replace("-", "0"); valStr = valStr.Trim().Replace("-", "0");
if (valStr.Count(c => c == '.') > 1) if (valStr.Count(c => c == '.') > 1)
{ {
@@ -120,7 +118,7 @@ namespace NzbDrone.Core.Parser
return GetBytes(unit, val); 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(); unit = unit.Replace("i", "").ToLowerInvariant();
if (unit.Contains("kb")) if (unit.Contains("kb"))
@@ -146,12 +144,12 @@ namespace NzbDrone.Core.Parser
return (long)value; 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);
} }
} }