From fa626a53e6f3db04d2aab05f85b4a4586379a5e7 Mon Sep 17 00:00:00 2001 From: Qstick Date: Mon, 17 Jan 2022 21:27:22 -0600 Subject: [PATCH] Fixed: Smarter Int normalization Fixes #787 --- .../ParserTests/ParseUtilFixture.cs | 18 +++++++++++++ src/NzbDrone.Core/Parser/ParseUtil.cs | 26 ++++++++++++++----- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/NzbDrone.Core.Test/ParserTests/ParseUtilFixture.cs b/src/NzbDrone.Core.Test/ParserTests/ParseUtilFixture.cs index 5766c0b40..24449ed7d 100644 --- a/src/NzbDrone.Core.Test/ParserTests/ParseUtilFixture.cs +++ b/src/NzbDrone.Core.Test/ParserTests/ParseUtilFixture.cs @@ -30,9 +30,27 @@ public void should_normalize_multiple_spaces(string original, string newString) [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); + } } } diff --git a/src/NzbDrone.Core/Parser/ParseUtil.cs b/src/NzbDrone.Core/Parser/ParseUtil.cs index e1f7fe8c6..f8cbc7bbc 100644 --- a/src/NzbDrone.Core/Parser/ParseUtil.cs +++ b/src/NzbDrone.Core/Parser/ParseUtil.cs @@ -16,14 +16,26 @@ public static class ParseUtil 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 @@ private static string NormalizeNumber(string s) 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) {