|
| 1 | +using Ramstack.Collections; |
| 2 | +using Ramstack.Text; |
| 3 | + |
| 4 | +namespace Ramstack.InteropServices; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Provides extension methods for the <see cref="MemoryMarshal"/> class. |
| 8 | +/// </summary> |
| 9 | +public static class MemoryMarshalExtensions |
| 10 | +{ |
| 11 | + extension(MemoryMarshal) |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Creates a new <see cref="StringView"/> over a portion of the specified string |
| 15 | + /// starting at a specified position to the end of the string. |
| 16 | + /// </summary> |
| 17 | + /// <param name="value">The string to create a view over.</param> |
| 18 | + /// <param name="index">The zero-based starting position of the view in the string.</param> |
| 19 | + /// <returns> |
| 20 | + /// A <see cref="StringView"/> representing the specified portion of the string. |
| 21 | + /// </returns> |
| 22 | + /// <remarks> |
| 23 | + /// This method should be used with caution as it doesn't perform argument validation. |
| 24 | + /// The caller is responsible for ensuring: |
| 25 | + /// <list type="bullet"> |
| 26 | + /// <item><description><paramref name="value"/> is not null.</description></item> |
| 27 | + /// <item><description><paramref name="index"/> is within the bounds of the string.</description></item> |
| 28 | + /// </list> |
| 29 | + /// </remarks> |
| 30 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 31 | + public static StringView CreateStringView(string value, int index) |
| 32 | + { |
| 33 | + Debug.Assert(value is not null); |
| 34 | + Debug.Assert(value.AsSpan(index).Length == value.Length - index); |
| 35 | + |
| 36 | + return new StringView(value, index, value.Length - index, unused: 0); |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Creates a new <see cref="StringView"/> over a portion of the specified string |
| 41 | + /// starting at a specified position for a specified number of characters. |
| 42 | + /// </summary> |
| 43 | + /// <param name="value">The string to create a view over.</param> |
| 44 | + /// <param name="index">The zero-based starting position of the view in the string.</param> |
| 45 | + /// <param name="length">The number of characters to include in the view.</param> |
| 46 | + /// <returns> |
| 47 | + /// A <see cref="StringView"/> representing the specified portion of the string. |
| 48 | + /// </returns> |
| 49 | + /// <remarks> |
| 50 | + /// This method should be used with caution as it doesn't perform argument validation. |
| 51 | + /// The caller is responsible for ensuring: |
| 52 | + /// <list type="bullet"> |
| 53 | + /// <item><description><paramref name="value"/> is not null.</description></item> |
| 54 | + /// <item><description><paramref name="index"/> is within the bounds of the string.</description></item> |
| 55 | + /// <item><description><paramref name="length"/> is valid for the specified index.</description></item> |
| 56 | + /// </list> |
| 57 | + /// </remarks> |
| 58 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 59 | + public static StringView CreateStringView(string value, int index, int length) |
| 60 | + { |
| 61 | + Debug.Assert(value is not null); |
| 62 | + Debug.Assert(value.AsSpan(index, length).Length == length); |
| 63 | + |
| 64 | + return new StringView(value, index, length, unused: 0); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Creates a new <see cref="ArrayView{T}"/> over a portion of the specified array, |
| 69 | + /// starting at a specified position to the end of the array. |
| 70 | + /// </summary> |
| 71 | + /// <typeparam name="T">The type of elements in the array.</typeparam> |
| 72 | + /// <param name="array">The array to create a view over.</param> |
| 73 | + /// <param name="index">The zero-based starting position of the view in the array.</param> |
| 74 | + /// <returns> |
| 75 | + /// An <see cref="ArrayView{T}"/> representing the specified portion of the array. |
| 76 | + /// </returns> |
| 77 | + /// <remarks> |
| 78 | + /// This method should be used with caution as it doesn't perform argument validation. |
| 79 | + /// The caller is responsible for ensuring: |
| 80 | + /// <list type="bullet"> |
| 81 | + /// <item><description><paramref name="array"/> is not null.</description></item> |
| 82 | + /// <item><description><paramref name="index"/> is within the bounds of the array.</description></item> |
| 83 | + /// </list> |
| 84 | + /// </remarks> |
| 85 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 86 | + public static ArrayView<T> CreateArrayView<T>(T[] array, int index) |
| 87 | + { |
| 88 | + Debug.Assert(array is not null); |
| 89 | + Debug.Assert(array.AsSpan(index).Length == array.Length - index); |
| 90 | + |
| 91 | + return new ArrayView<T>(array, index, array.Length - index, unused: 0); |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Creates a new <see cref="ArrayView{T}"/> over a portion of the specified array |
| 96 | + /// starting at a specified position for a specified number of elements. |
| 97 | + /// </summary> |
| 98 | + /// <typeparam name="T">The type of elements in the array.</typeparam> |
| 99 | + /// <param name="array">The array to create a view over.</param> |
| 100 | + /// <param name="index">The zero-based starting position of the view in the array.</param> |
| 101 | + /// <param name="length">The number of elements to include in the view.</param> |
| 102 | + /// <returns> |
| 103 | + /// An <see cref="ArrayView{T}"/> representing the specified portion of the array. |
| 104 | + /// </returns> |
| 105 | + /// <remarks> |
| 106 | + /// This method should be used with caution as it doesn't perform argument validation. |
| 107 | + /// The caller is responsible for ensuring: |
| 108 | + /// <list type="bullet"> |
| 109 | + /// <item><description><paramref name="array"/> is not null.</description></item> |
| 110 | + /// <item><description><paramref name="index"/> is within the bounds of the array.</description></item> |
| 111 | + /// <item><description><paramref name="length"/> is valid for the specified index.</description></item> |
| 112 | + /// </list> |
| 113 | + /// </remarks> |
| 114 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 115 | + public static ArrayView<T> CreateArrayView<T>(T[] array, int index, int length) |
| 116 | + { |
| 117 | + Debug.Assert(array is not null); |
| 118 | + Debug.Assert(array.AsSpan(index, length).Length == length); |
| 119 | + |
| 120 | + return new ArrayView<T>(array, index, length, unused: 0); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments