From 19971ec62c5112fec005ed54b9ccffce245f0caa Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 29 Feb 2024 00:31:07 -0500 Subject: [PATCH] Combine ArrayExtensions into Extensions --- ArrayExtensions.cs | 45 ------------------------------------------ Extensions.cs | 49 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 53 deletions(-) delete mode 100644 ArrayExtensions.cs diff --git a/ArrayExtensions.cs b/ArrayExtensions.cs deleted file mode 100644 index 32fd498..0000000 --- a/ArrayExtensions.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; - -namespace SabreTools.Matching -{ - public static class ArrayExtensions - { - /// - /// Indicates whether the specified array is null or has a length of zero - /// - public static bool IsNullOrEmpty(this Array? array) - { - return array == null || array.Length == 0; - } - - //// - /// Returns if the first byte array starts with the second array - /// - public static bool StartsWith(this T[]? arr1, T[]? arr2, bool exact = false) - { - // If we have any invalid inputs, we return false - if (arr1 == null || arr2 == null - || arr1.Length == 0 || arr2.Length == 0 - || arr2.Length > arr1.Length - || (exact && arr1.Length != arr2.Length)) - { - return false; - } - - // Otherwise, loop through and see - for (int i = 0; i < arr2.Length; i++) - { - if (arr1[i] == null && arr2[i] == null) - continue; - else if (arr1[i] == null && arr2[i] != null) - return false; - else if (arr1[i] != null && arr2[i] == null) - return false; - else if (!arr1[i]!.Equals(arr2[i])) - return false; - } - - return true; - } - } -} \ No newline at end of file diff --git a/Extensions.cs b/Extensions.cs index ac9374a..b3f4900 100644 --- a/Extensions.cs +++ b/Extensions.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; @@ -5,6 +6,14 @@ namespace SabreTools.Matching { public static class Extensions { + /// + /// Indicates whether the specified array is null or has a length of zero + /// + public static bool IsNullOrEmpty(this Array? array) + { + return array == null || array.Length == 0; + } + /// /// Find all positions of one array in another, if possible, if possible /// @@ -64,10 +73,16 @@ namespace SabreTools.Matching /// /// See if a byte array starts with another /// - public static bool StartsWith(this byte[] stack, byte[]? needle) + public static bool StartsWith(this byte[] stack, byte[]? needle, bool exact = false) { - if (needle == null) + // If we have any invalid inputs, we return false + if (needle == null + || stack.Length == 0 || needle.Length == 0 + || needle.Length > stack.Length + || (exact && stack.Length != needle.Length)) + { return false; + } return stack.FirstPosition(needle, out int _, start: 0, end: 1); } @@ -75,10 +90,16 @@ namespace SabreTools.Matching /// /// See if a byte array starts with another /// - public static bool StartsWith(this byte[] stack, byte?[]? needle) + public static bool StartsWith(this byte[] stack, byte?[]? needle, bool exact = false) { - if (needle == null) + // If we have any invalid inputs, we return false + if (needle == null + || stack.Length == 0 || needle.Length == 0 + || needle.Length > stack.Length + || (exact && stack.Length != needle.Length)) + { return false; + } return stack.FirstPosition(needle, out int _, start: 0, end: 1); } @@ -86,10 +107,16 @@ namespace SabreTools.Matching /// /// See if a byte array ends with another /// - public static bool EndsWith(this byte[] stack, byte[]? needle) + public static bool EndsWith(this byte[] stack, byte[]? needle, bool exact = false) { - if (needle == null) + // If we have any invalid inputs, we return false + if (needle == null + || stack.Length == 0 || needle.Length == 0 + || needle.Length > stack.Length + || (exact && stack.Length != needle.Length)) + { return false; + } return stack.FirstPosition(needle, out int _, start: stack.Length - needle.Length); } @@ -97,10 +124,16 @@ namespace SabreTools.Matching /// /// See if a byte array ends with another /// - public static bool EndsWith(this byte[] stack, byte?[]? needle) + public static bool EndsWith(this byte[] stack, byte?[]? needle, bool exact = false) { - if (needle == null) + // If we have any invalid inputs, we return false + if (needle == null + || stack.Length == 0 || needle.Length == 0 + || needle.Length > stack.Length + || (exact && stack.Length != needle.Length)) + { return false; + } return stack.FirstPosition(needle, out int _, start: stack.Length - needle.Length); }