From 87cd8cefbbdef693dc49789e4a9d6c195acfd31f Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Wed, 18 Mar 2026 12:37:48 -0400 Subject: [PATCH] Make transform better --- SabreTools.IO.Test/Transform/CombineTests.cs | 22 +++++++-------- .../SabreTools.IO.Transform/Combine.cs | 28 ++++++++----------- .../SabreTools.IO.Transform/Enums.cs | 26 ----------------- 3 files changed, 22 insertions(+), 54 deletions(-) diff --git a/SabreTools.IO.Test/Transform/CombineTests.cs b/SabreTools.IO.Test/Transform/CombineTests.cs index b7cc6f9..c180244 100644 --- a/SabreTools.IO.Test/Transform/CombineTests.cs +++ b/SabreTools.IO.Test/Transform/CombineTests.cs @@ -56,7 +56,7 @@ namespace SabreTools.IO.Test.Transform string odd = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt"); string output = Guid.NewGuid().ToString(); - bool actual = Combine.Interleave(even, odd, output, BlockSize.Byte); + bool actual = Combine.Interleave(even, odd, output, 1); Assert.False(actual); } @@ -67,33 +67,33 @@ namespace SabreTools.IO.Test.Transform string odd = "NOT A REAL PATH"; string output = Guid.NewGuid().ToString(); - bool actual = Combine.Interleave(even, odd, output, BlockSize.Byte); + bool actual = Combine.Interleave(even, odd, output, 1); Assert.False(actual); } [Fact] - public void Interleave_InvalidType_False() + public void Interleave_InvalidValue_False() { string even = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt"); string odd = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt"); string output = Guid.NewGuid().ToString(); - bool actual = Combine.Interleave(even, odd, output, (BlockSize)int.MaxValue); + bool actual = Combine.Interleave(even, odd, output, -1); Assert.False(actual); } [Theory] - [InlineData(BlockSize.Byte, "TThhiiss ddooeessnn''tt mmaattcchh aannyytthhiinngg")] - [InlineData(BlockSize.Word, "ThThisis d doeoesnsn't't m matatchch a anynyththiningg")] - [InlineData(BlockSize.Dword, "ThisThis doe doesn'tsn't mat match ach anythnythinging")] - [InlineData(BlockSize.Qword, "This doeThis doesn't matsn't match anythch anythinging")] - public void Interleave_SameLength_True(BlockSize type, string expected) + [InlineData(1, "TThhiiss ddooeessnn''tt mmaattcchh aannyytthhiinngg")] + [InlineData(2, "ThThisis d doeoesnsn't't m matatchch a anynyththiningg")] + [InlineData(4, "ThisThis doe doesn'tsn't mat match ach anythnythinging")] + [InlineData(8, "This doeThis doesn't matsn't match anythch anythinging")] + public void Interleave_SameLength_True(int blockSize, string expected) { string even = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt"); string odd = Path.Combine(Environment.CurrentDirectory, "TestData", "ascii.txt"); string output = Guid.NewGuid().ToString(); - bool actual = Combine.Interleave(even, odd, output, type); + bool actual = Combine.Interleave(even, odd, output, blockSize); Assert.True(actual); string text = File.ReadAllText(output); @@ -110,7 +110,7 @@ namespace SabreTools.IO.Test.Transform string output = Guid.NewGuid().ToString(); - bool actual = Combine.Interleave(even, odd, output, BlockSize.Byte); + bool actual = Combine.Interleave(even, odd, output, 1); Assert.True(actual); string text = File.ReadAllText(output); diff --git a/SabreTools.IO/SabreTools.IO.Transform/Combine.cs b/SabreTools.IO/SabreTools.IO.Transform/Combine.cs index fe2468f..81b4f1f 100644 --- a/SabreTools.IO/SabreTools.IO.Transform/Combine.cs +++ b/SabreTools.IO/SabreTools.IO.Transform/Combine.cs @@ -70,9 +70,9 @@ namespace SabreTools.IO.Transform /// First file to interleave /// Second file to interleave /// Path to the output file - /// representing how to process the inputs + /// Number of bytes read before switching input /// True if the files were interleaved successfully, false otherwise - public static bool Interleave(string even, string odd, string output, BlockSize type) + public static bool Interleave(string even, string odd, string output, int blockSize) { // If either file does not exist if (!File.Exists(even) || !File.Exists(odd)) @@ -85,7 +85,7 @@ namespace SabreTools.IO.Transform using var oddStream = File.Open(odd, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); // Interleave the streams - using var interleaved = Interleave(evenStream, oddStream, type); + using var interleaved = Interleave(evenStream, oddStream, blockSize); if (interleaved is null) return false; @@ -111,26 +111,20 @@ namespace SabreTools.IO.Transform /// First stream to interleave /// Second stream to interleave /// Path to the output file - /// representing how to process the inputs + /// Number of bytes read before switching input /// A filled stream on success, null otherwise /// - /// Thrown if is not a recognized value. + /// Thrown if is non-positive. /// - public static Stream? Interleave(Stream even, Stream odd, BlockSize type) + public static Stream? Interleave(Stream even, Stream odd, int blockSize) { // If either stream is unreadable if (!even.CanRead || !odd.CanRead) return null; - // Get the number of bytes to process - int byteCount = type switch - { - BlockSize.Byte => 1, - BlockSize.Word => 2, - BlockSize.Dword => 4, - BlockSize.Qword => 8, - _ => throw new ArgumentOutOfRangeException(nameof(type)), - }; + // If the block size is invalid + if (blockSize <= 0) + throw new ArgumentOutOfRangeException(nameof(blockSize)); try { @@ -141,8 +135,8 @@ namespace SabreTools.IO.Transform bool useEven = true; while (even.Position < even.Length || odd.Position < odd.Length) { - byte[] read = new byte[byteCount]; - int actual = (useEven ? even : odd).Read(read, 0, byteCount); + byte[] read = new byte[blockSize]; + int actual = (useEven ? even : odd).Read(read, 0, blockSize); outputStream.Write(read, 0, actual); outputStream.Flush(); useEven = !useEven; diff --git a/SabreTools.IO/SabreTools.IO.Transform/Enums.cs b/SabreTools.IO/SabreTools.IO.Transform/Enums.cs index 8c696ea..a64b9fa 100644 --- a/SabreTools.IO/SabreTools.IO.Transform/Enums.cs +++ b/SabreTools.IO/SabreTools.IO.Transform/Enums.cs @@ -1,31 +1,5 @@ namespace SabreTools.IO.Transform { - /// - /// Determines the block size of an operation - /// - public enum BlockSize - { - /// - /// 1 byte blocks - /// - Byte = 1, - - /// - /// 2 byte blocks - /// - Word = 2, - - /// - /// 4 byte blocks - /// - Dword = 4, - - /// - /// 8 byte blocks - /// - Qword = 8, - } - /// /// Determines the swapping operation ///