-
Notifications
You must be signed in to change notification settings - Fork 1
Add BinaryReaderExtensions and BinaryWriterExtensions #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
59d9cae
Add BinaryReaderExtensions with SkipZeroes and ReadNullTerminatedString
Copilot 51b5f4d
Add BinaryWriterExtensions with WriteNullTerminatedString
Copilot 7043ef0
Merge branch 'prime' into copilot/add-binary-reader-extensions
Tyrrrz e314ae7
Update PowerKit/Extensions/BinaryReaderExtensions.cs
Tyrrrz 7983663
Guard SkipZeroes with CanSeek check and cap end position to stream le…
Copilot e8a4e4d
Fix WriteNullTerminatedString to write encoded bytes with single null…
Copilot 7cab462
Refactor SkipZeroes to use PeekChar instead of seeking
Copilot f45cd92
Use u8 string literal in WriteNullTerminatedString test
Copilot 167c9a7
Revert WriteNullTerminatedString to use Write(char)
Copilot 005dfd1
Merge branch 'prime' into copilot/add-binary-reader-extensions
Tyrrrz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(""); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.