Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SpanUtility.cs
Go to the documentation of this file.
4
6
7internal static class SpanUtility
8{
9 [MethodImpl(MethodImplOptions.AggressiveInlining)]
10 public static bool IsValidIndex<T>(ReadOnlySpan<T> span, int index)
11 {
12 if ((uint)index >= (uint)span.Length)
13 {
14 return false;
15 }
16 return true;
17 }
18
19 [MethodImpl(MethodImplOptions.AggressiveInlining)]
20 public static bool IsValidIndex<T>(Span<T> span, int index)
21 {
22 if ((uint)index >= (uint)span.Length)
23 {
24 return false;
25 }
26 return true;
27 }
28
29 [MethodImpl(MethodImplOptions.AggressiveInlining)]
30 public static bool TryWriteBytes(Span<byte> span, byte a, byte b, byte c, byte d)
31 {
32 if (span.Length >= 4)
33 {
34 Unsafe.WriteUnaligned(value: (uint)((!BitConverter.IsLittleEndian) ? ((a << 24) | (b << 16) | (c << 8) | d) : ((d << 24) | (c << 16) | (b << 8) | a)), destination: ref MemoryMarshal.GetReference(span));
35 return true;
36 }
37 return false;
38 }
39
40 [MethodImpl(MethodImplOptions.AggressiveInlining)]
41 public static bool TryWriteBytes(Span<byte> span, byte a, byte b, byte c, byte d, byte e)
42 {
43 if (span.Length >= 5)
44 {
45 uint value = (uint)((!BitConverter.IsLittleEndian) ? ((a << 24) | (b << 16) | (c << 8) | d) : ((d << 24) | (c << 16) | (b << 8) | a));
46 ref byte reference = ref MemoryMarshal.GetReference(span);
47 Unsafe.WriteUnaligned(ref reference, value);
48 Unsafe.Add(ref reference, 4) = e;
49 return true;
50 }
51 return false;
52 }
53
54 [MethodImpl(MethodImplOptions.AggressiveInlining)]
55 public static bool TryWriteBytes(Span<byte> span, byte a, byte b, byte c, byte d, byte e, byte f)
56 {
57 if (span.Length >= 6)
58 {
59 uint value;
60 uint num;
62 {
63 value = (uint)((d << 24) | (c << 16) | (b << 8) | a);
64 num = (uint)((f << 8) | e);
65 }
66 else
67 {
68 value = (uint)((a << 24) | (b << 16) | (c << 8) | d);
69 num = (uint)((e << 8) | f);
70 }
71 ref byte reference = ref MemoryMarshal.GetReference(span);
72 Unsafe.WriteUnaligned(ref reference, value);
73 Unsafe.WriteUnaligned(ref Unsafe.Add(ref reference, 4), (ushort)num);
74 return true;
75 }
76 return false;
77 }
78
79 [MethodImpl(MethodImplOptions.AggressiveInlining)]
80 public static bool TryWriteChars(Span<char> span, char a, char b, char c, char d)
81 {
82 if (span.Length >= 4)
83 {
84 Unsafe.WriteUnaligned(value: (!BitConverter.IsLittleEndian) ? (((ulong)a << 48) | ((ulong)b << 32) | ((ulong)c << 16) | d) : (((ulong)d << 48) | ((ulong)c << 32) | ((ulong)b << 16) | a), destination: ref Unsafe.As<char, byte>(ref MemoryMarshal.GetReference(span)));
85 return true;
86 }
87 return false;
88 }
89
90 [MethodImpl(MethodImplOptions.AggressiveInlining)]
91 public static bool TryWriteChars(Span<char> span, char a, char b, char c, char d, char e)
92 {
93 if (span.Length >= 5)
94 {
95 ulong value = ((!BitConverter.IsLittleEndian) ? (((ulong)a << 48) | ((ulong)b << 32) | ((ulong)c << 16) | d) : (((ulong)d << 48) | ((ulong)c << 32) | ((ulong)b << 16) | a));
96 ref char reference = ref MemoryMarshal.GetReference(span);
97 Unsafe.WriteUnaligned(ref Unsafe.As<char, byte>(ref reference), value);
98 Unsafe.Add(ref reference, 4) = e;
99 return true;
100 }
101 return false;
102 }
103
104 [MethodImpl(MethodImplOptions.AggressiveInlining)]
105 public static bool TryWriteChars(Span<char> span, char a, char b, char c, char d, char e, char f)
106 {
107 if (span.Length >= 6)
108 {
109 ulong value;
110 uint value2;
112 {
113 value = ((ulong)d << 48) | ((ulong)c << 32) | ((ulong)b << 16) | a;
114 value2 = ((uint)f << 16) | e;
115 }
116 else
117 {
118 value = ((ulong)a << 48) | ((ulong)b << 32) | ((ulong)c << 16) | d;
119 value2 = ((uint)e << 16) | f;
120 }
121 ref byte reference = ref Unsafe.As<char, byte>(ref MemoryMarshal.GetReference(span));
122 Unsafe.WriteUnaligned(ref reference, value);
123 Unsafe.WriteUnaligned(ref Unsafe.AddByteOffset(ref reference, (IntPtr)8), value2);
124 return true;
125 }
126 return false;
127 }
128
129 [MethodImpl(MethodImplOptions.AggressiveInlining)]
130 public static bool TryWriteUInt64LittleEndian(Span<byte> span, int offset, ulong value)
131 {
133 {
135 {
137 }
138 Unsafe.WriteUnaligned(ref Unsafe.Add(ref MemoryMarshal.GetReference(span), (nint)(uint)offset), value);
139 return true;
140 }
141 return false;
142 }
143
144 [MethodImpl(MethodImplOptions.AggressiveInlining)]
145 private static bool AreValidIndexAndLength(int spanRealLength, int requestedOffset, int requestedLength)
146 {
147 if (IntPtr.Size == 4)
148 {
149 if ((uint)requestedOffset > (uint)spanRealLength)
150 {
151 return false;
152 }
153 if ((uint)requestedLength > (uint)(spanRealLength - requestedOffset))
154 {
155 return false;
156 }
157 }
158 else if ((ulong)(uint)spanRealLength < (ulong)((long)(uint)requestedOffset + (long)(uint)requestedLength))
159 {
160 return false;
161 }
162 return true;
163 }
164}
static readonly bool IsLittleEndian
static sbyte ReverseEndianness(sbyte value)
static bool TryWriteBytes(Span< byte > span, byte a, byte b, byte c, byte d)
static bool TryWriteBytes(Span< byte > span, byte a, byte b, byte c, byte d, byte e)
static bool IsValidIndex< T >(ReadOnlySpan< T > span, int index)
static bool TryWriteUInt64LittleEndian(Span< byte > span, int offset, ulong value)
static bool AreValidIndexAndLength(int spanRealLength, int requestedOffset, int requestedLength)
static bool TryWriteChars(Span< char > span, char a, char b, char c, char d, char e)
static bool TryWriteChars(Span< char > span, char a, char b, char c, char d, char e, char f)
static bool TryWriteBytes(Span< byte > span, byte a, byte b, byte c, byte d, byte e, byte f)
static bool TryWriteChars(Span< char > span, char a, char b, char c, char d)
static int Size
Definition IntPtr.cs:21
int Length
Definition Span.cs:70