Skip to content

Commit

Permalink
Use System.Memory
Browse files Browse the repository at this point in the history
  • Loading branch information
seionmoya committed Sep 25, 2024
1 parent 0cf5a82 commit 094cc46
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 26 deletions.
4 changes: 3 additions & 1 deletion Zlib.Managed/Elskom/Adler32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace Elskom.Generic.Libs
{
using System;

internal static class Adler32
{
// largest prime smaller than 65536
Expand All @@ -13,7 +15,7 @@ internal static class Adler32
// NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
private const int NMAX = 5552;

internal static long Calculate(long adler, byte[] buf, int index, int len)
internal static long Calculate(long adler, ReadOnlySpan<byte> buf, int index, int len)
{
if (buf == null)
{
Expand Down
9 changes: 7 additions & 2 deletions Zlib.Managed/Elskom/Deflate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1625,7 +1625,8 @@ internal int DeflateParams(ZStream strm, int level, int strategy)
return err;
}

internal int DeflateSetDictionary(ZStream strm, byte[] dictionary, int dictLength)
// TODO: Check if ReadOnlySpan<byte> dictionary is okay
internal int DeflateSetDictionary(ZStream strm, ReadOnlySpan<byte> dictionary, int dictLength)
{
var length = dictLength;
var index = 0;
Expand All @@ -1648,7 +1649,11 @@ internal int DeflateSetDictionary(ZStream strm, byte[] dictionary, int dictLengt
index = dictLength - length; // use the tail of the dictionary
}

Array.Copy(dictionary, index, this.Window, 0, length);
// Array.Copy(dictionary.ToArray(), index, this.Window, 0, length);
var source = dictionary.Slice(index, length);
var target = new Span<byte>(this.Window, 0, length);
source.CopyTo(target);

this.Strstart = length;
this.BlockStart = length;

Expand Down
9 changes: 7 additions & 2 deletions Zlib.Managed/Elskom/InfBlocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -689,9 +689,14 @@ internal void Free(ZStream z)
// ZFREE(z, s);
}

internal void Set_dictionary(byte[] d, int start, int n)
// TODO: Check if ReadOnlySpan<byte> d is okay
internal void Set_dictionary(ReadOnlySpan<byte> d, int start, int n)
{
Array.Copy(d, start, this.Window, 0, n);
//Array.Copy(d, start, this.Window, 0, n);
var source = d.Slice(start);
var target = new Span<byte>(this.Window, 0, n);
source.CopyTo(target);

this.Read = this.Write = n;
}

Expand Down
9 changes: 7 additions & 2 deletions Zlib.Managed/Elskom/Inflate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace Elskom.Generic.Libs
{
using System;

/// <summary>
/// Class for decompressing data through zlib.
/// </summary>
Expand Down Expand Up @@ -48,7 +50,10 @@ internal sealed class Inflate
private const int DONE = 12; // finished check, done
private const int BAD = 13; // got an error--stay here

private static readonly byte[] Mark = new byte[] { 0, 0, (byte)SupportClass.Identity(0xff), (byte)SupportClass.Identity(0xff) };
private static ReadOnlySpan<byte> Mark => new byte[]
{
0, 0, (byte)SupportClass.Identity(0xff), (byte)SupportClass.Identity(0xff)
};

internal int Mode { get; private set; } // current inflate mode

Expand Down Expand Up @@ -335,7 +340,7 @@ internal static int Decompress(ZStream z, int f)
}
}

internal static int InflateSetDictionary(ZStream z, byte[] dictionary, int dictLength)
internal static int InflateSetDictionary(ZStream z, Span<byte> dictionary, int dictLength)
{
var index = 0;
var length = dictLength;
Expand Down
19 changes: 2 additions & 17 deletions Zlib.Managed/Elskom/SupportClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace Elskom.Generic.Libs
{
using System;
using System.IO;
using System.Text;

/// <summary>
/// Class to support zlib stuff.
Expand Down Expand Up @@ -85,7 +84,7 @@ internal static class SupportClass
/// <param name="start">The starting index of the target array.</param>
/// <param name="count">The maximum number of characters to read from the source Stream.</param>
/// <returns>The number of characters read. The number will be less than or equal to count depending on the data available in the source Stream. Returns -1 if the end of the stream is reached.</returns>
internal static int ReadInput(Stream sourceStream, byte[] target, int start, int count)
internal static int ReadInput(Stream sourceStream, Span<byte> target, int start, int count)
{
if (sourceStream == null)
{
Expand Down Expand Up @@ -127,7 +126,7 @@ internal static int ReadInput(Stream sourceStream, byte[] target, int start, int
/// <param name="start">The starting index of the target array.</param>
/// <param name="count">The maximum number of characters to read from the source TextReader.</param>
/// <returns>The number of characters read. The number will be less than or equal to count depending on the data available in the source TextReader. Returns -1 if the end of the stream is reached.</returns>
internal static int ReadInput(TextReader sourceTextReader, byte[] target, int start, int count)
internal static int ReadInput(TextReader sourceTextReader, Span<byte> target, int start, int count)
{
if (sourceTextReader == null)
{
Expand Down Expand Up @@ -161,19 +160,5 @@ internal static int ReadInput(TextReader sourceTextReader, byte[] target, int st

return bytesRead;
}

/// <summary>
/// Converts a string to an array of bytes.
/// </summary>
/// <param name="sourceString">The string to be converted.</param>
/// <returns>The new array of bytes.</returns>
internal static byte[] ToByteArray(string sourceString) => Encoding.UTF8.GetBytes(sourceString);

/// <summary>
/// Converts an array of bytes to an array of chars.
/// </summary>
/// <param name="byteArray">The array of bytes to convert.</param>
/// <returns>The new array of chars.</returns>
internal static char[] ToCharArray(byte[] byteArray) => Encoding.UTF8.GetChars(byteArray);
}
}
4 changes: 2 additions & 2 deletions Zlib.Managed/Elskom/ZStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ internal int InflateEnd()
/// <param name="dictionary">The dictionary to use.</param>
/// <param name="dictLength">The dictionary length.</param>
/// <returns>The zlib status state.</returns>
internal int InflateSetDictionary(byte[] dictionary, int dictLength) => this.Istate == null ? ZSTREAMERROR : Libs.Inflate.InflateSetDictionary(this, dictionary, dictLength);
internal int InflateSetDictionary(Span<byte> dictionary, int dictLength) => this.Istate == null ? ZSTREAMERROR : Libs.Inflate.InflateSetDictionary(this, dictionary, dictLength);

/// <summary>
/// Initializes compression.
Expand Down Expand Up @@ -207,7 +207,7 @@ internal int DeflateEnd()
/// <param name="dictionary">The dictionary to use.</param>
/// <param name="dictLength">The dictionary length.</param>
/// <returns>The zlib status state.</returns>
internal int DeflateSetDictionary(byte[] dictionary, int dictLength) => this.Dstate == null ? ZSTREAMERROR : this.Dstate.DeflateSetDictionary(this, dictionary, dictLength);
internal int DeflateSetDictionary(ReadOnlySpan<byte> dictionary, int dictLength) => this.Dstate == null ? ZSTREAMERROR : this.Dstate.DeflateSetDictionary(this, dictionary.ToArray(), dictLength);

/// <summary>
/// Frees everything.
Expand Down

0 comments on commit 094cc46

Please sign in to comment.