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);
}

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;
}
}
22 changes: 10 additions & 12 deletions PowerKit/Extensions/ZipArchiveEntryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if NET40_OR_GREATER || NETSTANDARD || NET
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -10,8 +11,6 @@ namespace PowerKit.Extensions;

internal static class ZipArchiveEntryExtensions
{
private static readonly Encoding Utf8NoBom = new UTF8Encoding(false);

extension(ZipArchiveEntry entry)
{
/// <summary>
Expand Down Expand Up @@ -42,7 +41,7 @@ public void WriteAllBytes(byte[] bytes)
public string ReadAllText(Encoding? encoding = null)
{
using var stream = entry.Open();
using var reader = new StreamReader(stream, encoding ?? Utf8NoBom);
using var reader = new StreamReader(stream, encoding ?? Encoding.UTF8);

return reader.ReadToEnd();
}
Expand All @@ -53,7 +52,7 @@ public string ReadAllText(Encoding? encoding = null)
public void WriteAllText(string text, Encoding? encoding = null)
{
using var stream = entry.Open();
using var writer = new StreamWriter(stream, encoding ?? Utf8NoBom);
using var writer = new StreamWriter(stream, encoding ?? Encoding.Utf8WithoutBom);

writer.Write(text);
}
Expand All @@ -64,7 +63,7 @@ public void WriteAllText(string text, Encoding? encoding = null)
public string[] ReadAllLines(Encoding? encoding = null)
{
using var stream = entry.Open();
using var reader = new StreamReader(stream, encoding ?? Utf8NoBom);
using var reader = new StreamReader(stream, encoding ?? Encoding.UTF8);

var lines = new List<string>();
while (reader.ReadLine() is { } line)
Expand All @@ -81,15 +80,14 @@ public string[] ReadAllLines(Encoding? encoding = null)
public void WriteAllLines(IEnumerable<string> lines, Encoding? encoding = null)
{
using var stream = entry.Open();
using var writer = new StreamWriter(stream, encoding ?? Utf8NoBom);
using var writer = new StreamWriter(stream, encoding ?? Encoding.Utf8WithoutBom);

foreach (var line in lines)
{
writer.WriteLine(line);
}
}

#if NET40_OR_GREATER || NETSTANDARD || NET
/// <summary>
/// Reads all bytes from the zip archive entry asynchronously.
/// </summary>
Expand Down Expand Up @@ -126,7 +124,7 @@ public async Task<string> ReadAllTextAsync(
)
{
using var stream = entry.Open();
using var reader = new StreamReader(stream, encoding ?? Utf8NoBom);
using var reader = new StreamReader(stream, encoding ?? Encoding.UTF8);

return await reader.ReadToEndAsync(cancellationToken).ConfigureAwait(false);
}
Expand All @@ -141,7 +139,7 @@ public async Task WriteAllTextAsync(
)
{
using var stream = entry.Open();
using var writer = new StreamWriter(stream, encoding ?? Utf8NoBom);
using var writer = new StreamWriter(stream, encoding ?? Encoding.Utf8WithoutBom);

await writer.WriteAsync(text.AsMemory(), cancellationToken).ConfigureAwait(false);
await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
Expand All @@ -156,7 +154,7 @@ public async Task<string[]> ReadAllLinesAsync(
)
{
using var stream = entry.Open();
using var reader = new StreamReader(stream, encoding ?? Utf8NoBom);
using var reader = new StreamReader(stream, encoding ?? Encoding.UTF8);

var lines = new List<string>();
while (await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false) is { } line)
Expand All @@ -177,7 +175,7 @@ public async Task WriteAllLinesAsync(
)
{
using var stream = entry.Open();
using var writer = new StreamWriter(stream, encoding ?? Utf8NoBom);
using var writer = new StreamWriter(stream, encoding ?? Encoding.Utf8WithoutBom);

foreach (var line in lines)
{
Expand All @@ -188,6 +186,6 @@ await writer

await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
}
#endif
}
}
#endif