-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Konstantin Gukov
committed
May 21, 2021
1 parent
e640577
commit eb48cf4
Showing
460 changed files
with
20,083 additions
and
1,111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
namespace System | ||
{ | ||
/// <summary> | ||
/// Provides a type- and memory-safe representation of a contiguous region of arbitrary array. | ||
/// </summary> | ||
[Serializable, CLSCompliant(false)] | ||
public readonly ref struct SpanByte | ||
{ | ||
private readonly byte[] _array; | ||
private readonly int _start; | ||
private readonly int _length; | ||
|
||
/// <summary> | ||
/// Creates a new Span object over the entirety of a specified array. | ||
/// </summary> | ||
/// <param name="array">The array from which to create the System.Span object.</param> | ||
public SpanByte(byte[] array) | ||
{ | ||
_array = array; | ||
_length = array?.Length ?? 0; | ||
_start = 0; | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new Span object that includes a specified number of elements | ||
/// of an array starting at a specified index. | ||
/// </summary> | ||
/// <param name="array">The source array.</param> | ||
/// <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. | ||
/// </exception> | ||
public SpanByte(byte[] array, int start, int length) | ||
{ | ||
if (array != null) | ||
{ | ||
if ((start + length > array.Length) || (start >= array.Length)) | ||
{ | ||
// Array length too small | ||
throw new ArgumentOutOfRangeException(); | ||
} | ||
} | ||
else | ||
{ | ||
if ((start != 0) || (length != 0)) | ||
{ | ||
// Array is null but start and length are not 0 | ||
throw new ArgumentOutOfRangeException(); | ||
} | ||
} | ||
|
||
_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> | ||
// public ref byte this[int index] => ref _array[_start + index]; // <= this is not working and raises exception after few access | ||
public byte this[int index] | ||
{ | ||
get | ||
{ | ||
int realIndex = _start + index; | ||
|
||
if (realIndex >= _length) | ||
{ | ||
throw new ArgumentOutOfRangeException(); | ||
} | ||
|
||
return _array[realIndex]; | ||
} | ||
|
||
set | ||
{ | ||
int realIndex = _start + index; | ||
|
||
if (realIndex >= _length) | ||
{ | ||
throw new ArgumentOutOfRangeException(); | ||
} | ||
|
||
_array[realIndex] = value; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Returns an empty System.Span object. | ||
/// </summary> | ||
public static SpanByte Empty => new SpanByte(); | ||
|
||
/// <summary> | ||
/// Returns the length of the current span. | ||
/// </summary> | ||
public int Length => _length; | ||
|
||
/// <summary> | ||
/// Returns a value that indicates whether the current System.Span is empty. | ||
/// true if the current span is empty; otherwise, false. | ||
/// </summary> | ||
public bool IsEmpty => _length == 0; | ||
|
||
/// <summary> | ||
/// Copies the contents of this System.Span into a destination System.Span. | ||
/// </summary> | ||
/// <param name="destination"> The destination System.Span object.</param> | ||
/// <exception cref="System.ArgumentException"> | ||
/// destination is shorter than the source System.Span. | ||
/// </exception> | ||
public void CopyTo(SpanByte destination) | ||
{ | ||
if (destination.Length < _length) | ||
{ | ||
throw new ArgumentException(); | ||
} | ||
|
||
for (int i = 0; i < _length; i++) | ||
{ | ||
destination[i] = _array[_start + i]; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Forms a slice out of the current span 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> | ||
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. | ||
/// </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> | ||
public SpanByte Slice(int start, int length) | ||
{ | ||
if ((start < 0) || (length < 0) || (start + length > _length)) | ||
{ | ||
// start or start + length is less than zero or greater than length | ||
throw new ArgumentOutOfRangeException(); | ||
} | ||
|
||
return new SpanByte(_array, _start + start, length); | ||
} | ||
|
||
/// <summary> | ||
/// Copies the contents of this span into a new array. | ||
/// </summary> | ||
/// <returns> An array containing the data in the current span.</returns> | ||
public byte[] ToArray() | ||
{ | ||
var array = new byte[_length]; | ||
for (int i = 0; i < _length; i++) | ||
{ | ||
array[i] = _array[_start + i]; | ||
} | ||
|
||
return array; | ||
} | ||
|
||
public static implicit operator SpanByte(byte[] array) | ||
{ | ||
return new(array); | ||
} | ||
|
||
/// <summary> | ||
/// Defines an implicit conversion of a System.Span to a System.ReadOnlySpan. | ||
/// </summary> | ||
/// <param name="span">The object to convert to a System.ReadOnlySpan.</param> | ||
public static implicit operator ReadOnlySpanByte(SpanByte span) | ||
{ | ||
return span; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.