Skip to content

Commit

Permalink
More Encoding and Encoder compat extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
LTRData committed Sep 13, 2024
1 parent 608c43d commit dd5b787
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion Library/DiscUtils.Streams/Util/EncodingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static int GetCharCount(this Decoder decoder, ReadOnlySpan<byte> bytes, b
}
}

public static int GetChars(this Decoder decoder, ReadOnlySpan<byte> bytes, Span<char> chars, bool flush)
public static int GetChars(this Decoder decoder, ReadOnlySpan<byte> bytes, Span<char> chars)
{
var bytesBuffer = ArrayPool<byte>.Shared.Rent(bytes.Length);
try
Expand All @@ -88,6 +88,54 @@ public static int GetChars(this Decoder decoder, ReadOnlySpan<byte> bytes, Span<
}
}

public static int GetChars(this Decoder decoder, ReadOnlySpan<byte> bytes, Span<char> chars, bool flush)
{
var bytesBuffer = ArrayPool<byte>.Shared.Rent(bytes.Length);
try
{
var charsBuffer = ArrayPool<char>.Shared.Rent(chars.Length);
try
{
bytes.CopyTo(bytesBuffer);
var i = decoder.GetChars(bytesBuffer, 0, bytes.Length, charsBuffer, 0, flush);
charsBuffer.AsSpan(0, i).CopyTo(chars);
return i;
}
finally
{
ArrayPool<char>.Shared.Return(charsBuffer);
}
}
finally
{
ArrayPool<byte>.Shared.Return(bytesBuffer);
}
}

public static int GetChars(this Encoding encoding, ReadOnlySpan<byte> bytes, Span<char> chars)
{
var bytesBuffer = ArrayPool<byte>.Shared.Rent(bytes.Length);
try
{
var charsBuffer = ArrayPool<char>.Shared.Rent(chars.Length);
try
{
bytes.CopyTo(bytesBuffer);
var i = encoding.GetChars(bytesBuffer, 0, bytes.Length, charsBuffer, 0);
charsBuffer.AsSpan(0, i).CopyTo(chars);
return i;
}
finally
{
ArrayPool<char>.Shared.Return(charsBuffer);
}
}
finally
{
ArrayPool<byte>.Shared.Return(bytesBuffer);
}
}

public static void Convert(this Encoder decoder, ReadOnlySpan<char> chars, Span<byte> bytes, bool flush, out int charsUsed, out int bytesUsed, out bool completed)
{
var bytesBuffer = ArrayPool<byte>.Shared.Rent(bytes.Length);
Expand Down

0 comments on commit dd5b787

Please sign in to comment.