Skip to content

Add Encoding.WithoutPreamble() extension method#28

Merged
Tyrrrz merged 9 commits intoprimefrom
copilot/add-without-preamble-extension
Apr 14, 2026
Merged

Add Encoding.WithoutPreamble() extension method#28
Tyrrrz merged 9 commits intoprimefrom
copilot/add-without-preamble-extension

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 14, 2026

Adds a general-purpose Encoding.WithoutPreamble() extension that produces a derived encoding with an empty preamble, applicable to any Encoding — not just UTF-8.

Changes

  • NoPreambleEncoding (file-local) — sealed wrapper that delegates all abstract encode/decode methods and identity properties (CodePage, EncodingName, WebName, etc.) to the inner encoding, overriding only GetPreamble() to return Array.Empty<byte>()
  • WithoutPreamble() extension — returns this if the encoding already has no preamble; otherwise wraps in NoPreambleEncoding
  • Utf8WithoutBom updated — now implemented via Encoding.UTF8.WithoutPreamble() instead of new UTF8Encoding(false)

Usage

// Any encoding — strip the preamble
var encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: true);
var noBom = encoding.WithoutPreamble();
noBom.GetPreamble(); // []

// Already preamble-free — same instance returned
var ascii = Encoding.ASCII.WithoutPreamble(); // === Encoding.ASCII

Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/8c7ef6da-8614-4f0c-9c03-def636b66214

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
@Tyrrrz Tyrrrz added the enhancement New feature or request label Apr 14, 2026
@Tyrrrz Tyrrrz marked this pull request as ready for review April 14, 2026 19:30
Copilot AI review requested due to automatic review settings April 14, 2026 19:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a general-purpose way to strip BOM/preamble from any Encoding by introducing an Encoding.WithoutPreamble() extension and refactoring Encoding.Utf8WithoutBom to use it.

Changes:

  • Added a file-local NoPreambleEncoding wrapper that overrides GetPreamble() to return an empty preamble while delegating encoding/decoding operations to an inner Encoding.
  • Added Encoding.WithoutPreamble() extension that returns the original encoding when it already has no preamble, otherwise wraps it.
  • Updated Utf8WithoutBom to use Encoding.UTF8.WithoutPreamble() and added tests for the new behavior.

Reviewed changes

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

File Description
PowerKit/Extensions/EncodingExtensions.cs Implements NoPreambleEncoding, adds WithoutPreamble(), and updates Utf8WithoutBom to use the new extension.
PowerKit.Tests/EncodingExtensionsTests.cs Adds unit tests covering WithoutPreamble() behavior and instance-return semantics.

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

Comment thread PowerKit/Extensions/EncodingExtensions.cs
Comment thread PowerKit/Extensions/EncodingExtensions.cs Outdated
…ecoder, fix net35 compatibility

Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/c5fdc587-3fa8-4564-a9cd-54d151626c61

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Copilot AI requested a review from Tyrrrz April 14, 2026 19:43
@Tyrrrz
Copy link
Copy Markdown
Owner

Tyrrrz commented Apr 14, 2026

@copilot build errors

…te[0] for GetPreamble

Agent-Logs-Url: https://github.com/Tyrrrz/PowerKit/sessions/92664b43-fcf9-43aa-985b-07da7fd12b5b

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 14, 2026

@copilot build errors

Fixed in 7c11b70. Two net35 build errors:

  1. Encoding(int, EncoderFallback, DecoderFallback) — this 3-arg constructor was introduced in .NET 4.0 and doesn't exist in net35. Switched back to the default parameterless constructor; the cloned _inner already carries the source encoding's fallbacks so all encode/decode operations are correct.
  2. [] (collection expression) — not available in older C# language versions targeting net35. Kept as new byte[0].

@Tyrrrz Tyrrrz merged commit 356bb22 into prime Apr 14, 2026
7 checks passed
@Tyrrrz Tyrrrz deleted the copilot/add-without-preamble-extension branch April 14, 2026 19:55
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