Skip to content
Merged
22 changes: 22 additions & 0 deletions PowerKit.Tests/EncodingExtensionsTests.cs
Comment thread
Tyrrrz marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Text;
using FluentAssertions;
using PowerKit.Extensions;
using Xunit;

namespace PowerKit.Tests;

public class EncodingExtensionsTests
{
[Fact]
public void Utf8WithoutBom_Test()
{
// Arrange
var text = "hello, world! 🌍";

// Act
var bytes = Encoding.Utf8WithoutBom.GetBytes(text);

// Assert
Encoding.UTF8.GetString(bytes).Should().Be(text);
}
}
19 changes: 19 additions & 0 deletions PowerKit/Extensions/EncodingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Text;

namespace PowerKit.Extensions;

file static class EncodingEx
{
public static Encoding Utf8WithoutBom { get; } = new UTF8Encoding(false);
Comment thread
Tyrrrz marked this conversation as resolved.
}

internal static class EncodingExtensions
{
extension(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;
}
}