Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ReadOnlyMemory.cs
Go to the documentation of this file.
8
9namespace System;
10
11[DebuggerTypeProxy(typeof(MemoryDebugView<>))]
12[DebuggerDisplay("{ToString(),raw}")]
13public readonly struct ReadOnlyMemory<T> : IEquatable<ReadOnlyMemory<T>>
14{
15 private readonly object _object;
16
17 private readonly int _index;
18
19 private readonly int _length;
20
21 internal const int RemoveFlagsBitMask = int.MaxValue;
22
23 public static ReadOnlyMemory<T> Empty => default(ReadOnlyMemory<T>);
24
25 public int Length => _length;
26
27 public bool IsEmpty => _length == 0;
28
29 public unsafe ReadOnlySpan<T> Span
30 {
31 [MethodImpl(MethodImplOptions.AggressiveInlining)]
32 get
33 {
34 ref T ptr = ref Unsafe.NullRef<T>();
35 int length = 0;
36 object @object = _object;
37 if (@object != null)
38 {
39 if (typeof(T) == typeof(char) && @object.GetType() == typeof(string))
40 {
41 ptr = ref Unsafe.As<char, T>(ref Unsafe.As<string>(@object).GetRawStringData());
42 length = Unsafe.As<string>(@object).Length;
43 }
44 else if (RuntimeHelpers.ObjectHasComponentSize(@object))
45 {
46 ptr = ref MemoryMarshal.GetArrayDataReference(Unsafe.As<T[]>(@object));
47 length = Unsafe.As<T[]>(@object).Length;
48 }
49 else
50 {
51 Span<T> span = Unsafe.As<MemoryManager<T>>(@object).GetSpan();
52 ptr = ref MemoryMarshal.GetReference(span);
53 length = span.Length;
54 }
55 nuint num = (uint)_index & 0x7FFFFFFFu;
56 int length2 = _length;
57 if ((ulong)((long)num + (long)(uint)length2) > (ulong)(uint)length)
58 {
60 }
61 ptr = ref Unsafe.Add(ref ptr, (IntPtr)(void*)num);
63 }
64 return new ReadOnlySpan<T>(ref ptr, length);
65 }
66 }
67
68 [MethodImpl(MethodImplOptions.AggressiveInlining)]
69 public ReadOnlyMemory(T[]? array)
70 {
71 if (array == null)
72 {
73 this = default(ReadOnlyMemory<T>);
74 return;
75 }
76 _object = array;
77 _index = 0;
78 _length = array.Length;
79 }
80
81 [MethodImpl(MethodImplOptions.AggressiveInlining)]
82 public ReadOnlyMemory(T[]? array, int start, int length)
83 {
84 if (array == null)
85 {
86 if (start != 0 || length != 0)
87 {
89 }
90 this = default(ReadOnlyMemory<T>);
91 return;
92 }
93 if ((ulong)((long)(uint)start + (long)(uint)length) > (ulong)(uint)array.Length)
94 {
96 }
97 _object = array;
98 _index = start;
100 }
101
102 [MethodImpl(MethodImplOptions.AggressiveInlining)]
103 internal ReadOnlyMemory(object obj, int start, int length)
104 {
105 _object = obj;
106 _index = start;
107 _length = length;
108 }
109
110 public static implicit operator ReadOnlyMemory<T>(T[]? array)
111 {
112 return new ReadOnlyMemory<T>(array);
113 }
114
115 public static implicit operator ReadOnlyMemory<T>(ArraySegment<T> segment)
116 {
117 return new ReadOnlyMemory<T>(segment.Array, segment.Offset, segment.Count);
118 }
119
120 public override string ToString()
121 {
122 if (typeof(T) == typeof(char))
123 {
124 if (!(_object is string text))
125 {
126 return Span.ToString();
127 }
128 return text.Substring(_index, _length);
129 }
130 return $"System.ReadOnlyMemory<{typeof(T).Name}>[{_length}]";
131 }
132
133 [MethodImpl(MethodImplOptions.AggressiveInlining)]
135 {
136 if ((uint)start > (uint)_length)
137 {
139 }
141 }
142
143 [MethodImpl(MethodImplOptions.AggressiveInlining)]
145 {
146 if ((ulong)((long)(uint)start + (long)(uint)length) > (ulong)(uint)_length)
147 {
149 }
151 }
152
154 {
155 Span.CopyTo(destination.Span);
156 }
157
159 {
160 return Span.TryCopyTo(destination.Span);
161 }
162
163 public unsafe MemoryHandle Pin()
164 {
165 object @object = _object;
166 if (@object != null)
167 {
168 if (typeof(T) == typeof(char) && @object is string text)
169 {
170 GCHandle handle = GCHandle.Alloc(@object, GCHandleType.Pinned);
171 return new MemoryHandle(Unsafe.AsPointer(ref Unsafe.Add(ref text.GetRawStringData(), _index)), handle);
172 }
174 {
175 if (_index < 0)
176 {
177 void* pointer = Unsafe.Add<T>(Unsafe.AsPointer(ref MemoryMarshal.GetArrayDataReference(Unsafe.As<T[]>(@object))), _index & 0x7FFFFFFF);
178 return new MemoryHandle(pointer);
179 }
180 GCHandle handle2 = GCHandle.Alloc(@object, GCHandleType.Pinned);
181 void* pointer2 = Unsafe.Add<T>(Unsafe.AsPointer(ref MemoryMarshal.GetArrayDataReference(Unsafe.As<T[]>(@object))), _index);
182 return new MemoryHandle(pointer2, handle2);
183 }
184 return Unsafe.As<MemoryManager<T>>(@object).Pin(_index);
185 }
186 return default(MemoryHandle);
187 }
188
189 public T[] ToArray()
190 {
191 return Span.ToArray();
192 }
193
194 [EditorBrowsable(EditorBrowsableState.Never)]
195 public override bool Equals([NotNullWhen(true)] object? obj)
196 {
198 {
199 return Equals(other);
200 }
201 if (obj is Memory<T> memory)
202 {
203 return Equals(memory);
204 }
205 return false;
206 }
207
209 {
210 if (_object == other._object && _index == other._index)
211 {
212 return _length == other._length;
213 }
214 return false;
215 }
216
217 [EditorBrowsable(EditorBrowsableState.Never)]
218 public override int GetHashCode()
219 {
220 if (_object == null)
221 {
222 return 0;
223 }
225 }
226
227 [MethodImpl(MethodImplOptions.AggressiveInlining)]
228 internal object GetObjectStartLength(out int start, out int length)
229 {
230 start = _index;
231 length = _length;
232 return _object;
233 }
234}
static unsafe bool ObjectHasComponentSize(object obj)
static unsafe ref byte GetArrayDataReference(Array array)
static void ThrowArgumentOutOfRangeException(System.ExceptionArgument argument)
override string ToString()
bool Equals(ReadOnlyMemory< T > other)
object GetObjectStartLength(out int start, out int length)
ReadOnlyMemory(T[]? array, int start, int length)
ReadOnlyMemory(object obj, int start, int length)
void CopyTo(Memory< T > destination)
override int GetHashCode()
ReadOnlyMemory< T > Slice(int start)
readonly object _object
bool TryCopyTo(Memory< T > destination)
unsafe MemoryHandle Pin()
ReadOnlyMemory< T > Slice(int start, int length)
override bool Equals([NotNullWhen(true)] object? obj)
static GCHandle Alloc(object? value)
Definition GCHandle.cs:81
void CopyTo(Span< T > destination)
Definition Span.cs:224
T[] ToArray()
Definition Span.cs:291
int Length
Definition Span.cs:70
override string ToString()
Definition Span.cs:261
bool TryCopyTo(Span< T > destination)
Definition Span.cs:236