Skip to content

Commit f9decce

Browse files
Rework SpanByte (#178)
* Fix nullable warning The compiler was throwing a warning that it was possible for the constructer to return null in certain circumstances. This rearranges the function so that cannot happen. * Fix CodeSmell: Use NullReferenceException * Fix comments * Remove messages from exception constructors - Also add compiler defs to disable sonarcloud analysis on exceptions. * Fix param check on constructor * Fix comment close tags * More fixes in comments tags Co-authored-by: José Simões <jose.simoes@eclo.solutions>
1 parent 72b8248 commit f9decce

File tree

1 file changed

+53
-24
lines changed

1 file changed

+53
-24
lines changed

nanoFramework.CoreLibrary/System/SpanByte.cs

+53-24
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,17 @@ public SpanByte(byte[] array)
3535
/// <param name="start">The index of the first element to include in the new System.Span</param>
3636
/// <param name="length">The number of elements to include in the new System.Span</param>
3737
/// <exception cref="System.ArgumentOutOfRangeException">
38-
/// array is null, but start or length is non-zero. -or- start is outside the bounds
39-
/// of the array. -or- start and length exceeds the number of elements in the array.
38+
/// <para>
39+
/// array is null, but start or length is non-zero
40+
/// </para>
41+
/// <para>-or-</para>
42+
/// <para>
43+
/// start is outside the bounds of the array.
44+
/// </para>
45+
/// <para>-or-</para>
46+
/// <para>
47+
/// <paramref name="start"/> + <paramref name="length"/> exceed the number of elements in the array.
48+
/// </para>
4049
/// </exception>
4150
public SpanByte(byte[] array, int start, int length)
4251
{
@@ -45,36 +54,50 @@ public SpanByte(byte[] array, int start, int length)
4554
if (start < 0 ||
4655
length < 0 ||
4756
start + length > array.Length ||
48-
(start == array.Length && start > 0))
57+
start >= array.Length)
4958
{
50-
throw new ArgumentOutOfRangeException($"Array length too small");
59+
#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one
60+
throw new ArgumentOutOfRangeException();
61+
#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one
62+
}
63+
else
64+
{
65+
_array = array;
66+
_start = start;
67+
_length = length;
5168
}
5269
}
70+
else if ((start != 0) || (length != 0))
71+
{
72+
#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one
73+
throw new ArgumentOutOfRangeException();
74+
#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one
75+
}
5376
else
5477
{
55-
if ((start != 0) || (length != 0))
56-
{
57-
throw new ArgumentOutOfRangeException($"Array is null but start and length are not 0");
58-
}
78+
#pragma warning disable S3928
79+
throw new NullReferenceException();
80+
#pragma warning restore S3928
5981
}
60-
61-
_array = array;
62-
_start = start;
63-
_length = length;
6482
}
6583

6684
/// <summary>
6785
/// Gets the element at the specified zero-based index.
6886
/// </summary>
6987
/// <param name="index">The zero-based index of the element.</param>
7088
/// <returns>The element at the specified index.</returns>
89+
/// <exception cref="System.ArgumentOutOfRangeException">
90+
/// <paramref name="index"/> is out of range.
91+
/// </exception>
7192
public byte this[int index]
7293
{
7394
get
7495
{
7596
if (index >= _length)
7697
{
77-
throw new ArgumentOutOfRangeException($"Index out of range");
98+
#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one
99+
throw new ArgumentOutOfRangeException();
100+
#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one
78101
}
79102

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

89114
_array[_start + index] = value;
@@ -111,13 +136,15 @@ public byte this[int index]
111136
/// </summary>
112137
/// <param name="destination"> The destination System.Span object.</param>
113138
/// <exception cref="System.ArgumentException">
114-
/// destination is shorter than the source System.Span.
139+
/// destination is shorter than the source <see cref="SpanByte"/>.
115140
/// </exception>
116141
public void CopyTo(SpanByte destination)
117142
{
118143
if (destination.Length < _length)
119144
{
120-
throw new ArgumentException($"Destination too small");
145+
#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one
146+
throw new ArgumentException();
147+
#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one
121148
}
122149

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

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

140167
/// <summary>
141-
/// Forms a slice out of the current span starting at a specified index for a specified length.
168+
/// Forms a slice out of the current <see cref="SpanByte"/> starting at a specified index for a specified length.
142169
/// </summary>
143170
/// <param name="start">The index at which to begin this slice.</param>
144171
/// <param name="length">The desired length for the slice.</param>
145-
/// <returns>A span that consists of length elements from the current span starting at start.</returns>
146-
/// <exception cref="System.ArgumentOutOfRangeException">start or start + length is less than zero or greater than System.Span.Length.</exception>
172+
/// <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>
173+
/// <exception cref="System.ArgumentOutOfRangeException"><paramref name="start"/> or <paramref name="start"/> + <paramref name="length"/> is &lt; zero or &gt; <see cref="Length"/>.</exception>
147174
public SpanByte Slice(int start, int length)
148175
{
149176
if ((start < 0) || (length < 0) || (start + length > _length))
150177
{
151-
throw new ArgumentOutOfRangeException($"start or start + length is less than zero or greater than length");
178+
#pragma warning disable S3928 // Parameter names used into ArgumentException constructors should match an existing one
179+
throw new ArgumentOutOfRangeException();
180+
#pragma warning restore S3928 // Parameter names used into ArgumentException constructors should match an existing one
152181
}
153182

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

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

172201
/// <summary>
173-
/// Implicit conversion of an array to a span of byte
202+
/// Implicit conversion of an array to a <see cref="SpanByte"/>.
174203
/// </summary>
175204
/// <param name="array"></param>
176205
public static implicit operator SpanByte(byte[] array)

0 commit comments

Comments
 (0)