From 02e8d19e486895be9d7f25ac0c21eecfe5c76e18 Mon Sep 17 00:00:00 2001 From: Oleksii Holub <1935960+Tyrrrz@users.noreply.github.com> Date: Mon, 21 Feb 2022 22:35:23 +0200 Subject: [PATCH] Refactor polyfills --- CliFx/Utils/Polyfills.Collections.cs | 26 ++++++++++++++ CliFx/Utils/Polyfills.Streams.cs | 11 ++++++ CliFx/Utils/Polyfills.Strings.cs | 12 +++++++ CliFx/Utils/Polyfills.cs | 52 ---------------------------- 4 files changed, 49 insertions(+), 52 deletions(-) create mode 100644 CliFx/Utils/Polyfills.Collections.cs create mode 100644 CliFx/Utils/Polyfills.Streams.cs create mode 100644 CliFx/Utils/Polyfills.Strings.cs delete mode 100644 CliFx/Utils/Polyfills.cs diff --git a/CliFx/Utils/Polyfills.Collections.cs b/CliFx/Utils/Polyfills.Collections.cs new file mode 100644 index 0000000..7c3cbed --- /dev/null +++ b/CliFx/Utils/Polyfills.Collections.cs @@ -0,0 +1,26 @@ +// ReSharper disable CheckNamespace + +#if NETSTANDARD2_0 +using System.Collections.Generic; + +internal static class CollectionPolyfills +{ + public static void Deconstruct(this KeyValuePair pair, out TKey key, out TValue value) + { + key = pair.Key; + value = pair.Value; + } + + public static TValue GetValueOrDefault(this IReadOnlyDictionary dic, TKey key) => + dic.TryGetValue(key!, out var result) ? result! : default!; +} + +namespace System.Linq +{ + internal static class LinqPolyfills + { + public static HashSet ToHashSet(this IEnumerable source, IEqualityComparer comparer) => + new(source, comparer); + } +} +#endif \ No newline at end of file diff --git a/CliFx/Utils/Polyfills.Streams.cs b/CliFx/Utils/Polyfills.Streams.cs new file mode 100644 index 0000000..69ad260 --- /dev/null +++ b/CliFx/Utils/Polyfills.Streams.cs @@ -0,0 +1,11 @@ +// ReSharper disable CheckNamespace + +#if NETSTANDARD2_0 +using System.IO; + +internal static class StreamPolyfills +{ + public static void Write(this Stream stream, byte[] buffer) => + stream.Write(buffer, 0, buffer.Length); +} +#endif \ No newline at end of file diff --git a/CliFx/Utils/Polyfills.Strings.cs b/CliFx/Utils/Polyfills.Strings.cs new file mode 100644 index 0000000..b60cb88 --- /dev/null +++ b/CliFx/Utils/Polyfills.Strings.cs @@ -0,0 +1,12 @@ +// ReSharper disable CheckNamespace + +#if NETSTANDARD2_0 +internal static class StringPolyfills +{ + public static bool StartsWith(this string str, char c) => + str.Length > 0 && str[0] == c; + + public static bool EndsWith(this string str, char c) => + str.Length > 0 && str[str.Length - 1] == c; +} +#endif \ No newline at end of file diff --git a/CliFx/Utils/Polyfills.cs b/CliFx/Utils/Polyfills.cs deleted file mode 100644 index 63b8d34..0000000 --- a/CliFx/Utils/Polyfills.cs +++ /dev/null @@ -1,52 +0,0 @@ -// ReSharper disable CheckNamespace - -#if NETSTANDARD2_0 -using System; -using System.Collections.Generic; -using System.IO; - -internal static partial class PolyfillExtensions -{ - public static bool StartsWith(this string str, char c) => - str.Length > 0 && str[0] == c; - - public static bool EndsWith(this string str, char c) => - str.Length > 0 && str[str.Length - 1] == c; - - public static string[] Split(this string str, char separator, StringSplitOptions splitOptions) => - str.Split(new[] {separator}, splitOptions); -} - -internal static partial class PolyfillExtensions -{ - public static void Deconstruct(this KeyValuePair pair, out TKey key, out TValue value) - { - key = pair.Key; - value = pair.Value; - } -} - -internal static partial class PolyfillExtensions -{ - public static void Write(this Stream stream, byte[] buffer) => - stream.Write(buffer, 0, buffer.Length); -} - -namespace System.Linq -{ - internal static class PolyfillExtensions - { - public static HashSet ToHashSet(this IEnumerable source, IEqualityComparer comparer) => - new(source, comparer); - } -} - -namespace System.Collections.Generic -{ - internal static class PolyfillExtensions - { - public static TValue GetValueOrDefault(this IReadOnlyDictionary dic, TKey key) => - dic.TryGetValue(key!, out var result) ? result! : default!; - } -} -#endif \ No newline at end of file