Skip to content

Add BinaryReaderExtensions and BinaryWriterExtensions - #7

Merged
Tyrrrz merged 10 commits into
primefrom
copilot/add-binary-reader-extensions
Apr 12, 2026
Merged

Tyrrrz merged 10 commits into
primefrom
copilot/add-binary-reader-extensions

Conversation

Copilot AI commented Apr 12, 2026

Copy link
Copy Markdown
Contributor

Ports BinaryReader and BinaryWriter extension methods from Tyrrrz/Ressy.

New extensions

BinaryReaderExtensions

  • SkipZeroes(long? maxSkipLength = null) — advances past zero bytes, optionally bounded
  • ReadNullTerminatedString() — reads chars until \0, returns the accumulated string

BinaryWriterExtensions

  • WriteNullTerminatedString(string value) — writes each char then a null terminator
using var reader = new BinaryReader(stream);
reader.SkipZeroes();
var name = reader.ReadNullTerminatedString();

using var writer = new BinaryWriter(stream);
writer.WriteNullTerminatedString("hello");

@Tyrrrz Tyrrrz added the enhancement New feature or request label Apr 12, 2026
@Tyrrrz
Tyrrrz marked this pull request as ready for review April 12, 2026 12:51
Copilot AI review requested due to automatic review settings April 12, 2026 12:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Ports null-terminated string / padding-related BinaryReader and BinaryWriter extensions (and tests) into PowerKit.

Changes:

  • Added BinaryReaderExtensions.SkipZeroes(long? maxSkipLength = null) and ReadNullTerminatedString().
  • Added BinaryWriterExtensions.WriteNullTerminatedString(string value).
  • Added unit tests for the new extensions.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
PowerKit/Extensions/BinaryWriterExtensions.cs Adds null-terminated string writing extension for BinaryWriter.
PowerKit/Extensions/BinaryReaderExtensions.cs Adds zero-padding skipping and null-terminated string reading extensions for BinaryReader.
PowerKit.Tests/BinaryWriterExtensionsTests.cs Verifies null-terminated string writing behavior.
PowerKit.Tests/BinaryReaderExtensionsTests.cs Verifies zero skipping and null-terminated string reading behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
/// </summary>
public void SkipZeroes(long? maxSkipLength = null)
{
if (!reader.BaseStream.CanSeek)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use Peek to avoid relying on seeking? @copilot

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 7cab462. SkipZeroes now uses PeekChar() to look ahead before consuming a byte — no seeking at all. The CanSeek guard, BaseStream.Length, and Seek(-1, Current) are all gone. The bounded variant tracks a skipped counter instead of comparing stream positions. The NonSeekableStream test and helper class were also removed since they're no longer relevant.

Copilot AI requested a review from Tyrrrz April 12, 2026 13:26
writer.WriteNullTerminatedString("hello");

// Assert
stream.ToArray().Should().Equal((byte)'h', (byte)'e', (byte)'l', (byte)'l', (byte)'o', 0);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use an u8 string here? @copilot

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in f45cd92. The assertion now uses "hello\0"u8.ToArray().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants