-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUInt128Be.cs
More file actions
606 lines (511 loc) · 26.9 KB
/
Copy pathUInt128Be.cs
File metadata and controls
606 lines (511 loc) · 26.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
using System;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Stardust.Utilities
{
/// <summary>
/// Big-endian 128-bit unsigned integer type.
/// Stores bytes in network byte order (most significant byte first).
/// </summary>
[Serializable]
[TypeConverter(typeof(UInt128BeTypeConverter))]
[StructLayout(LayoutKind.Explicit, Size = 16)]
public struct UInt128Be : IComparable, IComparable<UInt128Be>, IEquatable<UInt128Be>,
IFormattable, ISpanFormattable, IParsable<UInt128Be>, ISpanParsable<UInt128Be>
{
[FieldOffset(0)] internal UInt64Be hi;
[FieldOffset(8)] internal UInt64Be lo;
/// <summary>
/// Creates a big-endian 128-bit unsigned integer from two UInt64Be values.
/// </summary>
/// <param name="hi">The high 64 bits.</param>
/// <param name="lo">The low 64 bits.</param>
public UInt128Be(UInt64Be hi, UInt64Be lo)
{
this.hi = hi;
this.lo = lo;
}
/// <summary>
/// Creates a big-endian 128-bit unsigned integer from an IList of bytes.
/// </summary>
/// <param name="bytes">Source bytes (must have at least 16 bytes from offset).</param>
/// <param name="offset">Starting offset.</param>
/// <exception cref="ArgumentException">If list is too short.</exception>
[Obsolete("IList<byte> overloads are deprecated. Use a byte[] or ReadOnlySpan<byte> overload instead.")]
public UInt128Be(IList<byte> bytes, int offset = 0)
{
if (bytes.Count - offset < 16)
{
throw new ArgumentException("List is too short");
}
hi = new UInt64Be(bytes, offset);
lo = new UInt64Be(bytes, offset + 8);
}
/// <summary>
/// Creates a big-endian 128-bit unsigned integer from a ReadOnlySpan.
/// </summary>
/// <param name="bytes">Source span (must have at least 16 bytes).</param>
/// <exception cref="ArgumentException">If span is too short.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public UInt128Be(ReadOnlySpan<byte> bytes)
{
if (bytes.Length < 16)
{
throw new ArgumentException("Span must have at least 16 bytes", nameof(bytes));
}
hi = new UInt64Be(bytes);
lo = new UInt64Be(bytes.Slice(8));
}
/// <summary>
/// Creates a big-endian 128-bit unsigned integer from a ReadOnlySpan whose byte order is specified.
/// </summary>
/// <param name="bytes">Source span (must have at least 16 bytes).</param>
/// <param name="isBigEndian">If <see langword="true"/> (default) the source is interpreted as big-endian; if <see langword="false"/> it is interpreted as little-endian and reversed during storage.</param>
/// <exception cref="ArgumentException">If span is too short.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public UInt128Be(ReadOnlySpan<byte> bytes, bool isBigEndian = true)
{
if (bytes.Length < 16)
{
throw new ArgumentException("Span must have at least 16 bytes", nameof(bytes));
}
Unsafe.SkipInit(out this);
Span<byte> dst = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref this, 1));
if (isBigEndian)
{
bytes[..16].CopyTo(dst);
}
else
{
for (int i = 0; i < 16; i++) dst[i] = bytes[15 - i];
}
}
/// <summary>
/// Creates a big-endian 128-bit unsigned integer from a ReadOnlySpan at the given offset.
/// </summary>
/// <param name="bytes">Source span (must have at least 16 bytes starting from <paramref name="offset"/>).</param>
/// <param name="offset">Starting offset in the span.</param>
/// <param name="isBigEndian">If <see langword="true"/> (default) the source is interpreted as big-endian; if <see langword="false"/> it is interpreted as little-endian and reversed during storage.</param>
/// <exception cref="ArgumentException">If span is too short from the given offset.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public UInt128Be(ReadOnlySpan<byte> bytes, int offset, bool isBigEndian = true)
{
if (bytes.Length - offset < 16)
{
throw new ArgumentException("Span must have at least 16 bytes", nameof(bytes));
}
Unsafe.SkipInit(out this);
Span<byte> dst = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref this, 1));
if (isBigEndian)
{
bytes.Slice(offset, 16).CopyTo(dst);
}
else
{
for (int i = 0; i < 16; i++) dst[i] = bytes[offset + 15 - i];
}
}
/// <summary>
/// Creates a big-endian 128-bit unsigned integer from a byte array whose byte order is specified.
/// </summary>
/// <param name="bytes">Source byte array (must have at least 16 bytes).</param>
/// <param name="isBigEndian">If <see langword="true"/> (default) the source is interpreted as big-endian; if <see langword="false"/> it is interpreted as little-endian and reversed during storage.</param>
/// <exception cref="ArgumentException">If array is too short.</exception>
public UInt128Be(byte[] bytes, bool isBigEndian = true) : this(new ReadOnlySpan<byte>(bytes), isBigEndian) { }
/// <summary>
/// Creates a big-endian 128-bit unsigned integer from a byte array at the given offset.
/// </summary>
/// <param name="bytes">Source byte array (must have at least 16 bytes starting from <paramref name="offset"/>).</param>
/// <param name="offset">Starting offset in the array.</param>
/// <param name="isBigEndian">If <see langword="true"/> (default) the source is interpreted as big-endian; if <see langword="false"/> it is interpreted as little-endian and reversed during storage.</param>
/// <exception cref="ArgumentException">If array is too short.</exception>
public UInt128Be(byte[] bytes, int offset, bool isBigEndian = true) : this(new ReadOnlySpan<byte>(bytes), offset, isBigEndian) { }
/// <summary>
/// Creates a big-endian 128-bit unsigned integer from a native UInt128.
/// </summary>
/// <param name="num">The native value.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public UInt128Be(UInt128 num)
{
hi = new UInt64Be((ulong)(num >> 64));
lo = new UInt64Be((ulong)num);
}
/// <summary>
/// Creates a big-endian 128-bit unsigned integer from a native ulong.
/// </summary>
/// <param name="num">The native value.</param>
public UInt128Be(ulong num)
{
hi = new UInt64Be(0UL);
lo = new UInt64Be(num);
}
/// <summary>
/// Writes the bytes to an IList in the specified byte order.
/// </summary>
/// <param name="bytes">Destination list.</param>
/// <param name="offset">Starting offset.</param>
/// <param name="isBigEndian">If <see langword="true"/> (default) bytes are written big-endian; if <see langword="false"/> they are written little-endian.</param>
[Obsolete("IList<byte> overloads are deprecated. Use WriteTo(byte[], int, bool) or WriteTo(Span<byte>, int, bool) instead.")]
public readonly void ToBytes(IList<byte> bytes, int offset = 0, bool isBigEndian = true)
{
Span<byte> buf = stackalloc byte[16];
WriteTo(buf, 0, isBigEndian);
for (int i = 0; i < 16; i++) bytes[offset + i] = buf[i];
}
/// <summary>
/// Writes the bytes to a Span in the specified byte order.
/// </summary>
/// <param name="destination">Destination span (must have at least 16 bytes starting from <paramref name="offset"/>).</param>
/// <param name="offset">Starting offset in the destination span.</param>
/// <param name="isBigEndian">If <see langword="true"/> (default) bytes are written big-endian; if <see langword="false"/> they are written little-endian.</param>
/// <exception cref="ArgumentException">If span is too short.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void WriteTo(Span<byte> destination, int offset = 0, bool isBigEndian = true)
{
if (destination.Length - offset < 16)
{
throw new ArgumentException("Destination span must have at least 16 bytes", nameof(destination));
}
ReadOnlySpan<byte> src = MemoryMarshal.AsBytes(MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in this), 1));
if (isBigEndian)
{
src.CopyTo(destination.Slice(offset));
}
else
{
for (int i = 0; i < 16; i++) destination[offset + i] = src[15 - i];
}
}
/// <summary>
/// Writes the bytes to a byte array in the specified byte order.
/// </summary>
/// <param name="destination">Destination byte array (must have at least 16 bytes starting from <paramref name="offset"/>).</param>
/// <param name="offset">Starting offset in the array.</param>
/// <param name="isBigEndian">If <see langword="true"/> (default) bytes are written big-endian; if <see langword="false"/> they are written little-endian.</param>
/// <exception cref="ArgumentException">If array is too short.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void WriteTo(byte[] destination, int offset = 0, bool isBigEndian = true)
=> WriteTo(new Span<byte>(destination), offset, isBigEndian);
/// <summary>
/// Tries to write the bytes to a Span in the specified byte order.
/// </summary>
/// <param name="destination">Destination span.</param>
/// <param name="offset">Starting offset in the destination span.</param>
/// <param name="isBigEndian">If <see langword="true"/> (default) bytes are written big-endian; if <see langword="false"/> they are written little-endian.</param>
/// <returns>True if successful, false if span is too short.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool TryWriteTo(Span<byte> destination, int offset = 0, bool isBigEndian = true)
{
if (destination.Length - offset < 16)
return false;
ReadOnlySpan<byte> src = MemoryMarshal.AsBytes(MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in this), 1));
if (isBigEndian)
{
src.CopyTo(destination.Slice(offset));
}
else
{
for (int i = 0; i < 16; i++) destination[offset + i] = src[15 - i];
}
return true;
}
/// <summary>
/// Tries to write the bytes to a byte array in the specified byte order.
/// </summary>
/// <param name="destination">Destination byte array.</param>
/// <param name="offset">Starting offset in the array.</param>
/// <param name="isBigEndian">If <see langword="true"/> (default) bytes are written big-endian; if <see langword="false"/> they are written little-endian.</param>
/// <returns>True if successful, false if array is too short.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool TryWriteTo(byte[] destination, int offset = 0, bool isBigEndian = true)
=> TryWriteTo(new Span<byte>(destination), offset, isBigEndian);
/// <summary>
/// Reads a <see cref="UInt128Be"/> from a ReadOnlySpan in the specified byte order.
/// </summary>
/// <param name="source">Source span.</param>
/// <param name="offset">Starting offset in the source span.</param>
/// <param name="isBigEndian">If <see langword="true"/> (default) the source is interpreted as big-endian; if <see langword="false"/> it is interpreted as little-endian.</param>
/// <returns>The parsed value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128Be ReadFrom(ReadOnlySpan<byte> source, int offset = 0, bool isBigEndian = true) => new(source, offset, isBigEndian);
/// <summary>
/// Reads a <see cref="UInt128Be"/> from a byte array in the specified byte order.
/// </summary>
/// <param name="source">Source byte array.</param>
/// <param name="offset">Starting offset in the array.</param>
/// <param name="isBigEndian">If <see langword="true"/> (default) the source is interpreted as big-endian; if <see langword="false"/> it is interpreted as little-endian.</param>
/// <returns>The parsed value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128Be ReadFrom(byte[] source, int offset = 0, bool isBigEndian = true) => new(new ReadOnlySpan<byte>(source), offset, isBigEndian);
#region Operators
/// <summary>Returns the value unchanged.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128Be operator +(UInt128Be a) => a;
/// <summary>Negates the value.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128Be operator -(UInt128Be a) => new((UInt128)(-(Int128)(UInt128)a));
/// <summary>Adds two values.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128Be operator +(UInt128Be a, UInt128Be b)
=> new((UInt128)a + (UInt128)b);
/// <summary>Subtracts two values.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128Be operator -(UInt128Be a, UInt128Be b)
=> new((UInt128)a - (UInt128)b);
/// <summary>Multiplies two values.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128Be operator *(UInt128Be a, UInt128Be b)
=> new((UInt128)a * (UInt128)b);
/// <summary>Divides two values.</summary>
/// <exception cref="DivideByZeroException">The divisor is zero.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128Be operator /(UInt128Be a, UInt128Be b)
{
if ((UInt128)b == 0)
{
throw new DivideByZeroException();
}
return new((UInt128)a / (UInt128)b);
}
/// <summary>Computes the remainder of dividing two values.</summary>
/// <exception cref="DivideByZeroException">The divisor is zero.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128Be operator %(UInt128Be a, UInt128Be b)
{
if ((UInt128)b == 0)
{
throw new DivideByZeroException();
}
return new((UInt128)a % (UInt128)b);
}
/// <summary>Determines whether the left operand is greater than the right.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator >(UInt128Be a, UInt128Be b) => (UInt128)a > (UInt128)b;
/// <summary>Determines whether the left operand is less than the right.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator <(UInt128Be a, UInt128Be b) => (UInt128)a < (UInt128)b;
/// <summary>Determines whether the left operand is greater than or equal to the right.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator >=(UInt128Be a, UInt128Be b) => (UInt128)a >= (UInt128)b;
/// <summary>Determines whether the left operand is less than or equal to the right.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator <=(UInt128Be a, UInt128Be b) => (UInt128)a <= (UInt128)b;
/// <summary>Determines whether two values are equal.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(UInt128Be a, UInt128Be b) => (UInt128)a == (UInt128)b;
/// <summary>Determines whether two values are not equal.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(UInt128Be a, UInt128Be b) => (UInt128)a != (UInt128)b;
/// <summary>Computes the bitwise AND of two values.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128Be operator &(UInt128Be a, UInt128Be b)
{
Unsafe.As<UInt128Be, ulong>(ref a) &= Unsafe.As<UInt128Be, ulong>(ref b);
Unsafe.Add(ref Unsafe.As<UInt128Be, ulong>(ref a), 1) &= Unsafe.Add(ref Unsafe.As<UInt128Be, ulong>(ref b), 1);
return a;
}
/// <summary>Computes the bitwise OR of two values.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128Be operator |(UInt128Be a, UInt128Be b)
{
Unsafe.As<UInt128Be, ulong>(ref a) |= Unsafe.As<UInt128Be, ulong>(ref b);
Unsafe.Add(ref Unsafe.As<UInt128Be, ulong>(ref a), 1) |= Unsafe.Add(ref Unsafe.As<UInt128Be, ulong>(ref b), 1);
return a;
}
/// <summary>Computes the bitwise XOR of two values.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128Be operator ^(UInt128Be a, UInt128Be b)
{
Unsafe.As<UInt128Be, ulong>(ref a) ^= Unsafe.As<UInt128Be, ulong>(ref b);
Unsafe.Add(ref Unsafe.As<UInt128Be, ulong>(ref a), 1) ^= Unsafe.Add(ref Unsafe.As<UInt128Be, ulong>(ref b), 1);
return a;
}
/// <summary>Computes the bitwise complement of a value.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128Be operator ~(UInt128Be a)
{
Unsafe.As<UInt128Be, ulong>(ref a) = ~Unsafe.As<UInt128Be, ulong>(ref a);
Unsafe.Add(ref Unsafe.As<UInt128Be, ulong>(ref a), 1) = ~Unsafe.Add(ref Unsafe.As<UInt128Be, ulong>(ref a), 1);
return a;
}
/// <summary>Shifts a value right by the specified number of bits.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128Be operator >>(UInt128Be a, int b) => new((UInt128)a >> b);
/// <summary>Performs an unsigned (logical) right shift. For unsigned types this is identical to <c>>></c>.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128Be operator >>>(UInt128Be a, int b) => new((UInt128)a >> b);
/// <summary>Shifts a value left by the specified number of bits.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128Be operator <<(UInt128Be a, int b) => new((UInt128)a << b);
/// <summary>Increments the value by one.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128Be operator ++(UInt128Be a) => new((UInt128)a + 1);
/// <summary>Decrements the value by one.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128Be operator --(UInt128Be a) => new((UInt128)a - 1);
#endregion
#region Conversions
/// <summary>Implicitly converts a <see cref="UInt128Be"/> to a <see cref="UInt128"/>.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator UInt128(UInt128Be a)
{
return ((UInt128)(ulong)a.hi << 64) | (ulong)a.lo;
}
/// <summary>Implicitly converts a <see cref="UInt128"/> to a <see cref="UInt128Be"/>.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator UInt128Be(UInt128 a) => new(a);
/// <summary>Explicitly converts a <see cref="UInt128Be"/> to a <see cref="ulong"/>.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator ulong(UInt128Be a) => (ulong)(UInt128)a;
/// <summary>Explicitly converts a <see cref="UInt128Be"/> to an <see cref="Int128"/>.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator Int128(UInt128Be a) => (Int128)(UInt128)a;
/// <summary>Widening conversion from a 64-bit big-endian unsigned value.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator UInt128Be(UInt64Be a) => new(new UInt64Be(0UL), a);
/// <summary>Widening conversion from a 32-bit big-endian unsigned value.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator UInt128Be(UInt32Be a) => new(new UInt64Be(0UL), new UInt64Be((uint)a));
/// <summary>Narrowing conversion to a 64-bit big-endian unsigned value.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator UInt64Be(UInt128Be a) => a.lo;
/// <summary>Narrowing conversion to a 32-bit big-endian unsigned value.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator UInt32Be(UInt128Be a) => (UInt32Be)a.lo;
#endregion
#region Parsing and Formatting
/// <summary>
/// Parses a string to a UInt128Be.
/// </summary>
/// <param name="s">The string to parse.</param>
/// <param name="style">The number style.</param>
/// <returns>The parsed value.</returns>
public static UInt128Be Parse(string s, NumberStyles style = NumberStyles.Integer)
{
if (style == NumberStyles.HexNumber)
{
s = s.ToLower().Replace("0x", "");
}
return new UInt128Be(UInt128.Parse(s, style));
}
/// <summary>
/// Parses a string to a UInt128Be 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 UInt128Be Parse(string s, IFormatProvider? provider)
{
return new UInt128Be(UInt128.Parse(s, provider));
}
/// <summary>
/// Parses a span of characters to a UInt128Be.
/// </summary>
/// <param name="s">The characters to parse.</param>
/// <param name="provider">The format provider.</param>
/// <returns>The parsed value.</returns>
public static UInt128Be Parse(ReadOnlySpan<char> s, IFormatProvider? provider)
{
return new UInt128Be(UInt128.Parse(s, provider));
}
/// <summary>
/// Tries to parse a string to a UInt128Be.
/// </summary>
/// <param name="s">The string to parse.</param>
/// <param name="provider">The format provider.</param>
/// <param name="result">The parsed result.</param>
/// <returns><see langword="true"/> if parsing succeeds; otherwise, <see langword="false"/>.</returns>
public static bool TryParse(string? s, IFormatProvider? provider, out UInt128Be result)
{
if (UInt128.TryParse(s, provider, out UInt128 value))
{
result = new UInt128Be(value);
return true;
}
result = default;
return false;
}
/// <summary>
/// Tries to parse a span of characters to a UInt128Be.
/// </summary>
/// <param name="s">The characters to parse.</param>
/// <param name="provider">The format provider.</param>
/// <param name="result">The parsed result.</param>
/// <returns><see langword="true"/> if parsing succeeds; otherwise, <see langword="false"/>.</returns>
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out UInt128Be result)
{
if (UInt128.TryParse(s, provider, out UInt128 value))
{
result = new UInt128Be(value);
return true;
}
result = default;
return false;
}
/// <summary>
/// Returns a string representation of the value.
/// </summary>
/// <returns>The formatted string.</returns>
public override readonly string ToString() => $"0x{(UInt128)this:x32}";
/// <summary>
/// Returns a string representation of the value using the specified format.
/// </summary>
/// <param name="format">The format string.</param>
/// <param name="formatProvider">The format provider.</param>
/// <returns>The formatted string.</returns>
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
return ((UInt128)this).ToString(format, formatProvider);
}
/// <summary>
/// Tries to format the value into the destination span.
/// </summary>
/// <param name="destination">The destination span.</param>
/// <param name="charsWritten">The number of characters written.</param>
/// <param name="format">The format string.</param>
/// <param name="provider">The format provider.</param>
/// <returns><see langword="true"/> if formatting succeeds; otherwise, <see langword="false"/>.</returns>
public readonly bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider)
{
return ((UInt128)this).TryFormat(destination, out charsWritten, format, provider);
}
#endregion
#region Comparison and Equality
/// <inheritdoc/>
public readonly int CompareTo(object? obj)
{
if (obj == null)
{
throw new ArgumentException("Object is null");
}
return CompareTo((UInt128Be)obj);
}
/// <inheritdoc/>
public readonly int CompareTo(UInt128Be other)
{
return ((UInt128)this).CompareTo((UInt128)other);
}
/// <inheritdoc/>
public override readonly bool Equals(object? obj)
{
if (obj == null)
{
return false;
}
return Equals((UInt128Be)obj);
}
/// <inheritdoc/>
public readonly bool Equals(UInt128Be other)
{
return this == other;
}
/// <inheritdoc/>
public override readonly int GetHashCode()
{
return ((UInt128)this).GetHashCode();
}
#endregion
}
}