From 1763d1773a7238b2282c399865857ba3dbeb7c2f Mon Sep 17 00:00:00 2001 From: Jeffery Thompson Date: Fri, 30 Mar 2018 15:47:46 -0400 Subject: [PATCH 1/4] feat: add TryParseSkipFailures extension --- .../Extensions/EnumerableExtensions.cs | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs index 5233bd5..3bc0fd4 100644 --- a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs @@ -1057,6 +1057,40 @@ namespace ICD.Common.Utils.Extensions } } + // since S# can't do anonymous types + private struct TryParseStruct + { + public readonly T value; + public readonly bool isParsed; + public TryParseStruct(T value, bool isParsed) + { + this.value = value; + this.isParsed = isParsed; + } + } + // since Func<...,T> can't specify `out` parameters + public delegate bool TryParseDelegate(string input, out T output); + + /// + /// Attempts to parse each value of the enumerable, + /// throwing away the values that don't parse correctly. + /// + /// type to parse to + /// enumerable of strings to parse + /// TryParse function for given type + /// enumerable of successfully parsed values + public static IEnumerable TryParseSkipFailures(this IEnumerable extends, TryParseDelegate tryParseFunc) + { + return extends.Select(str => + { + T value = default(T); + bool isParsed = tryParseFunc(str, out value); + return new TryParseStruct(value, isParsed); + }) + .Where(v => v.isParsed == true) + .Select(v => v.value); + } + #if SIMPLSHARP /// @@ -1093,5 +1127,5 @@ namespace ICD.Common.Utils.Extensions } #endif - } + } } From ce5288956e51e4667b5f19dd38068e0c8f81b53c Mon Sep 17 00:00:00 2001 From: Jeffery Thompson Date: Fri, 30 Mar 2018 15:50:50 -0400 Subject: [PATCH 2/4] fix: check if arguments are null in TryParseSkipFailures --- ICD.Common.Utils/Extensions/EnumerableExtensions.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs index 3bc0fd4..63da5e3 100644 --- a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs @@ -1081,6 +1081,12 @@ namespace ICD.Common.Utils.Extensions /// enumerable of successfully parsed values public static IEnumerable TryParseSkipFailures(this IEnumerable extends, TryParseDelegate tryParseFunc) { + if (extends == null) + throw new ArgumentNullException("extends"); + + if (tryParseFunc == null) + throw new ArgumentNullException("tryParseFunc"); + return extends.Select(str => { T value = default(T); From af202f612e8053c6b10a42ecb0cdfc2e295aa4dd Mon Sep 17 00:00:00 2001 From: Jeffery Thompson Date: Fri, 30 Mar 2018 15:52:39 -0400 Subject: [PATCH 3/4] style: convert spaces to tabs --- .../Extensions/EnumerableExtensions.cs | 72 +++++++++---------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs index 63da5e3..794687e 100644 --- a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs @@ -1057,45 +1057,45 @@ namespace ICD.Common.Utils.Extensions } } - // since S# can't do anonymous types - private struct TryParseStruct - { - public readonly T value; - public readonly bool isParsed; - public TryParseStruct(T value, bool isParsed) - { - this.value = value; - this.isParsed = isParsed; - } - } - // since Func<...,T> can't specify `out` parameters - public delegate bool TryParseDelegate(string input, out T output); + // since S# can't do anonymous types + private struct TryParseStruct + { + public readonly T value; + public readonly bool isParsed; + public TryParseStruct(T value, bool isParsed) + { + this.value = value; + this.isParsed = isParsed; + } + } + // since Func<...,T> can't specify `out` parameters + public delegate bool TryParseDelegate(string input, out T output); - /// - /// Attempts to parse each value of the enumerable, - /// throwing away the values that don't parse correctly. - /// - /// type to parse to - /// enumerable of strings to parse - /// TryParse function for given type - /// enumerable of successfully parsed values - public static IEnumerable TryParseSkipFailures(this IEnumerable extends, TryParseDelegate tryParseFunc) - { - if (extends == null) - throw new ArgumentNullException("extends"); + /// + /// Attempts to parse each value of the enumerable, + /// throwing away the values that don't parse correctly. + /// + /// type to parse to + /// enumerable of strings to parse + /// TryParse function for given type + /// enumerable of successfully parsed values + public static IEnumerable TryParseSkipFailures(this IEnumerable extends, TryParseDelegate tryParseFunc) + { + if (extends == null) + throw new ArgumentNullException("extends"); - if (tryParseFunc == null) - throw new ArgumentNullException("tryParseFunc"); + if (tryParseFunc == null) + throw new ArgumentNullException("tryParseFunc"); - return extends.Select(str => - { - T value = default(T); - bool isParsed = tryParseFunc(str, out value); - return new TryParseStruct(value, isParsed); - }) - .Where(v => v.isParsed == true) - .Select(v => v.value); - } + return extends.Select(str => + { + T value = default(T); + bool isParsed = tryParseFunc(str, out value); + return new TryParseStruct(value, isParsed); + }) + .Where(v => v.isParsed == true) + .Select(v => v.value); + } #if SIMPLSHARP From 88a79425510f0a396ff30730f003b0b95bbebd28 Mon Sep 17 00:00:00 2001 From: Jeffery Thompson Date: Fri, 30 Mar 2018 15:58:09 -0400 Subject: [PATCH 4/4] style: convert spaces to tabs... again --- ICD.Common.Utils/Extensions/EnumerableExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs index 794687e..3d928ae 100644 --- a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs @@ -1133,5 +1133,5 @@ namespace ICD.Common.Utils.Extensions } #endif - } + } }