ReadOnlySpan.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.Versioning;
  7. using System.Text;
  8. using EditorBrowsableAttribute = System.ComponentModel.EditorBrowsableAttribute;
  9. using EditorBrowsableState = System.ComponentModel.EditorBrowsableState;
  10. using Internal.Runtime.CompilerServices;
  11. #pragma warning disable 0809 //warning CS0809: Obsolete member 'Span<T>.Equals(object)' overrides non-obsolete member 'object.Equals(object)'
  12. #pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
  13. #if BIT64
  14. using nuint = System.UInt64;
  15. #else
  16. using nuint = System.UInt32;
  17. #endif
  18. namespace System
  19. {
  20. /// <summary>
  21. /// ReadOnlySpan represents a contiguous region of arbitrary memory. Unlike arrays, it can point to either managed
  22. /// or native memory, or to memory allocated on the stack. It is type- and memory-safe.
  23. /// </summary>
  24. [DebuggerTypeProxy(typeof(SpanDebugView<>))]
  25. [DebuggerDisplay("{ToString(),raw}")]
  26. [NonVersionable]
  27. public readonly ref struct ReadOnlySpan<T>
  28. {
  29. /// <summary>A byref or a native ptr.</summary>
  30. internal readonly ByReference<T> _pointer;
  31. /// <summary>The number of elements this ReadOnlySpan contains.</summary>
  32. private readonly int _length;
  33. /// <summary>
  34. /// Creates a new read-only span over the entirety of the target array.
  35. /// </summary>
  36. /// <param name="array">The target array.</param>
  37. /// <remarks>Returns default when <paramref name="array"/> is null.</remarks>
  38. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  39. public ReadOnlySpan(T[]? array)
  40. {
  41. if (array == null)
  42. {
  43. this = default;
  44. return; // returns default
  45. }
  46. _pointer = new ByReference<T>(ref Unsafe.As<byte, T>(ref array.GetRawSzArrayData()));
  47. _length = array.Length;
  48. }
  49. /// <summary>
  50. /// Creates a new read-only span over the portion of the target array beginning
  51. /// at 'start' index and ending at 'end' index (exclusive).
  52. /// </summary>
  53. /// <param name="array">The target array.</param>
  54. /// <param name="start">The index at which to begin the read-only span.</param>
  55. /// <param name="length">The number of items in the read-only span.</param>
  56. /// <remarks>Returns default when <paramref name="array"/> is null.</remarks>
  57. /// <exception cref="System.ArgumentOutOfRangeException">
  58. /// Thrown when the specified <paramref name="start"/> or end index is not in the range (&lt;0 or &gt;Length).
  59. /// </exception>
  60. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  61. public ReadOnlySpan(T[]? array, int start, int length)
  62. {
  63. if (array == null)
  64. {
  65. if (start != 0 || length != 0)
  66. ThrowHelper.ThrowArgumentOutOfRangeException();
  67. this = default;
  68. return; // returns default
  69. }
  70. #if BIT64
  71. // See comment in Span<T>.Slice for how this works.
  72. if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)array.Length)
  73. ThrowHelper.ThrowArgumentOutOfRangeException();
  74. #else
  75. if ((uint)start > (uint)array.Length || (uint)length > (uint)(array.Length - start))
  76. ThrowHelper.ThrowArgumentOutOfRangeException();
  77. #endif
  78. _pointer = new ByReference<T>(ref Unsafe.Add(ref Unsafe.As<byte, T>(ref array.GetRawSzArrayData()), start));
  79. _length = length;
  80. }
  81. /// <summary>
  82. /// Creates a new read-only span over the target unmanaged buffer. Clearly this
  83. /// is quite dangerous, because we are creating arbitrarily typed T's
  84. /// out of a void*-typed block of memory. And the length is not checked.
  85. /// But if this creation is correct, then all subsequent uses are correct.
  86. /// </summary>
  87. /// <param name="pointer">An unmanaged pointer to memory.</param>
  88. /// <param name="length">The number of <typeparamref name="T"/> elements the memory contains.</param>
  89. /// <exception cref="System.ArgumentException">
  90. /// Thrown when <typeparamref name="T"/> is reference type or contains pointers and hence cannot be stored in unmanaged memory.
  91. /// </exception>
  92. /// <exception cref="System.ArgumentOutOfRangeException">
  93. /// Thrown when the specified <paramref name="length"/> is negative.
  94. /// </exception>
  95. [CLSCompliant(false)]
  96. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  97. public unsafe ReadOnlySpan(void* pointer, int length)
  98. {
  99. if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
  100. ThrowHelper.ThrowInvalidTypeWithPointersNotSupported(typeof(T));
  101. if (length < 0)
  102. ThrowHelper.ThrowArgumentOutOfRangeException();
  103. _pointer = new ByReference<T>(ref Unsafe.As<byte, T>(ref *(byte*)pointer));
  104. _length = length;
  105. }
  106. // Constructor for internal use only.
  107. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  108. internal ReadOnlySpan(ref T ptr, int length)
  109. {
  110. Debug.Assert(length >= 0);
  111. _pointer = new ByReference<T>(ref ptr);
  112. _length = length;
  113. }
  114. /// <summary>
  115. /// Returns the specified element of the read-only span.
  116. /// </summary>
  117. /// <param name="index"></param>
  118. /// <returns></returns>
  119. /// <exception cref="System.IndexOutOfRangeException">
  120. /// Thrown when index less than 0 or index greater than or equal to Length
  121. /// </exception>
  122. public ref readonly T this[int index]
  123. {
  124. [Intrinsic]
  125. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  126. [NonVersionable]
  127. get
  128. {
  129. if ((uint)index >= (uint)_length)
  130. ThrowHelper.ThrowIndexOutOfRangeException();
  131. return ref Unsafe.Add(ref _pointer.Value, index);
  132. }
  133. }
  134. /// <summary>
  135. /// The number of items in the read-only span.
  136. /// </summary>
  137. public int Length
  138. {
  139. [NonVersionable]
  140. get => _length;
  141. }
  142. /// <summary>
  143. /// Returns true if Length is 0.
  144. /// </summary>
  145. public bool IsEmpty
  146. {
  147. [NonVersionable]
  148. get => 0 >= (uint)_length; // Workaround for https://github.com/dotnet/coreclr/issues/19620
  149. }
  150. /// <summary>
  151. /// Returns false if left and right point at the same memory and have the same length. Note that
  152. /// this does *not* check to see if the *contents* are equal.
  153. /// </summary>
  154. public static bool operator !=(ReadOnlySpan<T> left, ReadOnlySpan<T> right) => !(left == right);
  155. /// <summary>
  156. /// This method is not supported as spans cannot be boxed. To compare two spans, use operator==.
  157. /// <exception cref="System.NotSupportedException">
  158. /// Always thrown by this method.
  159. /// </exception>
  160. /// </summary>
  161. [Obsolete("Equals() on ReadOnlySpan will always throw an exception. Use == instead.")]
  162. [EditorBrowsable(EditorBrowsableState.Never)]
  163. public override bool Equals(object? obj) =>
  164. throw new NotSupportedException(SR.NotSupported_CannotCallEqualsOnSpan);
  165. /// <summary>
  166. /// This method is not supported as spans cannot be boxed.
  167. /// <exception cref="System.NotSupportedException">
  168. /// Always thrown by this method.
  169. /// </exception>
  170. /// </summary>
  171. [Obsolete("GetHashCode() on ReadOnlySpan will always throw an exception.")]
  172. [EditorBrowsable(EditorBrowsableState.Never)]
  173. public override int GetHashCode() =>
  174. throw new NotSupportedException(SR.NotSupported_CannotCallGetHashCodeOnSpan);
  175. /// <summary>
  176. /// Defines an implicit conversion of an array to a <see cref="ReadOnlySpan{T}"/>
  177. /// </summary>
  178. public static implicit operator ReadOnlySpan<T>(T[]? array) => new ReadOnlySpan<T>(array);
  179. /// <summary>
  180. /// Defines an implicit conversion of a <see cref="ArraySegment{T}"/> to a <see cref="ReadOnlySpan{T}"/>
  181. /// </summary>
  182. public static implicit operator ReadOnlySpan<T>(ArraySegment<T> segment)
  183. => new ReadOnlySpan<T>(segment.Array, segment.Offset, segment.Count);
  184. /// <summary>
  185. /// Returns a 0-length read-only span whose base is the null pointer.
  186. /// </summary>
  187. public static ReadOnlySpan<T> Empty => default;
  188. /// <summary>Gets an enumerator for this span.</summary>
  189. public Enumerator GetEnumerator() => new Enumerator(this);
  190. /// <summary>Enumerates the elements of a <see cref="ReadOnlySpan{T}"/>.</summary>
  191. public ref struct Enumerator
  192. {
  193. /// <summary>The span being enumerated.</summary>
  194. private readonly ReadOnlySpan<T> _span;
  195. /// <summary>The next index to yield.</summary>
  196. private int _index;
  197. /// <summary>Initialize the enumerator.</summary>
  198. /// <param name="span">The span to enumerate.</param>
  199. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  200. internal Enumerator(ReadOnlySpan<T> span)
  201. {
  202. _span = span;
  203. _index = -1;
  204. }
  205. /// <summary>Advances the enumerator to the next element of the span.</summary>
  206. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  207. public bool MoveNext()
  208. {
  209. int index = _index + 1;
  210. if (index < _span.Length)
  211. {
  212. _index = index;
  213. return true;
  214. }
  215. return false;
  216. }
  217. /// <summary>Gets the element at the current position of the enumerator.</summary>
  218. public ref readonly T Current
  219. {
  220. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  221. get => ref _span[_index];
  222. }
  223. }
  224. /// <summary>
  225. /// Returns a reference to the 0th element of the Span. If the Span is empty, returns null reference.
  226. /// It can be used for pinning and is required to support the use of span within a fixed statement.
  227. /// </summary>
  228. [EditorBrowsable(EditorBrowsableState.Never)]
  229. public ref readonly T GetPinnableReference()
  230. {
  231. // Ensure that the native code has just one forward branch that is predicted-not-taken.
  232. ref T ret = ref Unsafe.NullRef<T>();
  233. if (_length != 0) ret = ref _pointer.Value;
  234. return ref ret;
  235. }
  236. /// <summary>
  237. /// Copies the contents of this read-only span into destination span. If the source
  238. /// and destinations overlap, this method behaves as if the original values in
  239. /// a temporary location before the destination is overwritten.
  240. ///
  241. /// <param name="destination">The span to copy items into.</param>
  242. /// <exception cref="System.ArgumentException">
  243. /// Thrown when the destination Span is shorter than the source Span.
  244. /// </exception>
  245. /// </summary>
  246. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  247. public void CopyTo(Span<T> destination)
  248. {
  249. // Using "if (!TryCopyTo(...))" results in two branches: one for the length
  250. // check, and one for the result of TryCopyTo. Since these checks are equivalent,
  251. // we can optimize by performing the check once ourselves then calling Memmove directly.
  252. if ((uint)_length <= (uint)destination.Length)
  253. {
  254. Buffer.Memmove(ref destination._pointer.Value, ref _pointer.Value, (nuint)_length);
  255. }
  256. else
  257. {
  258. ThrowHelper.ThrowArgumentException_DestinationTooShort();
  259. }
  260. }
  261. /// <summary>
  262. /// Copies the contents of this read-only span into destination span. If the source
  263. /// and destinations overlap, this method behaves as if the original values in
  264. /// a temporary location before the destination is overwritten.
  265. /// </summary>
  266. /// <returns>If the destination span is shorter than the source span, this method
  267. /// return false and no data is written to the destination.</returns>
  268. /// <param name="destination">The span to copy items into.</param>
  269. public bool TryCopyTo(Span<T> destination)
  270. {
  271. bool retVal = false;
  272. if ((uint)_length <= (uint)destination.Length)
  273. {
  274. Buffer.Memmove(ref destination._pointer.Value, ref _pointer.Value, (nuint)_length);
  275. retVal = true;
  276. }
  277. return retVal;
  278. }
  279. /// <summary>
  280. /// Returns true if left and right point at the same memory and have the same length. Note that
  281. /// this does *not* check to see if the *contents* are equal.
  282. /// </summary>
  283. public static bool operator ==(ReadOnlySpan<T> left, ReadOnlySpan<T> right) =>
  284. left._length == right._length &&
  285. Unsafe.AreSame<T>(ref left._pointer.Value, ref right._pointer.Value);
  286. /// <summary>
  287. /// For <see cref="ReadOnlySpan{Char}"/>, returns a new instance of string that represents the characters pointed to by the span.
  288. /// Otherwise, returns a <see cref="string"/> with the name of the type and the number of elements.
  289. /// </summary>
  290. public override string ToString()
  291. {
  292. if (typeof(T) == typeof(char))
  293. {
  294. return new string(new ReadOnlySpan<char>(ref Unsafe.As<T, char>(ref _pointer.Value), _length));
  295. }
  296. #if FEATURE_UTF8STRING
  297. else if (typeof(T) == typeof(Char8))
  298. {
  299. // TODO_UTF8STRING: Call into optimized transcoding routine when it's available.
  300. return Encoding.UTF8.GetString(new ReadOnlySpan<byte>(ref Unsafe.As<T, byte>(ref _pointer.Value), _length));
  301. }
  302. #endif // FEATURE_UTF8STRING
  303. return string.Format("System.ReadOnlySpan<{0}>[{1}]", typeof(T).Name, _length);
  304. }
  305. /// <summary>
  306. /// Forms a slice out of the given read-only span, beginning at 'start'.
  307. /// </summary>
  308. /// <param name="start">The index at which to begin this slice.</param>
  309. /// <exception cref="System.ArgumentOutOfRangeException">
  310. /// Thrown when the specified <paramref name="start"/> index is not in range (&lt;0 or &gt;Length).
  311. /// </exception>
  312. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  313. public ReadOnlySpan<T> Slice(int start)
  314. {
  315. if ((uint)start > (uint)_length)
  316. ThrowHelper.ThrowArgumentOutOfRangeException();
  317. return new ReadOnlySpan<T>(ref Unsafe.Add(ref _pointer.Value, start), _length - start);
  318. }
  319. /// <summary>
  320. /// Forms a slice out of the given read-only span, beginning at 'start', of given length
  321. /// </summary>
  322. /// <param name="start">The index at which to begin this slice.</param>
  323. /// <param name="length">The desired length for the slice (exclusive).</param>
  324. /// <exception cref="System.ArgumentOutOfRangeException">
  325. /// Thrown when the specified <paramref name="start"/> or end index is not in range (&lt;0 or &gt;Length).
  326. /// </exception>
  327. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  328. public ReadOnlySpan<T> Slice(int start, int length)
  329. {
  330. #if BIT64
  331. // See comment in Span<T>.Slice for how this works.
  332. if ((ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)_length)
  333. ThrowHelper.ThrowArgumentOutOfRangeException();
  334. #else
  335. if ((uint)start > (uint)_length || (uint)length > (uint)(_length - start))
  336. ThrowHelper.ThrowArgumentOutOfRangeException();
  337. #endif
  338. return new ReadOnlySpan<T>(ref Unsafe.Add(ref _pointer.Value, start), length);
  339. }
  340. /// <summary>
  341. /// Copies the contents of this read-only span into a new array. This heap
  342. /// allocates, so should generally be avoided, however it is sometimes
  343. /// necessary to bridge the gap with APIs written in terms of arrays.
  344. /// </summary>
  345. public T[] ToArray()
  346. {
  347. if (_length == 0)
  348. return Array.Empty<T>();
  349. var destination = new T[_length];
  350. Buffer.Memmove(ref Unsafe.As<byte, T>(ref destination.GetRawSzArrayData()), ref _pointer.Value, (nuint)_length);
  351. return destination;
  352. }
  353. }
  354. }