Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ReadOnlySpan.cs
Go to the documentation of this file.
7
8namespace System;
9
10[DebuggerTypeProxy(typeof(SpanDebugView<>))]
11[DebuggerDisplay("{ToString(),raw}")]
12[NonVersionable]
13public readonly ref struct ReadOnlySpan<T>
14{
15 public ref struct Enumerator
16 {
17 private readonly ReadOnlySpan<T> _span;
18
19 private int _index;
20
21 public ref readonly T Current
22 {
23 [MethodImpl(MethodImplOptions.AggressiveInlining)]
24 get
25 {
26 return ref _span[_index];
27 }
28 }
29
30 [MethodImpl(MethodImplOptions.AggressiveInlining)]
32 {
33 _span = span;
34 _index = -1;
35 }
36
37 [MethodImpl(MethodImplOptions.AggressiveInlining)]
38 public bool MoveNext()
39 {
40 int num = _index + 1;
41 if (num < _span.Length)
42 {
43 _index = num;
44 return true;
45 }
46 return false;
47 }
48 }
49
50 internal readonly ByReference<T> _pointer;
51
52 private readonly int _length;
53
54 public ref readonly T this[int index]
55 {
56 [MethodImpl(MethodImplOptions.AggressiveInlining)]
57 [Intrinsic]
58 [NonVersionable]
59 get
60 {
61 if ((uint)index >= (uint)_length)
62 {
64 }
65 return ref Unsafe.Add(ref _pointer.Value, (nint)(uint)index);
66 }
67 }
68
69 public int Length
70 {
71 [NonVersionable]
72 get
73 {
74 return _length;
75 }
76 }
77
78 public bool IsEmpty
79 {
80 [NonVersionable]
81 get
82 {
83 return 0u >= (uint)_length;
84 }
85 }
86
87 public static ReadOnlySpan<T> Empty => default(ReadOnlySpan<T>);
88
89 [MethodImpl(MethodImplOptions.AggressiveInlining)]
90 public ReadOnlySpan(T[]? array)
91 {
92 if (array == null)
93 {
94 this = default(ReadOnlySpan<T>);
95 return;
96 }
98 _length = array.Length;
99 }
100
101 [MethodImpl(MethodImplOptions.AggressiveInlining)]
102 public ReadOnlySpan(T[]? array, int start, int length)
103 {
104 if (array == null)
105 {
106 if (start != 0 || length != 0)
107 {
109 }
110 this = default(ReadOnlySpan<T>);
111 return;
112 }
113 if ((ulong)((long)(uint)start + (long)(uint)length) > (ulong)(uint)array.Length)
114 {
116 }
118 _length = length;
119 }
120
121 [MethodImpl(MethodImplOptions.AggressiveInlining)]
122 [CLSCompliant(false)]
123 public unsafe ReadOnlySpan(void* pointer, int length)
124 {
125 if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
126 {
128 }
129 if (length < 0)
130 {
132 }
133 _pointer = new ByReference<T>(ref Unsafe.As<byte, T>(ref *(byte*)pointer));
134 _length = length;
135 }
136
137 [MethodImpl(MethodImplOptions.AggressiveInlining)]
138 internal ReadOnlySpan(ref T ptr, int length)
139 {
140 _pointer = new ByReference<T>(ref ptr);
141 _length = length;
142 }
143
144 public static bool operator !=(ReadOnlySpan<T> left, ReadOnlySpan<T> right)
145 {
146 return !(left == right);
147 }
148
149 [Obsolete("Equals() on ReadOnlySpan has will always throw an exception. Use the equality operator instead.")]
150 [EditorBrowsable(EditorBrowsableState.Never)]
151 public override bool Equals(object? obj)
152 {
154 }
155
156 [Obsolete("GetHashCode() on ReadOnlySpan will always throw an exception.")]
157 [EditorBrowsable(EditorBrowsableState.Never)]
158 public override int GetHashCode()
159 {
161 }
162
163 public static implicit operator ReadOnlySpan<T>(T[]? array)
164 {
165 return new ReadOnlySpan<T>(array);
166 }
167
168 public static implicit operator ReadOnlySpan<T>(ArraySegment<T> segment)
169 {
170 return new ReadOnlySpan<T>(segment.Array, segment.Offset, segment.Count);
171 }
172
174 {
175 return new Enumerator(this);
176 }
177
178 [EditorBrowsable(EditorBrowsableState.Never)]
179 public ref readonly T GetPinnableReference()
180 {
181 ref T result = ref Unsafe.NullRef<T>();
182 if (_length != 0)
183 {
184 result = ref _pointer.Value;
185 }
186 return ref result;
187 }
188
189 [MethodImpl(MethodImplOptions.AggressiveInlining)]
191 {
192 if ((uint)_length <= (uint)destination.Length)
193 {
194 Buffer.Memmove<T>(ref destination._pointer.Value, ref _pointer.Value, (uint)_length);
195 }
196 else
197 {
199 }
200 }
201
203 {
204 bool result = false;
205 if ((uint)_length <= (uint)destination.Length)
206 {
207 Buffer.Memmove<T>(ref destination._pointer.Value, ref _pointer.Value, (uint)_length);
208 result = true;
209 }
210 return result;
211 }
212
213 public static bool operator ==(ReadOnlySpan<T> left, ReadOnlySpan<T> right)
214 {
215 if (left._length == right._length)
216 {
217 return Unsafe.AreSame(ref left._pointer.Value, ref right._pointer.Value);
218 }
219 return false;
220 }
221
222 public override string ToString()
223 {
224 if (typeof(T) == typeof(char))
225 {
226 return new string(new ReadOnlySpan<char>(ref Unsafe.As<T, char>(ref _pointer.Value), _length));
227 }
228 return $"System.ReadOnlySpan<{typeof(T).Name}>[{_length}]";
229 }
230
231 [MethodImpl(MethodImplOptions.AggressiveInlining)]
233 {
234 if ((uint)start > (uint)_length)
235 {
237 }
238 return new ReadOnlySpan<T>(ref Unsafe.Add(ref _pointer.Value, (nint)(uint)start), _length - start);
239 }
240
241 [MethodImpl(MethodImplOptions.AggressiveInlining)]
243 {
244 if ((ulong)((long)(uint)start + (long)(uint)length) > (ulong)(uint)_length)
245 {
247 }
248 return new ReadOnlySpan<T>(ref Unsafe.Add(ref _pointer.Value, (nint)(uint)start), length);
249 }
250
251 public T[] ToArray()
252 {
253 if (_length == 0)
254 {
255 return Array.Empty<T>();
256 }
257 T[] array = new T[_length];
259 return array;
260 }
261}
static void Memmove(ref byte dest, ref byte src, nuint len)
Definition Buffer.cs:215
static unsafe ref byte GetArrayDataReference(Array array)
static string NotSupported_CannotCallGetHashCodeOnSpan
Definition SR.cs:1660
static string NotSupported_CannotCallEqualsOnSpan
Definition SR.cs:1658
Definition SR.cs:7
static void ThrowIndexOutOfRangeException()
static void ThrowArgumentOutOfRangeException(System.ExceptionArgument argument)
static void ThrowInvalidTypeWithPointersNotSupported(Type targetType)
static void ThrowArgumentException_DestinationTooShort()
Enumerator(ReadOnlySpan< T > span)
readonly ReadOnlySpan< T > _span
ref readonly T GetPinnableReference()
static bool operator==(ReadOnlySpan< T > left, ReadOnlySpan< T > right)
Enumerator GetEnumerator()
bool TryCopyTo(Span< T > destination)
unsafe ReadOnlySpan(void *pointer, int length)
ReadOnlySpan(ref T ptr, int length)
void CopyTo(Span< T > destination)
ReadOnlySpan< T > Slice(int start, int length)
override bool Equals(object? obj)
readonly int _length
override int GetHashCode()
ReadOnlySpan(T[]? array, int start, int length)
readonly ByReference< T > _pointer
ReadOnlySpan< T > Slice(int start)
static bool operator!=(ReadOnlySpan< T > left, ReadOnlySpan< T > right)
override string ToString()
ReadOnlySpan(T[]? array)