-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInt32Le.cs
More file actions
370 lines (340 loc) · 21.9 KB
/
Int32Le.cs
File metadata and controls
370 lines (340 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
using System;
using System.Buffers.Binary;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Stardust.Utilities
{
/// <summary>
/// Little-endian 32-bit signed integer type.
/// Stores bytes in little-endian order (least significant byte first).
/// </summary>
[Serializable]
[TypeConverter(typeof(Int32LeTypeConverter))]
[StructLayout(LayoutKind.Explicit, Size = 4)]
public struct Int32Le : IComparable, IComparable<Int32Le>, IEquatable<Int32Le>,
IFormattable, ISpanFormattable, IParsable<Int32Le>, ISpanParsable<Int32Le>
{
[FieldOffset(0)] internal UInt16Le lo;
[FieldOffset(2)] internal UInt16Le hi;
/// <summary>Initializes a new <see cref="Int32Le"/> from an <see cref="int"/> value.</summary>
/// <param name="num">The value to store.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Int32Le(int num)
{
Unsafe.SkipInit(out this);
#if BIG_ENDIAN
Unsafe.As<Int32Le, int>(ref this) = BinaryPrimitives.ReverseEndianness(num);
#else
Unsafe.As<Int32Le, int>(ref this) = num;
#endif
}
/// <summary>Initializes a new <see cref="Int32Le"/> from a read-only byte span.</summary>
/// <param name="bytes">A span containing at least 4 bytes.</param>
/// <exception cref="ArgumentException"><paramref name="bytes"/> has fewer than 4 bytes.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Int32Le(ReadOnlySpan<byte> bytes)
{
if (bytes.Length < 4) throw new ArgumentException("Span must have at least 4 bytes", nameof(bytes));
lo = new UInt16Le(bytes);
hi = new UInt16Le(bytes.Slice(2));
}
/// <summary>
/// Initializes a new <see cref="Int32Le"/> from a read-only byte span whose byte order is specified.
/// </summary>
/// <param name="bytes">Source span (must have at least 4 bytes).</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) the source is interpreted as little-endian; if <see langword="true"/> it is interpreted as big-endian and reversed during storage.</param>
/// <exception cref="ArgumentException">If span is too short.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Int32Le(ReadOnlySpan<byte> bytes, bool isBigEndian = false)
{
if (bytes.Length < 4) throw new ArgumentException("Span must have at least 4 bytes", nameof(bytes));
Unsafe.SkipInit(out this);
Span<byte> dst = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref this, 1));
if (isBigEndian)
{
for (int i = 0; i < 4; i++) dst[i] = bytes[3 - i];
}
else
{
bytes[..4].CopyTo(dst);
}
}
/// <summary>
/// Initializes a new <see cref="Int32Le"/> from a read-only byte span at the given offset.
/// </summary>
/// <param name="bytes">Source span (must have at least 4 bytes starting from <paramref name="offset"/>).</param>
/// <param name="offset">Starting offset in the span.</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) the source is interpreted as little-endian; if <see langword="true"/> it is interpreted as big-endian and reversed during storage.</param>
/// <exception cref="ArgumentException">If span is too short from the given offset.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Int32Le(ReadOnlySpan<byte> bytes, int offset, bool isBigEndian = false)
{
if (bytes.Length - offset < 4) throw new ArgumentException("Span must have at least 4 bytes", nameof(bytes));
Unsafe.SkipInit(out this);
Span<byte> dst = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref this, 1));
if (isBigEndian)
{
for (int i = 0; i < 4; i++) dst[i] = bytes[offset + 3 - i];
}
else
{
bytes.Slice(offset, 4).CopyTo(dst);
}
}
/// <summary>Initializes a new <see cref="Int32Le"/> from a byte array whose byte order is specified.</summary>
/// <param name="bytes">Source byte array (must have at least 4 bytes).</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) the source is interpreted as little-endian; if <see langword="true"/> it is interpreted as big-endian and reversed during storage.</param>
/// <exception cref="ArgumentException">The array is too short.</exception>
public Int32Le(byte[] bytes, bool isBigEndian = false) : this(new ReadOnlySpan<byte>(bytes), isBigEndian) { }
/// <summary>Initializes a new <see cref="Int32Le"/> from a byte array at the given offset.</summary>
/// <param name="bytes">Source byte array (must have at least 4 bytes starting from <paramref name="offset"/>).</param>
/// <param name="offset">The zero-based offset into <paramref name="bytes"/>.</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) the source is interpreted as little-endian; if <see langword="true"/> it is interpreted as big-endian and reversed during storage.</param>
/// <exception cref="ArgumentException">The array is too short for the given offset.</exception>
public Int32Le(byte[] bytes, int offset, bool isBigEndian = false) : this(new ReadOnlySpan<byte>(bytes), offset, isBigEndian) { }
/// <summary>Writes the value to a byte array in the specified byte order.</summary>
/// <param name="bytes">The destination byte array.</param>
/// <param name="offset">The zero-based offset into <paramref name="bytes"/>.</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) bytes are written little-endian; if <see langword="true"/> they are written big-endian.</param>
[Obsolete("Use WriteTo(byte[], int, bool) instead.")]
public readonly void ToBytes(byte[] bytes, int offset = 0, bool isBigEndian = false)
=> WriteTo(new Span<byte>(bytes), offset, isBigEndian);
/// <summary>Writes the value to a destination span in the specified byte order.</summary>
/// <param name="destination">A span with at least 4 bytes of space starting from <paramref name="offset"/>.</param>
/// <param name="offset">The zero-based offset into <paramref name="destination"/>.</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) bytes are written little-endian; if <see langword="true"/> they are written big-endian.</param>
/// <exception cref="ArgumentException"><paramref name="destination"/> is too short.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void WriteTo(Span<byte> destination, int offset = 0, bool isBigEndian = false)
{
if (destination.Length - offset < 4) throw new ArgumentException("Destination span must have at least 4 bytes", nameof(destination));
if (isBigEndian)
{
destination[offset + 0] = hi.hi;
destination[offset + 1] = hi.lo;
destination[offset + 2] = lo.hi;
destination[offset + 3] = lo.lo;
}
else
{
lo.WriteTo(destination, offset);
hi.WriteTo(destination, offset + 2);
}
}
/// <summary>Writes the value to a byte array in the specified byte order.</summary>
/// <param name="destination">Destination byte array (must have at least 4 bytes starting from <paramref name="offset"/>).</param>
/// <param name="offset">The zero-based offset into <paramref name="destination"/>.</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) bytes are written little-endian; if <see langword="true"/> they are written big-endian.</param>
/// <exception cref="ArgumentException">The array is too short.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void WriteTo(byte[] destination, int offset = 0, bool isBigEndian = false)
=> WriteTo(new Span<byte>(destination), offset, isBigEndian);
/// <summary>Attempts to write the value to a destination span in the specified byte order.</summary>
/// <param name="destination">The destination span.</param>
/// <param name="offset">The zero-based offset into <paramref name="destination"/>.</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) bytes are written little-endian; if <see langword="true"/> they are written big-endian.</param>
/// <returns><see langword="true"/> if the span was large enough; otherwise <see langword="false"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool TryWriteTo(Span<byte> destination, int offset = 0, bool isBigEndian = false)
{
if (destination.Length - offset < 4)
return false;
if (isBigEndian)
{
destination[offset + 0] = hi.hi;
destination[offset + 1] = hi.lo;
destination[offset + 2] = lo.hi;
destination[offset + 3] = lo.lo;
}
else
{
lo.WriteTo(destination, offset);
hi.WriteTo(destination, offset + 2);
}
return true;
}
/// <summary>Attempts to write the value to a byte array in the specified byte order.</summary>
/// <param name="destination">Destination byte array.</param>
/// <param name="offset">The zero-based offset into <paramref name="destination"/>.</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) bytes are written little-endian; if <see langword="true"/> they are written big-endian.</param>
/// <returns><see langword="true"/> if the array was large enough; otherwise <see langword="false"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool TryWriteTo(byte[] destination, int offset = 0, bool isBigEndian = false)
=> TryWriteTo(new Span<byte>(destination), offset, isBigEndian);
/// <summary>Reads an <see cref="Int32Le"/> from a read-only byte span in the specified byte order.</summary>
/// <param name="source">A span containing at least 4 bytes.</param>
/// <param name="offset">The zero-based offset into <paramref name="source"/>.</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) the source is interpreted as little-endian; if <see langword="true"/> it is interpreted as big-endian.</param>
/// <returns>The value read from the span.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32Le ReadFrom(ReadOnlySpan<byte> source, int offset = 0, bool isBigEndian = false) => new(source, offset, isBigEndian);
/// <summary>Reads an <see cref="Int32Le"/> from a byte array in the specified byte order.</summary>
/// <param name="source">A byte array containing at least 4 bytes.</param>
/// <param name="offset">The zero-based offset into <paramref name="source"/>.</param>
/// <param name="isBigEndian">If <see langword="false"/> (default) the source is interpreted as little-endian; if <see langword="true"/> it is interpreted as big-endian.</param>
/// <returns>The value read from the array.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32Le ReadFrom(byte[] source, int offset = 0, bool isBigEndian = false) => new(new ReadOnlySpan<byte>(source), offset, isBigEndian);
/// <summary>Parses a string into an <see cref="Int32Le"/>.</summary>
/// <param name="s">The string to parse.</param>
/// <param name="style">The number style to use.</param>
/// <returns>The parsed value.</returns>
public static Int32Le Parse(string s, NumberStyles style = NumberStyles.Integer)
{
if (style == NumberStyles.HexNumber) s = s.ToLower().Replace("0x", "");
return new(int.Parse(s, style));
}
/// <summary>Parses a string into an <see cref="Int32Le"/> using the specified format provider.</summary>
/// <param name="s">The string to parse.</param>
/// <param name="provider">The format provider.</param>
/// <returns>The parsed value.</returns>
public static Int32Le Parse(string s, IFormatProvider? provider) => new(int.Parse(s, provider));
/// <summary>Parses a character span into an <see cref="Int32Le"/> using the specified format provider.</summary>
/// <param name="s">The character span to parse.</param>
/// <param name="provider">The format provider.</param>
/// <returns>The parsed value.</returns>
public static Int32Le Parse(ReadOnlySpan<char> s, IFormatProvider? provider) => new(int.Parse(s, provider));
/// <summary>Tries to parse a string into an <see cref="Int32Le"/>.</summary>
/// <param name="s">The string to parse.</param>
/// <param name="provider">The format provider.</param>
/// <param name="result">When successful, the parsed value.</param>
/// <returns><see langword="true"/> if parsing succeeded; otherwise <see langword="false"/>.</returns>
public static bool TryParse(string? s, IFormatProvider? provider, out Int32Le result)
{
if (int.TryParse(s, provider, out int v)) { result = new(v); return true; }
result = default; return false;
}
/// <summary>Tries to parse a character span into an <see cref="Int32Le"/>.</summary>
/// <param name="s">The character span to parse.</param>
/// <param name="provider">The format provider.</param>
/// <param name="result">When successful, the parsed value.</param>
/// <returns><see langword="true"/> if parsing succeeded; otherwise <see langword="false"/>.</returns>
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out Int32Le result)
{
if (int.TryParse(s, provider, out int v)) { result = new(v); return true; }
result = default; return false;
}
/// <inheritdoc/>
public override readonly string ToString() => ((int)this).ToString();
/// <inheritdoc/>
public readonly string ToString(string? format, IFormatProvider? formatProvider) => ((int)this).ToString(format, formatProvider);
/// <inheritdoc/>
public readonly bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) =>
((int)this).TryFormat(destination, out charsWritten, format, provider);
/// <inheritdoc/>
public readonly int CompareTo(object? obj) { if (obj == null) throw new ArgumentException("obj is null"); return CompareTo((Int32Le)obj); }
/// <inheritdoc/>
public readonly int CompareTo(Int32Le other) => ((int)this).CompareTo((int)other);
/// <inheritdoc/>
public override readonly bool Equals(object? obj) => obj != null && Equals((Int32Le)obj);
/// <inheritdoc/>
public readonly bool Equals(Int32Le other) => this == other;
/// <inheritdoc/>
public override readonly int GetHashCode() => ((int)this).GetHashCode();
#region Operators
/// <summary>Returns the value of the operand (unary plus).</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32Le operator +(Int32Le a) => a;
/// <summary>Negates the value (unary minus).</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32Le operator -(Int32Le a) => new(-(int)a);
/// <summary>Adds two values.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32Le operator +(Int32Le a, Int32Le b) => new((int)a + (int)b);
/// <summary>Subtracts two values.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32Le operator -(Int32Le a, Int32Le b) => new((int)a - (int)b);
/// <summary>Determines whether the left operand is greater than the right.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator >(Int32Le a, Int32Le b) => (int)a > (int)b;
/// <summary>Determines whether the left operand is less than the right.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator <(Int32Le a, Int32Le b) => (int)a < (int)b;
/// <summary>Determines whether the left operand is greater than or equal to the right.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator >=(Int32Le a, Int32Le b) => (int)a >= (int)b;
/// <summary>Determines whether the left operand is less than or equal to the right.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator <=(Int32Le a, Int32Le b) => (int)a <= (int)b;
/// <summary>Determines whether two values are equal.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Int32Le a, Int32Le b) => (int)a == (int)b;
/// <summary>Determines whether two values are not equal.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Int32Le a, Int32Le b) => (int)a != (int)b;
/// <summary>Multiplies two values.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32Le operator *(Int32Le a, Int32Le b) => new((int)a * (int)b);
/// <summary>Divides two values.</summary>
/// <exception cref="DivideByZeroException">The divisor is zero.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32Le operator /(Int32Le a, Int32Le b)
{
if ((int)b == 0) throw new DivideByZeroException();
return new((int)a / (int)b);
}
/// <summary>Computes the bitwise AND of two values.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32Le operator &(Int32Le a, Int32Le b) => new((int)a & (int)b);
/// <summary>Computes the bitwise OR of two values.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32Le operator |(Int32Le a, Int32Le b) => new((int)a | (int)b);
/// <summary>Computes the bitwise XOR of two values.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32Le operator ^(Int32Le a, Int32Le b) => new((int)a ^ (int)b);
/// <summary>Computes the bitwise complement of the value.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32Le operator ~(Int32Le a) => new(~(int)a);
/// <summary>Shifts the value right by the specified amount.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32Le operator >>(Int32Le a, int b) => new((int)a >> b);
/// <summary>Performs an unsigned (logical) right shift by the specified amount.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32Le operator >>>(Int32Le a, int b) => new((int)((uint)(int)a >> b));
/// <summary>Shifts the value left by the specified amount.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32Le operator <<(Int32Le a, int b) => new((int)a << b);
/// <summary>Computes the remainder of dividing two values.</summary>
/// <exception cref="DivideByZeroException">The divisor is zero.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32Le operator %(Int32Le a, Int32Le b)
{
if ((int)b == 0) throw new DivideByZeroException();
return new((int)a % (int)b);
}
/// <summary>Increments the value by one.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32Le operator ++(Int32Le a) => new((int)a + 1);
/// <summary>Decrements the value by one.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32Le operator --(Int32Le a) => new((int)a - 1);
#endregion
#region Conversions
/// <summary>Implicitly converts an <see cref="Int32Le"/> to an <see cref="int"/>.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#if BIG_ENDIAN
public static implicit operator int(Int32Le a) => BinaryPrimitives.ReverseEndianness(Unsafe.As<Int32Le, int>(ref a));
#else
public static implicit operator int(Int32Le a) => Unsafe.As<Int32Le, int>(ref a);
#endif
/// <summary>Implicitly converts an <see cref="int"/> to an <see cref="Int32Le"/>.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Int32Le(int a) => new(a);
/// <summary>Implicitly converts an <see cref="Int32Le"/> to a <see cref="long"/>.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator long(Int32Le a) => (int)a;
/// <summary>Explicitly converts an <see cref="Int32Le"/> to a <see cref="uint"/>.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator uint(Int32Le a) => (uint)(int)a;
/// <summary>Sign-extending conversion from a 16-bit little-endian signed value.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator Int32Le(Int16Le v)
{
short value = (short)v;
return new Int32Le(value);
}
#endregion
}
}