Skip to content

Commit 8b72fcd

Browse files
committed
Merge branch 'add-span' into generics-wip
2 parents ebc9eac + d457663 commit 8b72fcd

File tree

9 files changed

+564
-30
lines changed

9 files changed

+564
-30
lines changed

Tests/NFUnitTestTypes/UnitTestsSpanByte.cs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,67 +13,67 @@ public class UnitTestsSpanByte
1313
public void EmptySpanTests()
1414
{
1515
// Empty span
16-
SpanByte span = SpanByte.Empty;
16+
Span span = Span.Empty;
1717
// Create a destination span larger
18-
SpanByte destination = new byte[1];
18+
Span destination = new byte[1];
1919
span.CopyTo(destination);
2020

2121
// Now also empty
22-
destination = SpanByte.Empty;
22+
destination = Span.Empty;
2323
span.CopyTo(destination);
2424
}
2525

2626
[TestMethod]
2727
public void RaisingExceptionsOfAllKindsTests()
2828
{
2929
// Should raise an exception on creation
30-
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { SpanByte span = new SpanByte(null, 1, 2); }, "ArgumentOutOfRangeException when array is null, start is 1 and length is 2");
31-
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { SpanByte span = new SpanByte(new byte[1], 1, 2); }, "ArgumentOutOfRangeException when array is new byte[1], start is 1 and length is 2");
32-
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { SpanByte span = new SpanByte(new byte[1], 0, 2); }, "ArgumentOutOfRangeException when array is new byte[1], start is 0 and length is 2");
33-
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { SpanByte span = new SpanByte(new byte[1], 2, 0); }, "ArgumentOutOfRangeException when array is new byte[1], start is 2 and length is 0");
30+
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { Span span = new Span(null, 1, 2); }, "ArgumentOutOfRangeException when array is null, start is 1 and length is 2");
31+
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { Span span = new Span(new byte[1], 1, 2); }, "ArgumentOutOfRangeException when array is new byte[1], start is 1 and length is 2");
32+
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { Span span = new Span(new byte[1], 0, 2); }, "ArgumentOutOfRangeException when array is new byte[1], start is 0 and length is 2");
33+
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => { Span span = new Span(new byte[1], 2, 0); }, "ArgumentOutOfRangeException when array is new byte[1], start is 2 and length is 0");
3434

3535
// Exception on index access
3636
byte[] array = new byte[16] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
3737
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () =>
3838
{
39-
SpanByte span = new SpanByte(array);
39+
Span span = new Span(array);
4040
var data = span[span.Length];
4141
});
4242
Assert.ThrowsException(typeof(IndexOutOfRangeException), () =>
4343
{
44-
SpanByte span = new SpanByte(array);
44+
Span span = new Span(array);
4545
var data = span[-1];
4646
});
4747

4848
// Copy to with too small destination
4949
Assert.ThrowsException(typeof(ArgumentException), () =>
5050
{
51-
SpanByte span = new SpanByte(array);
52-
SpanByte destination = new byte[span.Length - 1];
51+
Span span = new Span(array);
52+
Span destination = new byte[span.Length - 1];
5353
span.CopyTo(destination);
5454
});
5555

5656
// Slicing arguments
5757
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () =>
5858
{
59-
SpanByte span = new SpanByte(array);
59+
Span span = new Span(array);
6060
var sliced = span.Slice(span.Length + 1);
6161
});
6262
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () =>
6363
{
64-
SpanByte span = new SpanByte(array);
64+
Span span = new Span(array);
6565
var sliced = span.Slice(1, span.Length);
6666
});
6767

6868
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () =>
6969
{
70-
SpanByte span = new SpanByte(array);
70+
Span span = new Span(array);
7171
var sliced = span.Slice(-1, span.Length);
7272
});
7373

7474
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () =>
7575
{
76-
SpanByte span = new SpanByte(array);
76+
Span span = new Span(array);
7777
var sliced = span.Slice(1, -1);
7878
});
7979

