ReadOnlySpan.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.ComponentModel;
  5. using System.Diagnostics;
  6. using System.Runtime.CompilerServices;
  7. using System.Runtime.Versioning;
  8. #pragma warning disable 0809 //warning CS0809: Obsolete member 'Span<T>.Equals(object)' overrides non-obsolete member 'object.Equals(object)'
  9. namespace System
  10. {
  11. /// <summary>
  12. /// ReadOnlySpan represents a contiguous region of arbitrary memory. Unlike arrays, it can point to either managed
  13. /// or native memory, or to memory allocated on the stack. It is type- and memory-safe.
  14. /// </summary>
  15. [DebuggerTypeProxy(typeof(SpanDebugView<>))]
  16. [DebuggerDisplay("{ToString(),raw}")]
  17. public readonly ref partial struct ReadOnlySpan<T>
  18. {
  19. /// <summary>
  20. /// The number of items in the read-only span.
  21. /// </summary>
  22. public int Length
  23. {
  24. [NonVersionable]
  25. get
  26. {
  27. return _length;
  28. }
  29. }
  30. /// <summary>
  31. /// Returns true if Length is 0.
  32. /// </summary>
  33. public bool IsEmpty
  34. {
  35. [NonVersionable]
  36. get
  37. {
  38. // Workaround for https://github.com/dotnet/coreclr/issues/19620
  39. return 0 >= (uint)_length;
  40. }
  41. }
  42. /// <summary>
  43. /// Returns false if left and right point at the same memory and have the same length. Note that
  44. /// this does *not* check to see if the *contents* are equal.
  45. /// </summary>
  46. public static bool operator !=(ReadOnlySpan<T> left, ReadOnlySpan<T> right) => !(left == right);
  47. /// <summary>
  48. /// This method is not supported as spans cannot be boxed. To compare two spans, use operator==.
  49. /// <exception cref="System.NotSupportedException">
  50. /// Always thrown by this method.
  51. /// </exception>
  52. /// </summary>
  53. [Obsolete("Equals() on ReadOnlySpan will always throw an exception. Use == instead.")]
  54. [EditorBrowsable(EditorBrowsableState.Never)]
  55. public override bool Equals(object obj)
  56. {
  57. throw new NotSupportedException(SR.NotSupported_CannotCallEqualsOnSpan);
  58. }
  59. /// <summary>
  60. /// This method is not supported as spans cannot be boxed.
  61. /// <exception cref="System.NotSupportedException">
  62. /// Always thrown by this method.
  63. /// </exception>
  64. /// </summary>
  65. [Obsolete("GetHashCode() on ReadOnlySpan will always throw an exception.")]
  66. [EditorBrowsable(EditorBrowsableState.Never)]
  67. public override int GetHashCode()
  68. {
  69. throw new NotSupportedException(SR.NotSupported_CannotCallGetHashCodeOnSpan);
  70. }
  71. /// <summary>
  72. /// Defines an implicit conversion of an array to a <see cref="ReadOnlySpan{T}"/>
  73. /// </summary>
  74. public static implicit operator ReadOnlySpan<T>(T[] array) => new ReadOnlySpan<T>(array);
  75. /// <summary>
  76. /// Defines an implicit conversion of a <see cref="ArraySegment{T}"/> to a <see cref="ReadOnlySpan{T}"/>
  77. /// </summary>
  78. public static implicit operator ReadOnlySpan<T>(ArraySegment<T> segment)
  79. => new ReadOnlySpan<T>(segment.Array, segment.Offset, segment.Count);
  80. /// <summary>
  81. /// Returns a 0-length read-only span whose base is the null pointer.
  82. /// </summary>
  83. public static ReadOnlySpan<T> Empty => default;
  84. /// <summary>Gets an enumerator for this span.</summary>
  85. public Enumerator GetEnumerator() => new Enumerator(this);
  86. /// <summary>Enumerates the elements of a <see cref="ReadOnlySpan{T}"/>.</summary>
  87. public ref struct Enumerator
  88. {
  89. /// <summary>The span being enumerated.</summary>
  90. private readonly ReadOnlySpan<T> _span;
  91. /// <summary>The next index to yield.</summary>
  92. private int _index;
  93. /// <summary>Initialize the enumerator.</summary>
  94. /// <param name="span">The span to enumerate.</param>
  95. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  96. internal Enumerator(ReadOnlySpan<T> span)
  97. {
  98. _span = span;
  99. _index = -1;
  100. }
  101. /// <summary>Advances the enumerator to the next element of the span.</summary>
  102. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  103. public bool MoveNext()
  104. {
  105. int index = _index + 1;
  106. if (index < _span.Length)
  107. {
  108. _index = index;
  109. return true;
  110. }
  111. return false;
  112. }
  113. /// <summary>Gets the element at the current position of the enumerator.</summary>
  114. public ref readonly T Current
  115. {
  116. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  117. get => ref _span[_index];
  118. }
  119. }
  120. }
  121. }