Fixed: Smarter Int normalization

Fixes #787
pull/786/head
Qstick 3 years ago
parent 76daee3a1b
commit fa626a53e6

@ -30,9 +30,27 @@ namespace NzbDrone.Core.Test.ParserTests
[TestCase("1", 1)]
[TestCase("11", 11)]
[TestCase("1000 grabs", 1000)]
[TestCase("2.222", 2222)]
[TestCase("2,222", 2222)]
[TestCase("2 222", 2222)]
[TestCase("2,22", 222)]
public void should_parse_int_from_string(string original, int parsedInt)
{
ParseUtil.CoerceInt(original).Should().Be(parsedInt);
}
[TestCase("1.0", 1.0)]
[TestCase("1.1", 1.1)]
[TestCase("1000 grabs", 1000.0)]
[TestCase("2.222", 2.222)]
[TestCase("2,222", 2.222)]
[TestCase("2.222,22", 2222.22)]
[TestCase("2,222.22", 2222.22)]
[TestCase("2 222", 2222.0)]
[TestCase("2,22", 2.22)]
public void should_parse_double_from_string(string original, double parsedInt)
{
ParseUtil.CoerceDouble(original).Should().Be(parsedInt);
}
}
}

@ -16,14 +16,26 @@ namespace NzbDrone.Core.Parser
public static string NormalizeMultiSpaces(string s) =>
new Regex(@"\s+").Replace(s.Trim(), " ");
private static string NormalizeNumber(string s)
private static string NormalizeNumber(string s, bool isInt = false)
{
var valStr = new string(s.Where(c => char.IsDigit(c) || c == '.' || c == ',').ToArray());
valStr = (valStr.Length == 0) ? "0" : valStr.Replace(",", ".");
valStr = valStr.Trim().Replace("-", "0");
if (isInt)
{
if (valStr.Contains(',') && valStr.Contains('.'))
{
return valStr;
}
valStr = (valStr.Length == 0) ? "0" : valStr.Replace(".", ",");
return valStr;
}
valStr = (valStr.Length == 0) ? "0" : valStr.Replace(",", ".");
if (valStr.Count(c => c == '.') > 1)
{
var lastOcc = valStr.LastIndexOf('.');
@ -39,17 +51,17 @@ namespace NzbDrone.Core.Parser
public static float CoerceFloat(string str) => float.Parse(NormalizeNumber(str), NumberStyles.Any, CultureInfo.InvariantCulture);
public static int CoerceInt(string str) => int.Parse(NormalizeNumber(str), NumberStyles.Any, CultureInfo.InvariantCulture);
public static int CoerceInt(string str) => int.Parse(NormalizeNumber(str, true), NumberStyles.Any, CultureInfo.InvariantCulture);
public static long CoerceLong(string str) => long.Parse(NormalizeNumber(str), NumberStyles.Any, CultureInfo.InvariantCulture);
public static long CoerceLong(string str) => long.Parse(NormalizeNumber(str, true), NumberStyles.Any, CultureInfo.InvariantCulture);
public static bool TryCoerceDouble(string str, out double result) => double.TryParse(NormalizeNumber(str), NumberStyles.Any, CultureInfo.InvariantCulture, out result);
public static bool TryCoerceFloat(string str, out float result) => float.TryParse(NormalizeNumber(str), NumberStyles.Any, CultureInfo.InvariantCulture, out result);
public static bool TryCoerceInt(string str, out int result) => int.TryParse(NormalizeNumber(str), NumberStyles.Any, CultureInfo.InvariantCulture, out result);
public static bool TryCoerceInt(string str, out int result) => int.TryParse(NormalizeNumber(str, true), NumberStyles.Any, CultureInfo.InvariantCulture, out result);
public static bool TryCoerceLong(string str, out long result) => long.TryParse(NormalizeNumber(str), NumberStyles.Any, CultureInfo.InvariantCulture, out result);
public static bool TryCoerceLong(string str, out long result) => long.TryParse(NormalizeNumber(str, true), NumberStyles.Any, CultureInfo.InvariantCulture, out result);
public static long? GetLongFromString(string str)
{

Loading…
Cancel
Save