From b1d95652eaa10959bb7f2f0dc0b2c3a4abe3a322 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 25 Nov 2024 12:27:16 -0500 Subject: [PATCH] Implement EqualsExactly extension --- SabreTools.Matching.Test/ExtensionsTests.cs | 52 +++++++++++++++++++++ SabreTools.Matching/Extensions.cs | 30 +++++++++++- 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/SabreTools.Matching.Test/ExtensionsTests.cs b/SabreTools.Matching.Test/ExtensionsTests.cs index b964529..da9b4c4 100644 --- a/SabreTools.Matching.Test/ExtensionsTests.cs +++ b/SabreTools.Matching.Test/ExtensionsTests.cs @@ -237,6 +237,58 @@ namespace SabreTools.Matching.Test #endregion + #region Equals Exactly + + [Fact] + public void EqualsExactly_EmptyStack_NoMatches() + { + byte[] stack = []; + bool found = stack.EqualsExactly([0x01]); + Assert.False(found); + } + + [Fact] + public void EqualsExactly_EmptyNeedle_NoMatches() + { + byte[] stack = [0x01]; + bool found = stack.EqualsExactly(Array.Empty()); + Assert.False(found); + } + + [Fact] + public void EqualsExactly_ShorterNeedle_NoMatches() + { + byte[] stack = [0x01, 0x02]; + bool found = stack.EqualsExactly([0x01]); + Assert.False(found); + } + + [Fact] + public void EqualsExactly_LongerNeedle_NoMatches() + { + byte[] stack = [0x01]; + bool found = stack.EqualsExactly([0x01, 0x02]); + Assert.False(found); + } + + [Fact] + public void EqualsExactly_Matching_Matches() + { + byte[] stack = [0x01, 0x02]; + bool found = stack.EqualsExactly([0x01, 0x02]); + Assert.True(found); + } + + [Fact] + public void EqualsExactly_Mismatch_NoMatches() + { + byte[] stack = [0x01, 0x03]; + bool found = stack.EqualsExactly([0x01, 0x02]); + Assert.False(found); + } + + #endregion + #region Starts With [Fact] diff --git a/SabreTools.Matching/Extensions.cs b/SabreTools.Matching/Extensions.cs index 0504518..1bc91b7 100644 --- a/SabreTools.Matching/Extensions.cs +++ b/SabreTools.Matching/Extensions.cs @@ -4,7 +4,6 @@ using SabreTools.Matching.Content; namespace SabreTools.Matching { - // TODO: Write SequenceEqual equivilent: EqualsExactly public static class Extensions { /// @@ -137,6 +136,35 @@ namespace SabreTools.Matching return matcher.Match(stack, reverse: true); } + /// + /// Check if a byte array exactly matches another + /// + /// Byte array to search within + /// Byte array representing the search value + public static bool EqualsExactly(this byte[] stack, byte[] needle) + { + byte?[] nullableNeedle = Array.ConvertAll(needle, b => (byte?)b); + return EqualsExactly(stack, nullableNeedle); + } + + /// + /// Check if a byte array exactly matches another + /// + /// Byte array to search within + /// Byte array representing the search value + public static bool EqualsExactly(this byte[] stack, byte?[] needle) + { + // If either set is null or empty + if (stack.Length == 0 || needle.Length == 0) + return false; + + // If the needle is not the exact length of the stack + if (needle.Length != stack.Length) + return false; + + return FirstPosition(stack, needle, start: 0, end: 1) == 0; + } + /// /// Check if a byte array starts with another ///