Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Guid.cs
Go to the documentation of this file.
8
9namespace System;
10
12[NonVersionable]
13[TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
14public readonly struct Guid : ISpanFormattable, IFormattable, IComparable, IComparable<Guid>, IEquatable<Guid>, IComparisonOperators<Guid, Guid>, IEqualityOperators<Guid, Guid>, ISpanParseable<Guid>, IParseable<Guid>
15{
16 private enum GuidParseThrowStyle : byte
17 {
18 None,
19 All,
21 }
22
23 [StructLayout(LayoutKind.Explicit)]
24 private struct GuidResult
25 {
26 [FieldOffset(0)]
27 internal uint _a;
28
29 [FieldOffset(4)]
30 internal uint _bc;
31
32 [FieldOffset(4)]
33 internal ushort _b;
34
35 [FieldOffset(6)]
36 internal ushort _c;
37
38 [FieldOffset(8)]
39 internal uint _defg;
40
41 [FieldOffset(8)]
42 internal ushort _de;
43
44 [FieldOffset(8)]
45 internal byte _d;
46
47 [FieldOffset(10)]
48 internal ushort _fg;
49
50 [FieldOffset(12)]
51 internal uint _hijk;
52
53 [FieldOffset(16)]
55
57 {
58 this = default(GuidResult);
59 _throwStyle = canThrow;
60 }
61
62 internal readonly void SetFailure(bool overflow, string failureMessageID)
63 {
65 {
66 return;
67 }
68 if (overflow)
69 {
71 {
72 throw new OverflowException(SR.GetResourceString(failureMessageID));
73 }
75 }
76 throw new FormatException(SR.GetResourceString(failureMessageID));
77 }
78
79 [MethodImpl(MethodImplOptions.AggressiveInlining)]
80 public readonly Guid ToGuid()
81 {
82 return Unsafe.As<GuidResult, Guid>(ref Unsafe.AsRef(in this));
83 }
84 }
85
86 public static readonly Guid Empty;
87
88 private readonly int _a;
89
90 private readonly short _b;
91
92 private readonly short _c;
93
94 private readonly byte _d;
95
96 private readonly byte _e;
97
98 private readonly byte _f;
99
100 private readonly byte _g;
101
102 private readonly byte _h;
103
104 private readonly byte _i;
105
106 private readonly byte _j;
107
108 private readonly byte _k;
109
110 public Guid(byte[] b)
111 : this(new ReadOnlySpan<byte>(b ?? throw new ArgumentNullException("b")))
112 {
113 }
114
116 {
117 if (b.Length != 16)
118 {
119 throw new ArgumentException(SR.Format(SR.Arg_GuidArrayCtor, "16"), "b");
120 }
122 this = MemoryMarshal.Read<Guid>(b);
123 }
124
125 [CLSCompliant(false)]
126 public Guid(uint a, ushort b, ushort c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k)
127 {
128 _a = (int)a;
129 _b = (short)b;
130 _c = (short)c;
131 _d = d;
132 _e = e;
133 _f = f;
134 _g = g;
135 _h = h;
136 _i = i;
137 _j = j;
138 _k = k;
139 }
140
141 public Guid(int a, short b, short c, byte[] d)
142 {
143 if (d == null)
144 {
145 throw new ArgumentNullException("d");
146 }
147 if (d.Length != 8)
148 {
149 throw new ArgumentException(SR.Format(SR.Arg_GuidArrayCtor, "8"), "d");
150 }
151 _a = a;
152 _b = b;
153 _c = c;
154 _k = d[7];
155 _d = d[0];
156 _e = d[1];
157 _f = d[2];
158 _g = d[3];
159 _h = d[4];
160 _i = d[5];
161 _j = d[6];
162 }
163
164 public Guid(int a, short b, short c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k)
165 {
166 _a = a;
167 _b = b;
168 _c = c;
169 _d = d;
170 _e = e;
171 _f = f;
172 _g = g;
173 _h = h;
174 _i = i;
175 _j = j;
176 _k = k;
177 }
178
179 public Guid(string g)
180 {
181 if (g == null)
182 {
183 throw new ArgumentNullException("g");
184 }
185 GuidResult result = new GuidResult(GuidParseThrowStyle.All);
186 bool flag = TryParseGuid(g, ref result);
187 this = result.ToGuid();
188 }
189
190 public static Guid Parse(string input)
191 {
192 if (input == null)
193 {
194 throw new ArgumentNullException("input");
195 }
197 }
198
200 {
201 GuidResult result = new GuidResult(GuidParseThrowStyle.AllButOverflow);
202 bool flag = TryParseGuid(input, ref result);
203 return result.ToGuid();
204 }
205
206 public static bool TryParse([NotNullWhen(true)] string? input, out Guid result)
207 {
208 if (input == null)
209 {
210 result = default(Guid);
211 return false;
212 }
213 return TryParse((ReadOnlySpan<char>)input, out result);
214 }
215
216 public static bool TryParse(ReadOnlySpan<char> input, out Guid result)
217 {
218 GuidResult result2 = new GuidResult(GuidParseThrowStyle.None);
219 if (TryParseGuid(input, ref result2))
220 {
221 result = result2.ToGuid();
222 return true;
223 }
224 result = default(Guid);
225 return false;
226 }
227
228 public static Guid ParseExact(string input, string format)
229 {
230 if (input == null)
231 {
232 throw new ArgumentNullException("input");
233 }
234 ReadOnlySpan<char> input2 = input;
235 if (format == null)
236 {
237 throw new ArgumentNullException("format");
238 }
239 return ParseExact(input2, format);
240 }
241
243 {
244 if (format.Length != 1)
245 {
247 }
248 input = input.Trim();
249 GuidResult result = new GuidResult(GuidParseThrowStyle.AllButOverflow);
250 bool flag = (char)(ushort)(format[0] | 0x20) switch
251 {
252 'd' => TryParseExactD(input, ref result),
253 'n' => TryParseExactN(input, ref result),
254 'b' => TryParseExactB(input, ref result),
255 'p' => TryParseExactP(input, ref result),
256 'x' => TryParseExactX(input, ref result),
258 };
259 return result.ToGuid();
260 }
261
262 public static bool TryParseExact([NotNullWhen(true)] string? input, [NotNullWhen(true)] string? format, out Guid result)
263 {
264 if (input == null)
265 {
266 result = default(Guid);
267 return false;
268 }
270 }
271
273 {
274 if (format.Length != 1)
275 {
276 result = default(Guid);
277 return false;
278 }
279 input = input.Trim();
280 GuidResult result2 = new GuidResult(GuidParseThrowStyle.None);
281 bool flag = false;
282 switch ((char)(ushort)(format[0] | 0x20))
283 {
284 case 'd':
285 flag = TryParseExactD(input, ref result2);
286 break;
287 case 'n':
288 flag = TryParseExactN(input, ref result2);
289 break;
290 case 'b':
291 flag = TryParseExactB(input, ref result2);
292 break;
293 case 'p':
294 flag = TryParseExactP(input, ref result2);
295 break;
296 case 'x':
297 flag = TryParseExactX(input, ref result2);
298 break;
299 }
300 if (flag)
301 {
302 result = result2.ToGuid();
303 return true;
304 }
305 result = default(Guid);
306 return false;
307 }
308
309 private static bool TryParseGuid(ReadOnlySpan<char> guidString, ref GuidResult result)
310 {
311 guidString = guidString.Trim();
312 if (guidString.Length == 0)
313 {
314 result.SetFailure(overflow: false, "Format_GuidUnrecognized");
315 return false;
316 }
317 return guidString[0] switch
318 {
319 '(' => TryParseExactP(guidString, ref result),
320 '{' => guidString.Contains('-') ? TryParseExactB(guidString, ref result) : TryParseExactX(guidString, ref result),
321 _ => guidString.Contains('-') ? TryParseExactD(guidString, ref result) : TryParseExactN(guidString, ref result),
322 };
323 }
324
325 private static bool TryParseExactB(ReadOnlySpan<char> guidString, ref GuidResult result)
326 {
327 if (guidString.Length != 38 || guidString[0] != '{' || guidString[37] != '}')
328 {
329 result.SetFailure(overflow: false, "Format_GuidInvLen");
330 return false;
331 }
332 return TryParseExactD(guidString.Slice(1, 36), ref result);
333 }
334
335 private static bool TryParseExactD(ReadOnlySpan<char> guidString, ref GuidResult result)
336 {
337 if (guidString.Length != 36 || guidString[8] != '-' || guidString[13] != '-' || guidString[18] != '-' || guidString[23] != '-')
338 {
339 result.SetFailure(overflow: false, (guidString.Length != 36) ? "Format_GuidInvLen" : "Format_GuidDashes");
340 return false;
341 }
342 Span<byte> span = MemoryMarshal.AsBytes(new Span<GuidResult>(ref result, 1));
343 int invalidIfNegative = 0;
344 span[0] = DecodeByte(guidString[6], guidString[7], ref invalidIfNegative);
345 span[1] = DecodeByte(guidString[4], guidString[5], ref invalidIfNegative);
346 span[2] = DecodeByte(guidString[2], guidString[3], ref invalidIfNegative);
347 span[3] = DecodeByte(guidString[0], guidString[1], ref invalidIfNegative);
348 span[4] = DecodeByte(guidString[11], guidString[12], ref invalidIfNegative);
349 span[5] = DecodeByte(guidString[9], guidString[10], ref invalidIfNegative);
350 span[6] = DecodeByte(guidString[16], guidString[17], ref invalidIfNegative);
351 span[7] = DecodeByte(guidString[14], guidString[15], ref invalidIfNegative);
352 span[8] = DecodeByte(guidString[19], guidString[20], ref invalidIfNegative);
353 span[9] = DecodeByte(guidString[21], guidString[22], ref invalidIfNegative);
354 span[10] = DecodeByte(guidString[24], guidString[25], ref invalidIfNegative);
355 span[11] = DecodeByte(guidString[26], guidString[27], ref invalidIfNegative);
356 span[12] = DecodeByte(guidString[28], guidString[29], ref invalidIfNegative);
357 span[13] = DecodeByte(guidString[30], guidString[31], ref invalidIfNegative);
358 span[14] = DecodeByte(guidString[32], guidString[33], ref invalidIfNegative);
359 span[15] = DecodeByte(guidString[34], guidString[35], ref invalidIfNegative);
360 if (invalidIfNegative >= 0)
361 {
363 {
364 }
365 return true;
366 }
367 if (guidString.IndexOfAny('X', 'x', '+') != -1 && TryCompatParsing(guidString, ref result))
368 {
369 return true;
370 }
371 result.SetFailure(overflow: false, "Format_GuidInvalidChar");
372 return false;
373 static bool TryCompatParsing(ReadOnlySpan<char> guidString, ref GuidResult result)
374 {
375 if (TryParseHex(guidString.Slice(0, 8), out result._a) && TryParseHex(guidString.Slice(9, 4), out var result2))
376 {
377 result._b = (ushort)result2;
378 if (TryParseHex(guidString.Slice(14, 4), out result2))
379 {
380 result._c = (ushort)result2;
381 if (TryParseHex(guidString.Slice(19, 4), out result2))
382 {
384 {
385 }
386 result._de = BinaryPrimitives.ReverseEndianness((ushort)result2);
387 if (TryParseHex(guidString.Slice(24, 4), out result2))
388 {
390 {
391 }
392 result._fg = BinaryPrimitives.ReverseEndianness((ushort)result2);
393 if (Number.TryParseUInt32HexNumberStyle(guidString.Slice(28, 8), NumberStyles.AllowHexSpecifier, out result2) == Number.ParsingStatus.OK)
394 {
396 {
397 }
398 result._hijk = BinaryPrimitives.ReverseEndianness(result2);
399 return true;
400 }
401 }
402 }
403 }
404 }
405 return false;
406 }
407 }
408
409 private static bool TryParseExactN(ReadOnlySpan<char> guidString, ref GuidResult result)
410 {
411 if (guidString.Length != 32)
412 {
413 result.SetFailure(overflow: false, "Format_GuidInvLen");
414 return false;
415 }
416 Span<byte> span = MemoryMarshal.AsBytes(new Span<GuidResult>(ref result, 1));
417 int invalidIfNegative = 0;
418 span[0] = DecodeByte(guidString[6], guidString[7], ref invalidIfNegative);
419 span[1] = DecodeByte(guidString[4], guidString[5], ref invalidIfNegative);
420 span[2] = DecodeByte(guidString[2], guidString[3], ref invalidIfNegative);
421 span[3] = DecodeByte(guidString[0], guidString[1], ref invalidIfNegative);
422 span[4] = DecodeByte(guidString[10], guidString[11], ref invalidIfNegative);
423 span[5] = DecodeByte(guidString[8], guidString[9], ref invalidIfNegative);
424 span[6] = DecodeByte(guidString[14], guidString[15], ref invalidIfNegative);
425 span[7] = DecodeByte(guidString[12], guidString[13], ref invalidIfNegative);
426 span[8] = DecodeByte(guidString[16], guidString[17], ref invalidIfNegative);
427 span[9] = DecodeByte(guidString[18], guidString[19], ref invalidIfNegative);
428 span[10] = DecodeByte(guidString[20], guidString[21], ref invalidIfNegative);
429 span[11] = DecodeByte(guidString[22], guidString[23], ref invalidIfNegative);
430 span[12] = DecodeByte(guidString[24], guidString[25], ref invalidIfNegative);
431 span[13] = DecodeByte(guidString[26], guidString[27], ref invalidIfNegative);
432 span[14] = DecodeByte(guidString[28], guidString[29], ref invalidIfNegative);
433 span[15] = DecodeByte(guidString[30], guidString[31], ref invalidIfNegative);
434 if (invalidIfNegative >= 0)
435 {
437 {
438 }
439 return true;
440 }
441 result.SetFailure(overflow: false, "Format_GuidInvalidChar");
442 return false;
443 }
444
445 private static bool TryParseExactP(ReadOnlySpan<char> guidString, ref GuidResult result)
446 {
447 if (guidString.Length != 38 || guidString[0] != '(' || guidString[37] != ')')
448 {
449 result.SetFailure(overflow: false, "Format_GuidInvLen");
450 return false;
451 }
452 return TryParseExactD(guidString.Slice(1, 36), ref result);
453 }
454
455 private static bool TryParseExactX(ReadOnlySpan<char> guidString, ref GuidResult result)
456 {
457 guidString = EatAllWhitespace(guidString);
458 if (guidString.Length == 0 || guidString[0] != '{')
459 {
460 result.SetFailure(overflow: false, "Format_GuidBrace");
461 return false;
462 }
463 if (!IsHexPrefix(guidString, 1))
464 {
465 result.SetFailure(overflow: false, "Format_GuidHexPrefix");
466 return false;
467 }
468 int num = 3;
469 int num2 = guidString.Slice(num).IndexOf(',');
470 if (num2 <= 0)
471 {
472 result.SetFailure(overflow: false, "Format_GuidComma");
473 return false;
474 }
475 bool overflow = false;
476 if (!TryParseHex(guidString.Slice(num, num2), out result._a, ref overflow) || overflow)
477 {
478 result.SetFailure(overflow, overflow ? "Overflow_UInt32" : "Format_GuidInvalidChar");
479 return false;
480 }
481 if (!IsHexPrefix(guidString, num + num2 + 1))
482 {
483 result.SetFailure(overflow: false, "Format_GuidHexPrefix");
484 return false;
485 }
486 num = num + num2 + 3;
487 num2 = guidString.Slice(num).IndexOf(',');
488 if (num2 <= 0)
489 {
490 result.SetFailure(overflow: false, "Format_GuidComma");
491 return false;
492 }
493 if (!TryParseHex(guidString.Slice(num, num2), out result._b, ref overflow) || overflow)
494 {
495 result.SetFailure(overflow, overflow ? "Overflow_UInt32" : "Format_GuidInvalidChar");
496 return false;
497 }
498 if (!IsHexPrefix(guidString, num + num2 + 1))
499 {
500 result.SetFailure(overflow: false, "Format_GuidHexPrefix");
501 return false;
502 }
503 num = num + num2 + 3;
504 num2 = guidString.Slice(num).IndexOf(',');
505 if (num2 <= 0)
506 {
507 result.SetFailure(overflow: false, "Format_GuidComma");
508 return false;
509 }
510 if (!TryParseHex(guidString.Slice(num, num2), out result._c, ref overflow) || overflow)
511 {
512 result.SetFailure(overflow, overflow ? "Overflow_UInt32" : "Format_GuidInvalidChar");
513 return false;
514 }
515 if ((uint)guidString.Length <= (uint)(num + num2 + 1) || guidString[num + num2 + 1] != '{')
516 {
517 result.SetFailure(overflow: false, "Format_GuidBrace");
518 return false;
519 }
520 num2++;
521 for (int i = 0; i < 8; i++)
522 {
523 if (!IsHexPrefix(guidString, num + num2 + 1))
524 {
525 result.SetFailure(overflow: false, "Format_GuidHexPrefix");
526 return false;
527 }
528 num = num + num2 + 3;
529 if (i < 7)
530 {
531 num2 = guidString.Slice(num).IndexOf(',');
532 if (num2 <= 0)
533 {
534 result.SetFailure(overflow: false, "Format_GuidComma");
535 return false;
536 }
537 }
538 else
539 {
540 num2 = guidString.Slice(num).IndexOf('}');
541 if (num2 <= 0)
542 {
543 result.SetFailure(overflow: false, "Format_GuidBraceAfterLastNumber");
544 return false;
545 }
546 }
547 if (!TryParseHex(guidString.Slice(num, num2), out uint result2, ref overflow) || overflow || result2 > 255)
548 {
549 result.SetFailure(overflow, overflow ? "Overflow_UInt32" : ((result2 > 255) ? "Overflow_Byte" : "Format_GuidInvalidChar"));
550 return false;
551 }
552 Unsafe.Add(ref result._d, i) = (byte)result2;
553 }
554 if (num + num2 + 1 >= guidString.Length || guidString[num + num2 + 1] != '}')
555 {
556 result.SetFailure(overflow: false, "Format_GuidEndBrace");
557 return false;
558 }
559 if (num + num2 + 1 != guidString.Length - 1)
560 {
561 result.SetFailure(overflow: false, "Format_ExtraJunkAtEnd");
562 return false;
563 }
564 return true;
565 }
566
567 [MethodImpl(MethodImplOptions.AggressiveInlining)]
568 private static byte DecodeByte(nuint ch1, nuint ch2, ref int invalidIfNegative)
569 {
571 int num = -1;
572 if (ch1 < (nuint)charToHexLookup.Length)
573 {
574 num = (sbyte)Unsafe.Add(ref MemoryMarshal.GetReference(charToHexLookup), (nint)ch1);
575 }
576 num <<= 4;
577 int num2 = -1;
578 if (ch2 < (nuint)charToHexLookup.Length)
579 {
580 num2 = (sbyte)Unsafe.Add(ref MemoryMarshal.GetReference(charToHexLookup), (nint)ch2);
581 }
582 int num3 = num | num2;
583 invalidIfNegative |= num3;
584 return (byte)num3;
585 }
586
587 private static bool TryParseHex(ReadOnlySpan<char> guidString, out ushort result, ref bool overflow)
588 {
589 uint result2;
590 bool result3 = TryParseHex(guidString, out result2, ref overflow);
591 result = (ushort)result2;
592 return result3;
593 }
594
595 private static bool TryParseHex(ReadOnlySpan<char> guidString, out uint result)
596 {
597 bool overflow = false;
598 return TryParseHex(guidString, out result, ref overflow);
599 }
600
601 private static bool TryParseHex(ReadOnlySpan<char> guidString, out uint result, ref bool overflow)
602 {
603 if (guidString.Length != 0)
604 {
605 if (guidString[0] == '+')
606 {
607 guidString = guidString.Slice(1);
608 }
609 if ((uint)guidString.Length > 1u && guidString[0] == '0' && (guidString[1] | 0x20) == 120)
610 {
611 guidString = guidString.Slice(2);
612 }
613 }
614 int i;
615 for (i = 0; i < guidString.Length && guidString[i] == '0'; i++)
616 {
617 }
618 int num = 0;
619 uint num2 = 0u;
620 for (; i < guidString.Length; i++)
621 {
622 char c = guidString[i];
623 int num3 = HexConverter.FromChar(c);
624 if (num3 == 255)
625 {
626 if (num > 8)
627 {
628 overflow = true;
629 }
630 result = 0u;
631 return false;
632 }
633 num2 = num2 * 16 + (uint)num3;
634 num++;
635 }
636 if (num > 8)
637 {
638 overflow = true;
639 }
640 result = num2;
641 return true;
642 }
643
645 {
646 int i;
647 for (i = 0; i < str.Length && !char.IsWhiteSpace(str[i]); i++)
648 {
649 }
650 if (i == str.Length)
651 {
652 return str;
653 }
654 char[] array = new char[str.Length];
655 int length = 0;
656 if (i > 0)
657 {
658 length = i;
659 str.Slice(0, i).CopyTo(array);
660 }
661 for (; i < str.Length; i++)
662 {
663 char c = str[i];
664 if (!char.IsWhiteSpace(c))
665 {
666 array[length++] = c;
667 }
668 }
669 return new ReadOnlySpan<char>(array, 0, length);
670 }
671
672 private static bool IsHexPrefix(ReadOnlySpan<char> str, int i)
673 {
674 if (i + 1 < str.Length && str[i] == '0')
675 {
676 return (str[i + 1] | 0x20) == 120;
677 }
678 return false;
679 }
680
681 public byte[] ToByteArray()
682 {
683 byte[] array = new byte[16];
685 MemoryMarshal.TryWrite(array, ref Unsafe.AsRef(in this));
686 return array;
687 }
688
690 {
692 return MemoryMarshal.TryWrite(destination, ref Unsafe.AsRef(in this));
693 }
694
695 public override string ToString()
696 {
697 return ToString("D", null);
698 }
699
700 public override int GetHashCode()
701 {
702 ref int reference = ref Unsafe.AsRef(in _a);
703 return reference ^ Unsafe.Add(ref reference, 1) ^ Unsafe.Add(ref reference, 2) ^ Unsafe.Add(ref reference, 3);
704 }
705
706 public override bool Equals([NotNullWhen(true)] object? o)
707 {
708 if (o is Guid right)
709 {
710 return EqualsCore(in this, in right);
711 }
712 return false;
713 }
714
715 public bool Equals(Guid g)
716 {
717 return EqualsCore(in this, in g);
718 }
719
720 private static bool EqualsCore(in Guid left, in Guid right)
721 {
722 ref int reference = ref Unsafe.AsRef(in left._a);
723 ref int reference2 = ref Unsafe.AsRef(in right._a);
724 if (reference == reference2 && Unsafe.Add(ref reference, 1) == Unsafe.Add(ref reference2, 1) && Unsafe.Add(ref reference, 2) == Unsafe.Add(ref reference2, 2))
725 {
726 return Unsafe.Add(ref reference, 3) == Unsafe.Add(ref reference2, 3);
727 }
728 return false;
729 }
730
731 private static int GetResult(uint me, uint them)
732 {
733 if (me >= them)
734 {
735 return 1;
736 }
737 return -1;
738 }
739
740 public int CompareTo(object? value)
741 {
742 if (value == null)
743 {
744 return 1;
745 }
746 if (!(value is Guid guid))
747 {
748 throw new ArgumentException(SR.Arg_MustBeGuid, "value");
749 }
750 if (guid._a != _a)
751 {
752 return GetResult((uint)_a, (uint)guid._a);
753 }
754 if (guid._b != _b)
755 {
756 return GetResult((uint)_b, (uint)guid._b);
757 }
758 if (guid._c != _c)
759 {
760 return GetResult((uint)_c, (uint)guid._c);
761 }
762 if (guid._d != _d)
763 {
764 return GetResult(_d, guid._d);
765 }
766 if (guid._e != _e)
767 {
768 return GetResult(_e, guid._e);
769 }
770 if (guid._f != _f)
771 {
772 return GetResult(_f, guid._f);
773 }
774 if (guid._g != _g)
775 {
776 return GetResult(_g, guid._g);
777 }
778 if (guid._h != _h)
779 {
780 return GetResult(_h, guid._h);
781 }
782 if (guid._i != _i)
783 {
784 return GetResult(_i, guid._i);
785 }
786 if (guid._j != _j)
787 {
788 return GetResult(_j, guid._j);
789 }
790 if (guid._k != _k)
791 {
792 return GetResult(_k, guid._k);
793 }
794 return 0;
795 }
796
797 public int CompareTo(Guid value)
798 {
799 if (value._a != _a)
800 {
801 return GetResult((uint)_a, (uint)value._a);
802 }
803 if (value._b != _b)
804 {
805 return GetResult((uint)_b, (uint)value._b);
806 }
807 if (value._c != _c)
808 {
809 return GetResult((uint)_c, (uint)value._c);
810 }
811 if (value._d != _d)
812 {
813 return GetResult(_d, value._d);
814 }
815 if (value._e != _e)
816 {
817 return GetResult(_e, value._e);
818 }
819 if (value._f != _f)
820 {
821 return GetResult(_f, value._f);
822 }
823 if (value._g != _g)
824 {
825 return GetResult(_g, value._g);
826 }
827 if (value._h != _h)
828 {
829 return GetResult(_h, value._h);
830 }
831 if (value._i != _i)
832 {
833 return GetResult(_i, value._i);
834 }
835 if (value._j != _j)
836 {
837 return GetResult(_j, value._j);
838 }
839 if (value._k != _k)
840 {
841 return GetResult(_k, value._k);
842 }
843 return 0;
844 }
845
846 public static bool operator ==(Guid a, Guid b)
847 {
848 return EqualsCore(in a, in b);
849 }
850
851 public static bool operator !=(Guid a, Guid b)
852 {
853 return !EqualsCore(in a, in b);
854 }
855
856 public string ToString(string? format)
857 {
858 return ToString(format, null);
859 }
860
861 private unsafe static int HexsToChars(char* guidChars, int a, int b)
862 {
863 *guidChars = HexConverter.ToCharLower(a >> 4);
864 guidChars[1] = HexConverter.ToCharLower(a);
865 guidChars[2] = HexConverter.ToCharLower(b >> 4);
866 guidChars[3] = HexConverter.ToCharLower(b);
867 return 4;
868 }
869
870 private unsafe static int HexsToCharsHexOutput(char* guidChars, int a, int b)
871 {
872 *guidChars = '0';
873 guidChars[1] = 'x';
874 guidChars[2] = HexConverter.ToCharLower(a >> 4);
875 guidChars[3] = HexConverter.ToCharLower(a);
876 guidChars[4] = ',';
877 guidChars[5] = '0';
878 guidChars[6] = 'x';
879 guidChars[7] = HexConverter.ToCharLower(b >> 4);
880 guidChars[8] = HexConverter.ToCharLower(b);
881 return 9;
882 }
883
884 public string ToString(string? format, IFormatProvider? provider)
885 {
886 if (string.IsNullOrEmpty(format))
887 {
888 format = "D";
889 }
890 if (format.Length != 1)
891 {
893 }
894 int length;
895 switch (format[0])
896 {
897 case 'D':
898 case 'd':
899 length = 36;
900 break;
901 case 'N':
902 case 'n':
903 length = 32;
904 break;
905 case 'B':
906 case 'P':
907 case 'b':
908 case 'p':
909 length = 38;
910 break;
911 case 'X':
912 case 'x':
913 length = 68;
914 break;
915 default:
917 }
918 string text = string.FastAllocateString(length);
919 int charsWritten;
920 bool flag = TryFormat(new Span<char>(ref text.GetRawStringData(), text.Length), out charsWritten, format);
921 return text;
922 }
923
924 public unsafe bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default(ReadOnlySpan<char>))
925 {
926 if (format.Length == 0)
927 {
928 format = "D";
929 }
930 if (format.Length != 1)
931 {
933 }
934 bool flag = true;
935 bool flag2 = false;
936 int num = 0;
937 int num2;
938 switch (format[0])
939 {
940 case 'D':
941 case 'd':
942 num2 = 36;
943 break;
944 case 'N':
945 case 'n':
946 flag = false;
947 num2 = 32;
948 break;
949 case 'B':
950 case 'b':
951 num = 8192123;
952 num2 = 38;
953 break;
954 case 'P':
955 case 'p':
956 num = 2687016;
957 num2 = 38;
958 break;
959 case 'X':
960 case 'x':
961 num = 8192123;
962 flag = false;
963 flag2 = true;
964 num2 = 68;
965 break;
966 default:
968 }
969 if (destination.Length < num2)
970 {
971 charsWritten = 0;
972 return false;
973 }
974 fixed (char* ptr = &MemoryMarshal.GetReference(destination))
975 {
976 char* ptr2 = ptr;
977 if (num != 0)
978 {
979 *(ptr2++) = (char)num;
980 }
981 if (flag2)
982 {
983 *(ptr2++) = '0';
984 *(ptr2++) = 'x';
985 ptr2 += HexsToChars(ptr2, _a >> 24, _a >> 16);
986 ptr2 += HexsToChars(ptr2, _a >> 8, _a);
987 *(ptr2++) = ',';
988 *(ptr2++) = '0';
989 *(ptr2++) = 'x';
990 ptr2 += HexsToChars(ptr2, _b >> 8, _b);
991 *(ptr2++) = ',';
992 *(ptr2++) = '0';
993 *(ptr2++) = 'x';
994 ptr2 += HexsToChars(ptr2, _c >> 8, _c);
995 *(ptr2++) = ',';
996 *(ptr2++) = '{';
997 ptr2 += HexsToCharsHexOutput(ptr2, _d, _e);
998 *(ptr2++) = ',';
999 ptr2 += HexsToCharsHexOutput(ptr2, _f, _g);
1000 *(ptr2++) = ',';
1001 ptr2 += HexsToCharsHexOutput(ptr2, _h, _i);
1002 *(ptr2++) = ',';
1003 ptr2 += HexsToCharsHexOutput(ptr2, _j, _k);
1004 *(ptr2++) = '}';
1005 }
1006 else
1007 {
1008 ptr2 += HexsToChars(ptr2, _a >> 24, _a >> 16);
1009 ptr2 += HexsToChars(ptr2, _a >> 8, _a);
1010 if (flag)
1011 {
1012 *(ptr2++) = '-';
1013 }
1014 ptr2 += HexsToChars(ptr2, _b >> 8, _b);
1015 if (flag)
1016 {
1017 *(ptr2++) = '-';
1018 }
1019 ptr2 += HexsToChars(ptr2, _c >> 8, _c);
1020 if (flag)
1021 {
1022 *(ptr2++) = '-';
1023 }
1024 ptr2 += HexsToChars(ptr2, _d, _e);
1025 if (flag)
1026 {
1027 *(ptr2++) = '-';
1028 }
1029 ptr2 += HexsToChars(ptr2, _f, _g);
1030 ptr2 += HexsToChars(ptr2, _h, _i);
1031 ptr2 += HexsToChars(ptr2, _j, _k);
1032 }
1033 if (num != 0)
1034 {
1035 *(ptr2++) = (char)(num >> 16);
1036 }
1037 }
1038 charsWritten = num2;
1039 return true;
1040 }
1041
1043 {
1044 return TryFormat(destination, out charsWritten, format);
1045 }
1046
1047 [RequiresPreviewFeatures("Generic Math is in preview.", Url = "https://aka.ms/dotnet-warnings/generic-math-preview")]
1048 static bool IComparisonOperators<Guid, Guid>.operator <(Guid left, Guid right)
1049 {
1050 if (left._a != right._a)
1051 {
1052 return (uint)left._a < (uint)right._a;
1053 }
1054 if (left._b != right._b)
1055 {
1056 return (uint)left._b < (uint)right._b;
1057 }
1058 if (left._c != right._c)
1059 {
1060 return (uint)left._c < (uint)right._c;
1061 }
1062 if (left._d != right._d)
1063 {
1064 return left._d < right._d;
1065 }
1066 if (left._e != right._e)
1067 {
1068 return left._e < right._e;
1069 }
1070 if (left._f != right._f)
1071 {
1072 return left._f < right._f;
1073 }
1074 if (left._g != right._g)
1075 {
1076 return left._g < right._g;
1077 }
1078 if (left._h != right._h)
1079 {
1080 return left._h < right._h;
1081 }
1082 if (left._i != right._i)
1083 {
1084 return left._i < right._i;
1085 }
1086 if (left._j != right._j)
1087 {
1088 return left._j < right._j;
1089 }
1090 if (left._k != right._k)
1091 {
1092 return left._k < right._k;
1093 }
1094 return false;
1095 }
1096
1097 [RequiresPreviewFeatures("Generic Math is in preview.", Url = "https://aka.ms/dotnet-warnings/generic-math-preview")]
1098 static bool IComparisonOperators<Guid, Guid>.operator <=(Guid left, Guid right)
1099 {
1100 if (left._a != right._a)
1101 {
1102 return (uint)left._a < (uint)right._a;
1103 }
1104 if (left._b != right._b)
1105 {
1106 return (uint)left._b < (uint)right._b;
1107 }
1108 if (left._c != right._c)
1109 {
1110 return (uint)left._c < (uint)right._c;
1111 }
1112 if (left._d != right._d)
1113 {
1114 return left._d < right._d;
1115 }
1116 if (left._e != right._e)
1117 {
1118 return left._e < right._e;
1119 }
1120 if (left._f != right._f)
1121 {
1122 return left._f < right._f;
1123 }
1124 if (left._g != right._g)
1125 {
1126 return left._g < right._g;
1127 }
1128 if (left._h != right._h)
1129 {
1130 return left._h < right._h;
1131 }
1132 if (left._i != right._i)
1133 {
1134 return left._i < right._i;
1135 }
1136 if (left._j != right._j)
1137 {
1138 return left._j < right._j;
1139 }
1140 if (left._k != right._k)
1141 {
1142 return left._k < right._k;
1143 }
1144 return true;
1145 }
1146
1147 [RequiresPreviewFeatures("Generic Math is in preview.", Url = "https://aka.ms/dotnet-warnings/generic-math-preview")]
1148 static bool IComparisonOperators<Guid, Guid>.operator >(Guid left, Guid right)
1149 {
1150 if (left._a != right._a)
1151 {
1152 return (uint)left._a > (uint)right._a;
1153 }
1154 if (left._b != right._b)
1155 {
1156 return (uint)left._b > (uint)right._b;
1157 }
1158 if (left._c != right._c)
1159 {
1160 return (uint)left._c > (uint)right._c;
1161 }
1162 if (left._d != right._d)
1163 {
1164 return left._d > right._d;
1165 }
1166 if (left._e != right._e)
1167 {
1168 return left._e > right._e;
1169 }
1170 if (left._f != right._f)
1171 {
1172 return left._f > right._f;
1173 }
1174 if (left._g != right._g)
1175 {
1176 return left._g > right._g;
1177 }
1178 if (left._h != right._h)
1179 {
1180 return left._h > right._h;
1181 }
1182 if (left._i != right._i)
1183 {
1184 return left._i > right._i;
1185 }
1186 if (left._j != right._j)
1187 {
1188 return left._j > right._j;
1189 }
1190 if (left._k != right._k)
1191 {
1192 return left._k > right._k;
1193 }
1194 return false;
1195 }
1196
1197 [RequiresPreviewFeatures("Generic Math is in preview.", Url = "https://aka.ms/dotnet-warnings/generic-math-preview")]
1198 static bool IComparisonOperators<Guid, Guid>.operator >=(Guid left, Guid right)
1199 {
1200 if (left._a != right._a)
1201 {
1202 return (uint)left._a > (uint)right._a;
1203 }
1204 if (left._b != right._b)
1205 {
1206 return (uint)left._b > (uint)right._b;
1207 }
1208 if (left._c != right._c)
1209 {
1210 return (uint)left._c > (uint)right._c;
1211 }
1212 if (left._d != right._d)
1213 {
1214 return left._d > right._d;
1215 }
1216 if (left._e != right._e)
1217 {
1218 return left._e > right._e;
1219 }
1220 if (left._f != right._f)
1221 {
1222 return left._f > right._f;
1223 }
1224 if (left._g != right._g)
1225 {
1226 return left._g > right._g;
1227 }
1228 if (left._h != right._h)
1229 {
1230 return left._h > right._h;
1231 }
1232 if (left._i != right._i)
1233 {
1234 return left._i > right._i;
1235 }
1236 if (left._j != right._j)
1237 {
1238 return left._j > right._j;
1239 }
1240 if (left._k != right._k)
1241 {
1242 return left._k > right._k;
1243 }
1244 return true;
1245 }
1246
1247 [RequiresPreviewFeatures("Generic Math is in preview.", Url = "https://aka.ms/dotnet-warnings/generic-math-preview")]
1248 static bool IEqualityOperators<Guid, Guid>.operator ==(Guid left, Guid right)
1249 {
1250 return left == right;
1251 }
1252
1253 [RequiresPreviewFeatures("Generic Math is in preview.", Url = "https://aka.ms/dotnet-warnings/generic-math-preview")]
1254 static bool IEqualityOperators<Guid, Guid>.operator !=(Guid left, Guid right)
1255 {
1256 return left != right;
1257 }
1258
1259 [RequiresPreviewFeatures("Generic Math is in preview.", Url = "https://aka.ms/dotnet-warnings/generic-math-preview")]
1261 {
1262 return Parse(s);
1263 }
1264
1265 [RequiresPreviewFeatures("Generic Math is in preview.", Url = "https://aka.ms/dotnet-warnings/generic-math-preview")]
1266 static bool IParseable<Guid>.TryParse([NotNullWhen(true)] string s, IFormatProvider provider, out Guid result)
1267 {
1268 return TryParse(s, out result);
1269 }
1270
1271 [RequiresPreviewFeatures("Generic Math is in preview.", Url = "https://aka.ms/dotnet-warnings/generic-math-preview")]
1273 {
1274 return Parse(s);
1275 }
1276
1277 [RequiresPreviewFeatures("Generic Math is in preview.", Url = "https://aka.ms/dotnet-warnings/generic-math-preview")]
1279 {
1280 return TryParse(s, out result);
1281 }
1282
1283 public static Guid NewGuid()
1284 {
1285 Guid guid;
1286 int num = Interop.Ole32.CoCreateGuid(out guid);
1287 if (num != 0)
1288 {
1289 Exception ex = new Exception();
1290 ex.HResult = num;
1291 throw ex;
1292 }
1293 return guid;
1294 }
1295}
static int CoCreateGuid(out Guid guid)
static readonly bool IsLittleEndian
static sbyte ReverseEndianness(sbyte value)
static char ToCharLower(int value)
static ReadOnlySpan< byte > CharToHexLookup
static int FromChar(int c)
static ParsingStatus TryParseUInt32HexNumberStyle(ReadOnlySpan< char > value, NumberStyles styles, out uint result)
Definition Number.cs:4972
static string Arg_GuidArrayCtor
Definition SR.cs:178
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Format_InvalidGuidFormatSpecification
Definition SR.cs:1352
static string GetResourceString(string resourceKey)
Definition SR.cs:101
static string Arg_MustBeGuid
Definition SR.cs:278
static string Format_GuidUnrecognized
Definition SR.cs:1346
Definition SR.cs:7
static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, out TSelf result)
static TSelf Parse(string s, IFormatProvider? provider)
bool TryFormat(Span< char > destination, out int charsWritten, ReadOnlySpan< char > format, IFormatProvider? provider)
static bool TryParse(ReadOnlySpan< char > s, IFormatProvider? provider, out TSelf result)
static TSelf Parse(ReadOnlySpan< char > s, IFormatProvider? provider)
readonly void SetFailure(bool overflow, string failureMessageID)
Definition Guid.cs:62
GuidResult(GuidParseThrowStyle canThrow)
Definition Guid.cs:56
readonly GuidParseThrowStyle _throwStyle
Definition Guid.cs:54
readonly Guid ToGuid()
Definition Guid.cs:80
static Guid Parse(ReadOnlySpan< char > input)
Definition Guid.cs:199
static bool EqualsCore(in Guid left, in Guid right)
Definition Guid.cs:720
string ToString(string? format, IFormatProvider? provider)
Definition Guid.cs:884
static Guid ParseExact(string input, string format)
Definition Guid.cs:228
readonly short _b
Definition Guid.cs:90
static unsafe int HexsToChars(char *guidChars, int a, int b)
Definition Guid.cs:861
readonly byte _f
Definition Guid.cs:98
readonly byte _h
Definition Guid.cs:102
byte[] ToByteArray()
Definition Guid.cs:681
static bool TryParseExactX(ReadOnlySpan< char > guidString, ref GuidResult result)
Definition Guid.cs:455
readonly short _c
Definition Guid.cs:92
Guid(uint a, ushort b, ushort c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k)
Definition Guid.cs:126
unsafe bool TryFormat(Span< char > destination, out int charsWritten, ReadOnlySpan< char > format=default(ReadOnlySpan< char >))
Definition Guid.cs:924
readonly byte _j
Definition Guid.cs:106
static bool TryParseExact(ReadOnlySpan< char > input, ReadOnlySpan< char > format, out Guid result)
Definition Guid.cs:272
override bool Equals([NotNullWhen(true)] object? o)
Definition Guid.cs:706
string ToString(string? format)
Definition Guid.cs:856
Guid(int a, short b, short c, byte[] d)
Definition Guid.cs:141
Guid(byte[] b)
Definition Guid.cs:110
static byte DecodeByte(nuint ch1, nuint ch2, ref int invalidIfNegative)
Definition Guid.cs:568
readonly byte _k
Definition Guid.cs:108
static bool TryParseExact([NotNullWhen(true)] string? input, [NotNullWhen(true)] string? format, out Guid result)
Definition Guid.cs:262
static bool operator!=(Guid a, Guid b)
Definition Guid.cs:851
override int GetHashCode()
Definition Guid.cs:700
static ReadOnlySpan< char > EatAllWhitespace(ReadOnlySpan< char > str)
Definition Guid.cs:644
static bool TryParseHex(ReadOnlySpan< char > guidString, out uint result, ref bool overflow)
Definition Guid.cs:601
static bool TryParse([NotNullWhen(true)] string? input, out Guid result)
Definition Guid.cs:206
readonly byte _d
Definition Guid.cs:94
static int GetResult(uint me, uint them)
Definition Guid.cs:731
readonly byte _e
Definition Guid.cs:96
static bool IsHexPrefix(ReadOnlySpan< char > str, int i)
Definition Guid.cs:672
static bool TryParseGuid(ReadOnlySpan< char > guidString, ref GuidResult result)
Definition Guid.cs:309
static bool TryParseExactB(ReadOnlySpan< char > guidString, ref GuidResult result)
Definition Guid.cs:325
static bool operator==(Guid a, Guid b)
Definition Guid.cs:846
static bool TryParseExactN(ReadOnlySpan< char > guidString, ref GuidResult result)
Definition Guid.cs:409
static unsafe int HexsToCharsHexOutput(char *guidChars, int a, int b)
Definition Guid.cs:870
GuidParseThrowStyle
Definition Guid.cs:17
static bool TryParseHex(ReadOnlySpan< char > guidString, out ushort result, ref bool overflow)
Definition Guid.cs:587
static Guid NewGuid()
Definition Guid.cs:1283
Guid(string g)
Definition Guid.cs:179
Guid(int a, short b, short c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k)
Definition Guid.cs:164
static bool TryParse(ReadOnlySpan< char > input, out Guid result)
Definition Guid.cs:216
override string ToString()
Definition Guid.cs:695
static bool TryParseHex(ReadOnlySpan< char > guidString, out uint result)
Definition Guid.cs:595
bool Equals(Guid g)
Definition Guid.cs:715
Guid(ReadOnlySpan< byte > b)
Definition Guid.cs:115
static Guid ParseExact(ReadOnlySpan< char > input, ReadOnlySpan< char > format)
Definition Guid.cs:242
readonly byte _i
Definition Guid.cs:104
int CompareTo(Guid value)
Definition Guid.cs:797
static readonly Guid Empty
Definition Guid.cs:86
readonly int _a
Definition Guid.cs:88
static Guid Parse(string input)
Definition Guid.cs:190
static bool TryParseExactP(ReadOnlySpan< char > guidString, ref GuidResult result)
Definition Guid.cs:445
int CompareTo(object? value)
Definition Guid.cs:740
static bool TryParseExactD(ReadOnlySpan< char > guidString, ref GuidResult result)
Definition Guid.cs:335
bool TryWriteBytes(Span< byte > destination)
Definition Guid.cs:689
readonly byte _g
Definition Guid.cs:100
ReadOnlySpan< T > Slice(int start)