diff --git a/Ramstack.Structures.Tests/Collections/ArrayViewTests.cs b/Ramstack.Structures.Tests/Collections/ArrayViewTests.cs index 913837c..efe3eed 100644 --- a/Ramstack.Structures.Tests/Collections/ArrayViewTests.cs +++ b/Ramstack.Structures.Tests/Collections/ArrayViewTests.cs @@ -1,3 +1,4 @@ +using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; @@ -417,9 +418,15 @@ public void IndexOf() public void AsView(string? value) { var list = value?.ToCharArray(); + var immutable = value.AsSpan().ToImmutableArray(); + Assert.That( list.AsView(), Is.EquivalentTo(value.AsSpan().ToArray())); + + Assert.That( + immutable.AsView(), + Is.EquivalentTo(value.AsSpan().ToArray())); } [TestCase(null, 0)] @@ -432,10 +439,15 @@ public void AsView(string? value) public void AsView_Index(string? value, int index) { var list = value?.ToCharArray(); + var immutable = value.AsSpan().ToImmutableArray(); Assert.That( list.AsView(index), Is.EquivalentTo(value.AsSpan(index).ToArray())); + + Assert.That( + immutable.AsView(index), + Is.EquivalentTo(value.AsSpan(index).ToArray())); } [TestCase(null, 0, 0)] @@ -449,10 +461,15 @@ public void AsView_Index(string? value, int index) public void AsView_Range(string? value, int index, int length) { var list = value?.ToCharArray(); + var immutable = value.AsSpan().ToImmutableArray(); Assert.That( list.AsView(index, length), Is.EquivalentTo(value.AsSpan(index, length).ToArray())); + + Assert.That( + immutable.AsView(index, length), + Is.EquivalentTo(value.AsSpan(index, length).ToArray())); } [TestCase(null, 1)] @@ -462,10 +479,15 @@ public void AsView_Range(string? value, int index, int length) public void AsView_InvalidIndex_ShouldThrow(string? value, int index) { var list = value?.ToCharArray(); + var immutable = value.AsSpan().ToImmutableArray(); Assert.That( () => list.AsView(index), Throws.TypeOf()); + + Assert.That( + () => immutable.AsView(index), + Throws.TypeOf()); } [TestCase(null, 1, 0)] @@ -479,9 +501,14 @@ public void AsView_InvalidIndex_ShouldThrow(string? value, int index) public void AsView_InvalidRange_ShouldThrow(string? value, int index, int length) { var list = value?.ToCharArray(); + var immutable = value.AsSpan().ToImmutableArray(); Assert.That( () => list.AsView(index, length), Throws.TypeOf()); + + Assert.That( + () => immutable.AsView(index, length), + Throws.TypeOf()); } } diff --git a/Ramstack.Structures/Collections/ArrayViewExtensions.cs b/Ramstack.Structures/Collections/ArrayViewExtensions.cs index 6b32dfd..2676e02 100644 --- a/Ramstack.Structures/Collections/ArrayViewExtensions.cs +++ b/Ramstack.Structures/Collections/ArrayViewExtensions.cs @@ -22,7 +22,7 @@ public static ArrayView AsView(this T[]? value) => /// Creates a new over a portion of the target array from a specified position to the end of the array. /// /// The target array. - /// The index at which to begin this slice. + /// The index at which to begin the array view. /// /// An representing the array. /// @@ -34,14 +34,14 @@ public static ArrayView AsView(this T[]? value, int index) => /// Creates a new over a portion of the target array from a specified position for a specified number of elements. /// /// The target array. - /// The index at which to begin this slice. - /// The desired length for the slice. + /// The index at which to begin the array view. + /// The number of items in the array view. /// /// An representing the array. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ArrayView AsView(this T[]? value, int index, int length) => - new(value ?? [], index, length); + public static ArrayView AsView(this T[]? value, int index, int count) => + new(value ?? [], index, count); /// /// Finds the index of the first occurrence of a specified value in this instance. diff --git a/Ramstack.Structures/Collections/ImmutableArrayExtensions.cs b/Ramstack.Structures/Collections/ImmutableArrayExtensions.cs new file mode 100644 index 0000000..3041f0f --- /dev/null +++ b/Ramstack.Structures/Collections/ImmutableArrayExtensions.cs @@ -0,0 +1,43 @@ +using System.Collections.Immutable; +using System.Runtime.InteropServices; + +namespace Ramstack.Collections; + +/// +/// Provides extension methods for the structure. +/// +public static class ImmutableArrayExtensions +{ + /// + /// Creates an over an immutable array. + /// + /// The immutable array to wrap. + /// + /// The read-only view of the array. + /// + public static ArrayView AsView(this ImmutableArray value) => + ImmutableCollectionsMarshal.AsArray(value).AsView(); + + /// + /// Creates an over an immutable array starting at a specified position to the end of the array. + /// + /// The immutable array to wrap. + /// The index at which to begin the array view. + /// + /// The read-only view of the array. + /// + public static ArrayView AsView(this ImmutableArray value, int index) => + ImmutableCollectionsMarshal.AsArray(value).AsView(index); + + /// + /// Creates an over an immutable array starting at a specified position for a specified length. + /// + /// The immutable array to wrap. + /// The index at which to begin the array view. + /// The number of items in the array view. + /// + /// The read-only view of the array. + /// + public static ArrayView AsView(this ImmutableArray value, int index, int count) => + ImmutableCollectionsMarshal.AsArray(value).AsView(index, count); +}