Find and fix equal size issue in streams

This commit is contained in:
Matt Nadareski
2024-10-26 00:14:29 -04:00
parent 3b0e74b9f4
commit b0fdff2d6e
2 changed files with 24 additions and 3 deletions

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using SabreTools.Matching.Content;
using Xunit;
@@ -9,7 +8,7 @@ namespace SabreTools.Matching.Test
public class MatchUtilTests
{
[Fact]
public void ExactSizeMatch()
public void ExactSizeArrayMatch()
{
byte[] source = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
byte?[] check = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
@@ -23,5 +22,23 @@ namespace SabreTools.Matching.Test
string? actual = MatchUtil.GetFirstMatch("testfile", source, matchers);
Assert.Equal(expected, actual);
}
[Fact]
public void ExactSizeStreamMatch()
{
byte[] source = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
var stream = new MemoryStream(source);
byte?[] check = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
string expected = "match";
var matchers = new List<ContentMatchSet>
{
new(check, expected),
};
string? actual = MatchUtil.GetFirstMatch("testfile", stream, matchers);
Assert.Equal(expected, actual);
}
}
}

View File

@@ -134,6 +134,10 @@ namespace SabreTools.Matching.Content
if (Needle.Length > stack.Length)
return -1;
// If the needle and stack are identically sized, short-circuit
if (Needle.Length == stack.Length)
return EqualAt(stack, 0) ? 0 : -1;
// Set the default start and end values
int start = Start;
int end = End;