Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
44 changes: 44 additions & 0 deletions PowerKit.Tests/EncodingExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,48 @@ public void Utf8WithoutBom_Test()
// Assert
Encoding.UTF8.GetString(bytes).Should().Be(text);
}

[Fact]
public void WithoutPreamble_Test()
{
// Arrange
var encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: true);
encoding.GetPreamble().Should().NotBeEmpty();

// Act
var result = encoding.WithoutPreamble();

// Assert
result.GetPreamble().Should().BeEmpty();
result.GetString(result.GetBytes("hello")).Should().Be("hello");
}

[Fact]
public void WithoutPreamble_WithoutPreamble_Test()
{
// Arrange
var encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
encoding.GetPreamble().Should().BeEmpty();

// Act
var result = encoding.WithoutPreamble();

// Assert
result.Should().BeSameAs(encoding);
}

[Fact]
public void WithoutPreamble_RoundTrip_Test()
{
// Arrange
var text = "hello, world! 🌍";
var encoding = new UTF8Encoding(true).WithoutPreamble();

// Act
var bytes = encoding.GetBytes(text);
var decoded = encoding.GetString(bytes);

// Assert
decoded.Should().Be(text);
}
}
45 changes: 43 additions & 2 deletions PowerKit/Extensions/EncodingExtensions.cs
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;
Comment thread
Tyrrrz marked this conversation as resolved.
Outdated
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() => [];
Comment thread
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;
}
}