Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions PowerKit.Tests/BinaryReaderExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System.IO;
using System.Text;
using FluentAssertions;
using PowerKit.Extensions;
using Xunit;

namespace PowerKit.Tests;

public class BinaryReaderExtensionsTests
{
[Fact]
public void SkipZeroes_Test()
{
// Arrange
var data = new byte[] { 0, 0, 0, 1, 2, 3 };
using var stream = new MemoryStream(data);
using var reader = new BinaryReader(stream);

// Act
reader.SkipZeroes();

// Assert
stream.Position.Should().Be(3);
reader.ReadByte().Should().Be(1);
}

[Fact]
public void SkipZeroes_WithMaxLength_Test()
{
// Arrange
var data = new byte[] { 0, 0, 0, 0, 1, 2, 3 };
using var stream = new MemoryStream(data);
using var reader = new BinaryReader(stream);

// Act
reader.SkipZeroes(maxSkipLength: 2);

// Assert
stream.Position.Should().Be(2);
reader.ReadByte().Should().Be(0);
}

[Fact]
public void SkipZeroes_AllZeroes_Test()
{
// Arrange
var data = new byte[] { 0, 0, 0 };
using var stream = new MemoryStream(data);
using var reader = new BinaryReader(stream);

// Act
reader.SkipZeroes();

// Assert
stream.Position.Should().Be(3);
}

[Fact]
public void ReadNullTerminatedString_Test()
{
// Arrange
var data = Encoding.ASCII.GetBytes("hello\0");
using var stream = new MemoryStream(data);
using var reader = new BinaryReader(stream, Encoding.ASCII);

// Act
var result = reader.ReadNullTerminatedString();

// Assert
result.Should().Be("hello");
}

[Fact]
public void ReadNullTerminatedString_Empty_Test()
{
// Arrange
var data = new byte[] { 0 };
using var stream = new MemoryStream(data);
using var reader = new BinaryReader(stream, Encoding.ASCII);

// Act
var result = reader.ReadNullTerminatedString();

// Assert
result.Should().Be("");
}
}
38 changes: 38 additions & 0 deletions PowerKit.Tests/BinaryWriterExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.IO;
using System.Text;
using FluentAssertions;
using PowerKit.Extensions;
using Xunit;

namespace PowerKit.Tests;

public class BinaryWriterExtensionsTests
{
[Fact]
public void WriteNullTerminatedString_Test()
{
// Arrange
using var stream = new MemoryStream();
using var writer = new BinaryWriter(stream, Encoding.ASCII);

// Act
writer.WriteNullTerminatedString("hello");

// Assert
stream.ToArray().Should().Equal("hello\0"u8.ToArray());
}

[Fact]
public void WriteNullTerminatedString_Empty_Test()
{
// Arrange
using var stream = new MemoryStream();
using var writer = new BinaryWriter(stream, Encoding.ASCII);

// Act
writer.WriteNullTerminatedString("");

// Assert
stream.ToArray().Should().Equal(0);
}
}
49 changes: 49 additions & 0 deletions PowerKit/Extensions/BinaryReaderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.IO;
using System.Text;

namespace PowerKit.Extensions;

internal static class BinaryReaderExtensions
{
extension(BinaryReader reader)
{
/// <summary>
/// Skips zero bytes in the stream, up to the specified maximum length.
/// </summary>
public void SkipZeroes(long? maxSkipLength = null)
{
var skipped = 0L;
while (maxSkipLength is null || skipped < maxSkipLength)
{
if (reader.PeekChar() != 0)
{
break;
}

reader.ReadByte();
skipped++;
}
}

/// <summary>
/// Reads a null-terminated string from the binary reader.
/// </summary>
public string ReadNullTerminatedString()
{
var buffer = new StringBuilder();

while (true)
{
var ch = reader.ReadChar();
if (ch == '\0')
{
break;
}

buffer.Append(ch);
}

return buffer.ToString();
}
}
}
22 changes: 22 additions & 0 deletions PowerKit/Extensions/BinaryWriterExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.IO;

namespace PowerKit.Extensions;

internal static class BinaryWriterExtensions
{
extension(BinaryWriter writer)
{
/// <summary>
/// Writes a null-terminated string to the binary writer.
/// </summary>
public void WriteNullTerminatedString(string value)
{
foreach (var ch in value)
{
writer.Write(ch);
}

writer.Write('\0');
}
Comment thread
Tyrrrz marked this conversation as resolved.
}
}