@@ -84,7 +84,7 @@ public void ToArrayTest()
8484
{
8585
byte[] array = new byte[16] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
8686

87-
SpanByte span = new(array);
87+
Span span = new(array);
8888

8989
byte[] toArray = span.ToArray();
9090

@@ -95,28 +95,28 @@ public void ToArrayTest()
9595
public void ConstructorsOfAllKindsTests()
9696
{
9797
// Empty span
98-
SpanByte span = new SpanByte();
98+
Span span = new Span();
9999
Assert.AreEqual(span.Length, 0, "Empty SpanByte should have length of 0");
100100
Assert.IsTrue(span.IsEmpty, "Empty SpanByte should be IsEmpty");
101101

102102
// Empty span
103-
span = new SpanByte(null, 0, 0);
103+
span = new Span(null, 0, 0);
104104
Assert.AreEqual(span.Length, 0, "Empty SpanByte should have length of 0");
105105
Assert.IsTrue(span.IsEmpty, "Empty SpanByte should be IsEmpty");
106106

107107
// Empty span
108-
span = SpanByte.Empty;
108+
span = Span.Empty;
109109
Assert.AreEqual(span.Length, 0, "Empty SpanByte should have length of 0");
110110
Assert.IsTrue(span.IsEmpty, "Empty SpanByte should be IsEmpty");
111111

112112
// Span from normal array
113113
byte[] array = new byte[16] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
114-
span = new SpanByte(array);
114+
span = new Span(array);
115115
Assert.AreEqual(span.Length, array.Length, $"SpanByte should have length of the array it takes: {array.Length}");
116116
Assert.IsFalse(span.IsEmpty, "SpanByte should NOT be IsEmpty");
117117

118118
// Span from normal array with different start and length
119-
span = new SpanByte(array, 2, 8);
119+
span = new Span(array, 2, 8);
120120
Assert.AreEqual(span.Length, 8, $"SpanByte should have length of 8");
121121
Assert.IsFalse(span.IsEmpty, "SpanByte should NOT be IsEmpty");
122122
}
@@ -126,7 +126,7 @@ public void SliceTests()
126126
{
127127
// Span from normal array
128128
byte[] array = new byte[16] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
129-
SpanByte span = new SpanByte(array);
129+
Span span = new Span(array);
130130
// Slice 2 elements and check
131131
var sliced = span.Slice(0, 2);
132132
Assert.AreEqual(sliced.Length, 2, "Sliced span lenght must be 2");
@@ -167,9 +167,9 @@ public void SliceTests()
167167
public void CopyToTests()
168168
{
169169
byte[] array = new byte[16] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
170-
SpanByte span = new SpanByte(array);
170+
Span span = new Span(array);
171171
// First a copy to with the full span
172-
SpanByte toCopy = new byte[span.Length];
172+
Span toCopy = new byte[span.Length];
173173
span.CopyTo(toCopy);
174174
for (int i = 0; i < span.Length; i++)
175175
{
@@ -192,14 +192,14 @@ public void GetElementsTests()
192192
{
193193
// Span from normal array
194194
byte[] array = new byte[16] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
195-
SpanByte span = new SpanByte(array);
195+
Span span = new Span(array);
196196
for (int i = 0; i < span.Length; i++)
197197
{
198198
Assert.AreEqual(span[i], array[i], "SpanByte value should be the same as from the original array");
199199
}
200200

201201
// Partial span
202-
span = new SpanByte(array, 2, 8);
202+
span = new Span(array, 2, 8);
203203
for (int i = 0; i < span.Length; i++)
204204
{
205205
Assert.AreEqual(span[i], array[i + 2], "SpanByte value should be the same as from the original array");
@@ -210,7 +210,7 @@ public void GetElementsTests()
210210
public void SetElementsTests()
211211
{
212212
// Create a span, and set the data
213-
SpanByte span = new byte[12];
213+
Span span = new byte[12];
214214
// All should be 0
215215
for (int i = 0; i < span.Length; i++)
216216
{

nanoFramework.CoreLibrary/CoreLibrary.nfproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@
6666
<Compile Include="System\AppDomainUnloadedException.cs" />
6767
<Compile Include="System\ApplicationException.cs" />
6868
<Compile Include="System\ArgumentException.cs" />
69+
<Compile Include="System\ArrayTypeMismatchException.cs" />
70+
<Compile Include="System\Diagnostics\StackTraceHiddenAttribute.cs" />
71+
<Compile Include="System\Runtime\InteropServices\InAttribute .cs" />
6972
<Compile Include="System\Diagnostics\NativeProfiledAttribute.cs" />
7073
<Compile Include="System\PlatformNotSupportedException.cs" />
7174
<Compile Include="System\ArgumentNullException.cs" />
@@ -188,7 +191,9 @@
188191
<Compile Include="System\SByte.cs" />
189192
<Compile Include="System\SerializableAttribute.cs" />
190193
<Compile Include="System\Single.cs" />
191-
<Compile Include="System\SpanByte.cs" />
194+
<Compile Include="System\ReadOnlySpan.cs" />
195+
<Compile Include="System\SpanDebugView.cs" />
196+
<Compile Include="System\Span.cs" />
192197
<Compile Include="System\String.cs" />
193198
<Compile Include="System\SystemException.cs" />
194199
<Compile Include="System\TargetFrameworkAttribute.cs" />
@@ -251,4 +256,4 @@
251256
<Import Project="..\packages\Microsoft.SourceLink.Common.1.1.1\build\Microsoft.SourceLink.Common.targets" Condition="Exists('..\packages\Microsoft.SourceLink.Common.1.1.1\build\Microsoft.SourceLink.Common.targets')" />
252257
<Import Project="..\packages\Microsoft.SourceLink.GitHub.1.1.1\build\Microsoft.SourceLink.GitHub.targets" Condition="Exists('..\packages\Microsoft.SourceLink.GitHub.1.1.1\build\Microsoft.SourceLink.GitHub.targets')" />
253258
<Import Project="..\packages\Nerdbank.GitVersioning.3.7.115\build\Nerdbank.GitVersioning.targets" Condition="Exists('..\packages\Nerdbank.GitVersioning.3.7.115\build\Nerdbank.GitVersioning.targets')" />
254-
</Project>
259+
</Project>

nanoFramework.CoreLibrary/System/Array.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections;
@@ -343,9 +343,28 @@ public static int IndexOf(Array array, Object value, int startIndex, int count)
343343
return -1;
344344
}
345345

346+
#if NANOCLR_REFLECTION
347+
/// <summary>
348+
/// Creates and returns an empty array of the specified type.
349+
/// </summary>
350+
/// <typeparam name="T"></typeparam>
351+
/// <returns></returns>
352+
public static T[] Empty<T>()
353+
{
354+
return EmptyArray<T>.Value;
355+
}
356+
#endif
357+
346358
[MethodImpl(MethodImplOptions.InternalCall)]
347359
private static extern bool TrySzIndexOf(Array sourceArray, int sourceIndex, int count, Object value, out int retVal);
348360

361+
private static class EmptyArray<T>
362+
{
363+
#pragma warning disable CA1825, IDE0300 // this is the implementation of Array.Empty<T>()
364+
internal static readonly T[] Value = new T[0];
365+
#pragma warning restore CA1825, IDE0300
366+
}
367+
349368
// This is the underlying Enumerator for all of our array-based data structures (Array, ArrayList, Stack, and Queue)
350369
// It supports enumerating over an array, a part of an array, and also will wrap around when the endIndex
351370
// specified is larger than the size of the array (to support Queue's internal circular array)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace System
5+
{
6+
/// <summary>
7+
/// The exception that is thrown when an attempt is made to store an element of the wrong type within an array.
8+
/// </summary>
9+
[Serializable]
10+
public class ArrayTypeMismatchException : SystemException
11+
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="ArrayTypeMismatchException"/> class.
14+
/// </summary>
15+
public ArrayTypeMismatchException()
16+
{
17+
}
18+
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="ArrayTypeMismatchException"/> class with a specified error message.
21+
/// </summary>
22+
/// <param name="message"></param>
23+
public ArrayTypeMismatchException(string? message)
24+
: base(message)
25+
{
26+
}
27+
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="ArrayTypeMismatchException"/> class with a specified error message and a reference to the inner exception that is the cause of this exception.
30+
/// </summary>
31+
/// <param name="message"></param>
32+
/// <param name="innerException"></param>
33+
public ArrayTypeMismatchException(string? message, Exception? innerException)
34+
: base(message, innerException)
35+
{
36+
}
37+
}
38+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace System.Diagnostics
5+
{
6+
/// <summary>
7+
/// Types and Methods attributed with StackTraceHidden will be omitted from the stack trace text shown in StackTrace.ToString()
8+
/// and Exception.StackTrace
9+
/// </summary>
10+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Constructor | AttributeTargets.Struct, Inherited = false)]
11+
public sealed class StackTraceHiddenAttribute : Attribute
12+
{
13+
/// <summary>
14+
/// Initializes a new instance of the <see cref="StackTraceHiddenAttribute"/> class.
15+
/// </summary>
16+
public StackTraceHiddenAttribute() { }
17+
}
18+
}

0 commit comments

Comments
 (0)