-
Notifications
You must be signed in to change notification settings - Fork 1
Add Encoding.WithoutPreamble() extension method
#28
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 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9202535
Add Encoding.WithoutPreamble() extension method
Copilot 0e54c38
Update EncodingExtensionsTests.cs
Tyrrrz 843f475
Update EncodingExtensions.cs
Tyrrrz d77e785
Update EncodingExtensions.cs
Tyrrrz 0d45d89
Update EncodingExtensions.cs
Tyrrrz 6354e16
Fix NoPreambleEncoding: clone inner for isolation, delegate encoder/d…
Copilot 71044f1
Update EncodingExtensions.cs
Tyrrrz 7c11b70
Fix net35 build errors: remove 3-arg Encoding constructor, use new by…
Copilot 23a03c0
Update EncodingExtensions.cs
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
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 |
|---|---|---|
| @@ -1,19 +1,60 @@ | ||
| using System; | ||
| using System.Text; | ||
|
|
||
| namespace PowerKit.Extensions; | ||
|
|
||
| file sealed class NoPreambleEncoding(Encoding inner) : Encoding | ||
| { | ||
| public override string BodyName => inner.BodyName; | ||
| public override string EncodingName => inner.EncodingName; | ||
| public override string HeaderName => inner.HeaderName; | ||
| public override string WebName => inner.WebName; | ||
| public override int CodePage => inner.CodePage; | ||
| public override bool IsBrowserDisplay => inner.IsBrowserDisplay; | ||
| public override bool IsBrowserSave => inner.IsBrowserSave; | ||
| public override bool IsMailNewsDisplay => inner.IsMailNewsDisplay; | ||
| public override bool IsMailNewsSave => inner.IsMailNewsSave; | ||
| public override bool IsSingleByte => inner.IsSingleByte; | ||
|
|
||
| public override byte[] GetPreamble() => []; | ||
|
Tyrrrz marked this conversation as resolved.
|
||
|
|
||
| public override int GetByteCount(char[] chars, int index, int count) => | ||
| inner.GetByteCount(chars, index, count); | ||
|
|
||
| public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) => | ||
| inner.GetBytes(chars, charIndex, charCount, bytes, byteIndex); | ||
|
|
||
| public override int GetCharCount(byte[] bytes, int index, int count) => | ||
| inner.GetCharCount(bytes, index, count); | ||
|
|
||
| public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) => | ||
| inner.GetChars(bytes, byteIndex, byteCount, chars, charIndex); | ||
|
|
||
| public override int GetMaxByteCount(int charCount) => inner.GetMaxByteCount(charCount); | ||
|
|
||
| public override int GetMaxCharCount(int byteCount) => inner.GetMaxCharCount(byteCount); | ||
| } | ||
|
|
||
| file static class EncodingEx | ||
| { | ||
| public static Encoding Utf8WithoutBom { get; } = new UTF8Encoding(false); | ||
| public static Encoding Utf8WithoutBom { get; } = Encoding.UTF8.WithoutPreamble(); | ||
| } | ||
|
|
||
| internal static class EncodingExtensions | ||
| { | ||
| extension(Encoding) | ||
| extension(Encoding encoding) | ||
| { | ||
| /// <summary> | ||
| /// Gets an instance of the UTF-8 encoding that does not emit a byte order mark (BOM). | ||
| /// </summary> | ||
| public static Encoding Utf8WithoutBom => EncodingEx.Utf8WithoutBom; | ||
|
|
||
| /// <summary> | ||
| /// Creates a derived encoding that produces an empty preamble, regardless of the original encoding's preamble. | ||
| /// </summary> | ||
| public Encoding WithoutPreamble() => | ||
| encoding.GetPreamble().Length > 0 | ||
| ? new NoPreambleEncoding(encoding) | ||
| : encoding; | ||
| } | ||
| } | ||
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.