/// <param name="count">The number of bytes to decode.</param>
/// <param name="dst">The destination byte array to store the decoded data.</param>
/// <returns>The number of decoded symbols.</returns>
- public static int Decode(byte[] src, int offset, int count, byte[] dst)
+ public static int Decode(ReadOnlySpan<byte> src, Span<byte> dst)
{
- int i = offset;
+ int i = 0;
int j = 0;
int lastDecodedBits = 0;
- while (i < count)
+ while (i < src.Length)
{
// Note that if lastDecodeBits is 3 or more, then we will only get 5 bits (or less)
// from src[i]. Thus we need to read 5 bytes here to ensure that we always have
next |= (i + 4 < src.Length ? (uint)(src[i + 4] >> (8 - lastDecodedBits)) : 0);
uint ones = (uint)(int.MinValue >> (8 - lastDecodedBits - 1));
- if (i == count - 1 && lastDecodedBits > 0 && (next & ones) == ones)
+ if (i == src.Length - 1 && lastDecodedBits > 0 && (next & ones) == ones)
{
// The remaining 7 or less bits are all 1, which is padding.
// We specifically check that lastDecodedBits > 0 because padding
// The longest possible symbol size is 30 bits. If we're at the last 4 bytes
// of the input, we need to make sure we pass the correct number of valid bits
// left, otherwise the trailing 0s in next may form a valid symbol.
- int validBits = Math.Min(30, (8 - lastDecodedBits) + (count - i - 1) * 8);
+ int validBits = Math.Min(30, (8 - lastDecodedBits) + (src.Length - i - 1) * 8);
int ch = DecodeValue(next, validBits, out int decodedBits);
if (ch == -1)
/// </param>
/// <param name="decodedBits">The number of bits decoded from <paramref name="data"/>.</param>
/// <returns>The decoded symbol.</returns>
- public static int DecodeValue(uint data, int validBits, out int decodedBits)
+ private static int DecodeValue(uint data, int validBits, out int decodedBits)
{
// The code below implements the decoding logic for a canonical Huffman code.
//