Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ActivityTraceId.cs
Go to the documentation of this file.
6
7namespace System.Diagnostics;
8
9public readonly struct ActivityTraceId : IEquatable<ActivityTraceId>
10{
11 private readonly string _hexString;
12
13 internal ActivityTraceId(string hexString)
14 {
15 _hexString = hexString;
16 }
17
19 {
20 Span<byte> span = stackalloc byte[16];
21 SetToRandomBytes(span);
22 return CreateFromBytes(span);
23 }
24
26 {
27 if (idData.Length != 16)
28 {
29 throw new ArgumentOutOfRangeException("idData");
30 }
32 }
33
35 {
36 return new ActivityTraceId(idData);
37 }
38
40 {
41 if (idData.Length != 32 || !IsLowerCaseHexAndNotAllZeros(idData))
42 {
43 throw new ArgumentOutOfRangeException("idData");
44 }
45 return new ActivityTraceId(idData.ToString());
46 }
47
48 public string ToHexString()
49 {
50 return _hexString ?? "00000000000000000000000000000000";
51 }
52
53 public override string ToString()
54 {
55 return ToHexString();
56 }
57
58 public static bool operator ==(ActivityTraceId traceId1, ActivityTraceId traceId2)
59 {
60 return traceId1._hexString == traceId2._hexString;
61 }
62
63 public static bool operator !=(ActivityTraceId traceId1, ActivityTraceId traceId2)
64 {
65 return traceId1._hexString != traceId2._hexString;
66 }
67
68 public bool Equals(ActivityTraceId traceId)
69 {
70 return _hexString == traceId._hexString;
71 }
72
73 public override bool Equals([NotNullWhen(true)] object? obj)
74 {
75 if (obj is ActivityTraceId activityTraceId)
76 {
77 return _hexString == activityTraceId._hexString;
78 }
79 return false;
80 }
81
82 public override int GetHashCode()
83 {
84 return ToHexString().GetHashCode();
85 }
86
88 {
89 if (idData.Length != 32)
90 {
91 throw new ArgumentOutOfRangeException("idData");
92 }
93 Span<ulong> span = stackalloc ulong[2];
94 if (!Utf8Parser.TryParse(idData.Slice(0, 16), out span[0], out int bytesConsumed, 'x'))
95 {
96 _hexString = CreateRandom()._hexString;
97 return;
98 }
99 if (!Utf8Parser.TryParse(idData.Slice(16, 16), out span[1], out bytesConsumed, 'x'))
100 {
101 _hexString = CreateRandom()._hexString;
102 return;
103 }
105 {
106 span[0] = BinaryPrimitives.ReverseEndianness(span[0]);
107 span[1] = BinaryPrimitives.ReverseEndianness(span[1]);
108 }
110 }
111
113 {
115 }
116
117 internal static void SetToRandomBytes(Span<byte> outBytes)
118 {
120 Unsafe.WriteUnaligned(ref outBytes[0], current.Next());
121 if (outBytes.Length == 16)
122 {
123 Unsafe.WriteUnaligned(ref outBytes[8], current.Next());
124 }
125 }
126
127 internal static void SetSpanFromHexChars(ReadOnlySpan<char> charData, Span<byte> outBytes)
128 {
129 for (int i = 0; i < outBytes.Length; i++)
130 {
131 outBytes[i] = HexByteFromChars(charData[i * 2], charData[i * 2 + 1]);
132 }
133 }
134
135 internal static byte HexByteFromChars(char char1, char char2)
136 {
137 int num = System.HexConverter.FromLowerChar(char1);
138 int num2 = System.HexConverter.FromLowerChar(char2);
139 if ((num | num2) == 255)
140 {
141 throw new ArgumentOutOfRangeException("idData");
142 }
143 return (byte)((num << 4) | num2);
144 }
145
147 {
148 bool result = false;
149 for (int i = 0; i < idData.Length; i++)
150 {
151 char c = idData[i];
153 {
154 return false;
155 }
156 if (c != '0')
157 {
158 result = true;
159 }
160 }
161 return result;
162 }
163}
static readonly bool IsLittleEndian
static sbyte ReverseEndianness(sbyte value)
static bool TryParse(ReadOnlySpan< byte > source, out bool value, out int bytesConsumed, char standardFormat='\0')
static int FromLowerChar(int c)
static bool IsHexLowerChar(int c)
static unsafe string ToString(ReadOnlySpan< byte > bytes, Casing casing=Casing.Upper)
static ActivityTraceId CreateFromString(ReadOnlySpan< char > idData)
static ActivityTraceId CreateRandom()
static ActivityTraceId CreateFromUtf8String(ReadOnlySpan< byte > idData)
static bool IsLowerCaseHexAndNotAllZeros(ReadOnlySpan< char > idData)
static ActivityTraceId CreateFromBytes(ReadOnlySpan< byte > idData)
ActivityTraceId(ReadOnlySpan< byte > idData)
override bool Equals([NotNullWhen(true)] object? obj)
static bool operator==(ActivityTraceId traceId1, ActivityTraceId traceId2)
bool Equals(ActivityTraceId traceId)
static void SetSpanFromHexChars(ReadOnlySpan< char > charData, Span< byte > outBytes)
void CopyTo(Span< byte > destination)
static bool operator!=(ActivityTraceId traceId1, ActivityTraceId traceId2)
static byte HexByteFromChars(char char1, char char2)
static void SetToRandomBytes(Span< byte > outBytes)
ReadOnlySpan< T > Slice(int start)
override string ToString()
int Length
Definition Span.cs:70