Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
LTRData committed Mar 6, 2024
1 parent 9378685 commit 6eaa82a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 64 deletions.
61 changes: 0 additions & 61 deletions Library/DiscUtils.Streams/Util/CompatExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,67 +99,6 @@ public static void WriteTo<T>(ref this T serializable, byte[] bytes, int offset)

#if !NETSTANDARD2_1_OR_GREATER && !NETCOREAPP

public static int GetCharCount(this Decoder decoder, ReadOnlySpan<byte> bytes, bool flush)
{
var buffer = ArrayPool<byte>.Shared.Rent(bytes.Length);
try
{
bytes.CopyTo(buffer);
return decoder.GetCharCount(buffer, 0, bytes.Length);
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
}

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);
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);
try
{
var charsBuffer = ArrayPool<char>.Shared.Rent(chars.Length);
try
{
chars.CopyTo(charsBuffer);
decoder.Convert(charsBuffer, 0, chars.Length, bytesBuffer, 0, bytes.Length, flush, out charsUsed, out bytesUsed, out completed);
bytesBuffer.AsSpan(0, bytesUsed).CopyTo(bytes);
}
finally
{
ArrayPool<char>.Shared.Return(charsBuffer);
}
}
finally
{
ArrayPool<byte>.Shared.Return(bytesBuffer);
}
}

public static void NextBytes(this Random random, Span<byte> buffer)
{
var bytes = new byte[buffer.Length];
Expand Down
65 changes: 62 additions & 3 deletions Library/DiscUtils.Streams/Util/EncodingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ public static int GetBytes(this Encoding encoding, ReadOnlySpan<char> chars, Spa
ArrayPool<char>.Shared.Return(str);
}
}
#endif

#if !NETSTANDARD2_1_OR_GREATER && !NETCOREAPP
public static string GetString(this Encoding encoding, ReadOnlySpan<byte> bytes)
{
var buffer = ArrayPool<byte>.Shared.Rent(bytes.Length);
Expand All @@ -51,5 +48,67 @@ public static string GetString(this Encoding encoding, ReadOnlySpan<byte> bytes)
ArrayPool<byte>.Shared.Return(buffer);
}
}

public static int GetCharCount(this Decoder decoder, ReadOnlySpan<byte> bytes, bool flush)
{
var buffer = ArrayPool<byte>.Shared.Rent(bytes.Length);
try
{
bytes.CopyTo(buffer);
return decoder.GetCharCount(buffer, 0, bytes.Length);
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
}

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);
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);
try
{
var charsBuffer = ArrayPool<char>.Shared.Rent(chars.Length);
try
{
chars.CopyTo(charsBuffer);
decoder.Convert(charsBuffer, 0, chars.Length, bytesBuffer, 0, bytes.Length, flush, out charsUsed, out bytesUsed, out completed);
bytesBuffer.AsSpan(0, bytesUsed).CopyTo(bytes);
}
finally
{
ArrayPool<char>.Shared.Return(charsBuffer);
}
}
finally
{
ArrayPool<byte>.Shared.Return(bytesBuffer);
}
}

#endif
}

0 comments on commit 6eaa82a

Please sign in to comment.