Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework SpanByte #178

Merged
merged 8 commits into from
Mar 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 53 additions & 24 deletions nanoFramework.CoreLibrary/System/SpanByte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,17 @@ public SpanByte(byte[] array)
/// <param name="start">The index of the first element to include in the new System.Span</param>
/// <param name="length">The number of elements to include in the new System.Span</param>
/// <exception cref="System.ArgumentOutOfRangeException">
/// array is null, but start or length is non-zero. -or- start is outside the bounds
/// of the array. -or- start and length exceeds the number of elements in the array.
/// <para>
/// array is null, but start or length is non-zero
/// </para>
/// <para>-or-</para>
/// <para>
/// start is outside the bounds of the array.
/// </para>
/// <para>-or-</para>
/// <para>
/// <paramref name="start"/> + <paramref name="length"/> exceed the number of elements in the array.
/// </para>
/// </exception>
public SpanByte(byte[] array, int start, int length)
{
Expand All @@ -45,36 +54,50 @@ public SpanByte(byte[] array, int start, int length)
if (start < 0 ||
length < 0 ||
start + length > array.Length ||
(start == array.Length && start > 0))
start >= array.Length)
{
throw new ArgumentOutOfRangeException($"Array length too small");
#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one
throw new ArgumentOutOfRangeException();
#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one
}
else
{
_array = array;
_start = start;
_length = length;
}
}
else if ((start != 0) || (length != 0))
{
#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one
throw new ArgumentOutOfRangeException();
#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one
}
else
{
if ((start != 0) || (length != 0))
{
throw new ArgumentOutOfRangeException($"Array is null but start and length are not 0");
}
#pragma warning disable S3928
throw new NullReferenceException();
#pragma warning restore S3928
}

_array = array;
_start = start;
_length = length;
}

/// <summary>
/// Gets the element at the specified zero-based index.
/// </summary>
/// <param name="index">The zero-based index of the element.</param>
/// <returns>The element at the specified index.</returns>
/// <exception cref="System.ArgumentOutOfRangeException">
/// <paramref name="index"/> is out of range.
/// </exception>
public byte this[int index]
{
get
{
if (index >= _length)
{
throw new ArgumentOutOfRangeException($"Index out of range");
#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one
throw new ArgumentOutOfRangeException();
#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one
}

return _array[_start + index];
Expand All @@ -83,7 +106,9 @@ public byte this[int index]
{
if (index >= _length)
{
throw new ArgumentOutOfRangeException($"Index out of range");
#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one
throw new ArgumentOutOfRangeException();
#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one
}

_array[_start + index] = value;
Expand Down Expand Up @@ -111,13 +136,15 @@ public byte this[int index]
/// </summary>
/// <param name="destination"> The destination System.Span object.</param>
/// <exception cref="System.ArgumentException">
/// destination is shorter than the source System.Span.
/// destination is shorter than the source <see cref="SpanByte"/>.
/// </exception>
public void CopyTo(SpanByte destination)
{
if (destination.Length < _length)
{
throw new ArgumentException($"Destination too small");
#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one
throw new ArgumentException();
#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one
}

for (int i = 0; i < _length; i++)
Expand All @@ -127,35 +154,37 @@ public void CopyTo(SpanByte destination)
}

/// <summary>
/// Forms a slice out of the current span that begins at a specified index.
/// Forms a slice out of the current <see cref="SpanByte"/> that begins at a specified index.
/// </summary>
/// <param name="start">The index at which to begin the slice.</param>
/// <returns>A span that consists of all elements of the current span from start to the end of the span.</returns>
/// <exception cref="System.ArgumentOutOfRangeException">start is less than zero or greater than System.Span.Length.</exception>
/// <exception cref="System.ArgumentOutOfRangeException"><paramref name="start"/> is &lt; zero or &gt; <see cref="Length"/>.</exception>
public SpanByte Slice(int start)
{
return Slice(start, _length - start);
}

/// <summary>
/// Forms a slice out of the current span starting at a specified index for a specified length.
/// Forms a slice out of the current <see cref="SpanByte"/> starting at a specified index for a specified length.
/// </summary>
/// <param name="start">The index at which to begin this slice.</param>
/// <param name="length">The desired length for the slice.</param>
/// <returns>A span that consists of length elements from the current span starting at start.</returns>
/// <exception cref="System.ArgumentOutOfRangeException">start or start + length is less than zero or greater than System.Span.Length.</exception>
/// <returns>A <see cref="SpanByte"/> that consists of <paramref name="length"/> number of elements from the current <see cref="SpanByte"/> starting at <paramref name="start"/>.</returns>
/// <exception cref="System.ArgumentOutOfRangeException"><paramref name="start"/> or <paramref name="start"/> + <paramref name="length"/> is &lt; zero or &gt; <see cref="Length"/>.</exception>
public SpanByte Slice(int start, int length)
{
if ((start < 0) || (length < 0) || (start + length > _length))
{
throw new ArgumentOutOfRangeException($"start or start + length is less than zero or greater than length");
#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one
throw new ArgumentOutOfRangeException();
#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one
}

return new SpanByte(_array, _start + start, length);
}

/// <summary>
/// Copies the contents of this span into a new array.
/// Copies the contents of this <see cref="SpanByte"/> into a new array.
/// </summary>
/// <returns> An array containing the data in the current span.</returns>
public byte[] ToArray()
Expand All @@ -170,7 +199,7 @@ public byte[] ToArray()
}

/// <summary>
/// Implicit conversion of an array to a span of byte
/// Implicit conversion of an array to a <see cref="SpanByte"/>.
/// </summary>
/// <param name="array"></param>
public static implicit operator SpanByte(byte[] array)
Expand Down