Terraria
v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ActivityTraceId.cs
Go to the documentation of this file.
1
using
System.Buffers.Binary
;
2
using
System.Buffers.Text
;
3
using
System.Diagnostics.CodeAnalysis
;
4
using
System.Runtime.CompilerServices
;
5
using
System.Runtime.InteropServices
;
6
7
namespace
System.Diagnostics
;
8
9
public
readonly
struct
ActivityTraceId
:
IEquatable
<ActivityTraceId>
10
{
11
private
readonly
string
_hexString
;
12
13
internal
ActivityTraceId
(
string
hexString)
14
{
15
_hexString
= hexString;
16
}
17
18
public
static
ActivityTraceId
CreateRandom
()
19
{
20
Span<byte>
span = stackalloc
byte
[16];
21
SetToRandomBytes
(span);
22
return
CreateFromBytes
(span);
23
}
24
25
public
static
ActivityTraceId
CreateFromBytes
(
ReadOnlySpan<byte>
idData)
26
{
27
if
(idData.
Length
!= 16)
28
{
29
throw
new
ArgumentOutOfRangeException
(
"idData"
);
30
}
31
return
new
ActivityTraceId
(
System
.
HexConverter
.
ToString
(idData,
System
.
HexConverter
.
Casing
.Lower));
32
}
33
34
public
static
ActivityTraceId
CreateFromUtf8String
(
ReadOnlySpan<byte>
idData)
35
{
36
return
new
ActivityTraceId
(idData);
37
}
38
39
public
static
ActivityTraceId
CreateFromString
(
ReadOnlySpan<char>
idData)
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
87
private
ActivityTraceId
(
ReadOnlySpan<byte>
idData)
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
}
104
if
(
BitConverter
.
IsLittleEndian
)
105
{
106
span[0] =
BinaryPrimitives
.
ReverseEndianness
(span[0]);
107
span[1] =
BinaryPrimitives
.
ReverseEndianness
(span[1]);
108
}
109
_hexString
=
System
.
HexConverter
.
ToString
(
MemoryMarshal
.AsBytes(span),
System
.
HexConverter
.
Casing
.Lower);
110
}
111
112
public
void
CopyTo
(
Span<byte>
destination
)
113
{
114
SetSpanFromHexChars
(
ToHexString
().AsSpan(),
destination
);
115
}
116
117
internal
static
void
SetToRandomBytes
(
Span<byte>
outBytes)
118
{
119
RandomNumberGenerator
current =
RandomNumberGenerator
.
Current
;
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
146
internal
static
bool
IsLowerCaseHexAndNotAllZeros
(
ReadOnlySpan<char>
idData)
147
{
148
bool
result =
false
;
149
for
(
int
i = 0; i < idData.
Length
; i++)
150
{
151
char
c = idData[i];
152
if
(!
System
.
HexConverter
.
IsHexLowerChar
(c))
153
{
154
return
false
;
155
}
156
if
(c !=
'0'
)
157
{
158
result =
true
;
159
}
160
}
161
return
result;
162
}
163
}
System.ArgumentOutOfRangeException
Definition
ArgumentOutOfRangeException.cs:9
System.BitConverter.IsLittleEndian
static readonly bool IsLittleEndian
Definition
BitConverter.cs:12
System.BitConverter
Definition
BitConverter.cs:10
System.Buffers.Binary.BinaryPrimitives.ReverseEndianness
static sbyte ReverseEndianness(sbyte value)
Definition
BinaryPrimitives.cs:11
System.Buffers.Binary.BinaryPrimitives
Definition
BinaryPrimitives.cs:8
System.Buffers.Text.Utf8Parser.TryParse
static bool TryParse(ReadOnlySpan< byte > source, out bool value, out int bytesConsumed, char standardFormat='\0')
Definition
Utf8Parser.cs:179
System.Buffers.Text.Utf8Parser
Definition
Utf8Parser.cs:7
System.Diagnostics.RandomNumberGenerator.Current
static RandomNumberGenerator Current
Definition
RandomNumberGenerator.cs:17
System.Diagnostics.RandomNumberGenerator.Next
long Next()
Definition
RandomNumberGenerator.cs:53
System.Diagnostics.RandomNumberGenerator
Definition
RandomNumberGenerator.cs:4
System.HexConverter.FromLowerChar
static int FromLowerChar(int c)
Definition
HexConverter.cs:43
System.HexConverter.IsHexLowerChar
static bool IsHexLowerChar(int c)
Definition
HexConverter.cs:71
System.HexConverter.Casing
Casing
Definition
HexConverter.cs:8
System.HexConverter.ToString
static unsafe string ToString(ReadOnlySpan< byte > bytes, Casing casing=Casing.Upper)
Definition
HexConverter.cs:30
System.HexConverter
Definition
HexConverter.cs:6
System.Runtime.CompilerServices.Unsafe
Definition
Unsafe.cs:6
System.Runtime.InteropServices.MemoryMarshal
Definition
MemoryMarshal.cs:11
System.IEquatable
Definition
IEquatable.cs:4
System.Buffers.Binary
Definition
BinaryPrimitives.cs:5
System.Buffers.Text
Definition
Base64.cs:7
System.Diagnostics.CodeAnalysis
Definition
AllowNullAttribute.cs:1
System.Diagnostics
Definition
AggregationManager.cs:6
System.Runtime.CompilerServices
Definition
NullablePublicOnlyAttribute.cs:3
System.Runtime.InteropServices
Definition
SequenceMarshal.cs:4
System.ExceptionArgument.destination
@ destination
System.ExceptionArgument.obj
@ obj
System
Definition
BlockingCollection.cs:8
System.Diagnostics.ActivityTraceId.CreateFromString
static ActivityTraceId CreateFromString(ReadOnlySpan< char > idData)
Definition
ActivityTraceId.cs:39
System.Diagnostics.ActivityTraceId.CreateRandom
static ActivityTraceId CreateRandom()
Definition
ActivityTraceId.cs:18
System.Diagnostics.ActivityTraceId.CreateFromUtf8String
static ActivityTraceId CreateFromUtf8String(ReadOnlySpan< byte > idData)
Definition
ActivityTraceId.cs:34
System.Diagnostics.ActivityTraceId.IsLowerCaseHexAndNotAllZeros
static bool IsLowerCaseHexAndNotAllZeros(ReadOnlySpan< char > idData)
Definition
ActivityTraceId.cs:146
System.Diagnostics.ActivityTraceId.CreateFromBytes
static ActivityTraceId CreateFromBytes(ReadOnlySpan< byte > idData)
Definition
ActivityTraceId.cs:25
System.Diagnostics.ActivityTraceId.ActivityTraceId
ActivityTraceId(ReadOnlySpan< byte > idData)
Definition
ActivityTraceId.cs:87
System.Diagnostics.ActivityTraceId.Equals
override bool Equals([NotNullWhen(true)] object? obj)
Definition
ActivityTraceId.cs:73
System.Diagnostics.ActivityTraceId.ToHexString
string ToHexString()
Definition
ActivityTraceId.cs:48
System.Diagnostics.ActivityTraceId.GetHashCode
override int GetHashCode()
Definition
ActivityTraceId.cs:82
System.Diagnostics.ActivityTraceId._hexString
readonly string _hexString
Definition
ActivityTraceId.cs:11
System.Diagnostics.ActivityTraceId.operator==
static bool operator==(ActivityTraceId traceId1, ActivityTraceId traceId2)
Definition
ActivityTraceId.cs:58
System.Diagnostics.ActivityTraceId.Equals
bool Equals(ActivityTraceId traceId)
Definition
ActivityTraceId.cs:68
System.Diagnostics.ActivityTraceId.SetSpanFromHexChars
static void SetSpanFromHexChars(ReadOnlySpan< char > charData, Span< byte > outBytes)
Definition
ActivityTraceId.cs:127
System.Diagnostics.ActivityTraceId.CopyTo
void CopyTo(Span< byte > destination)
Definition
ActivityTraceId.cs:112
System.Diagnostics.ActivityTraceId.ToString
override string ToString()
Definition
ActivityTraceId.cs:53
System.Diagnostics.ActivityTraceId.ActivityTraceId
ActivityTraceId(string hexString)
Definition
ActivityTraceId.cs:13
System.Diagnostics.ActivityTraceId.operator!=
static bool operator!=(ActivityTraceId traceId1, ActivityTraceId traceId2)
Definition
ActivityTraceId.cs:63
System.Diagnostics.ActivityTraceId.HexByteFromChars
static byte HexByteFromChars(char char1, char char2)
Definition
ActivityTraceId.cs:135
System.Diagnostics.ActivityTraceId.SetToRandomBytes
static void SetToRandomBytes(Span< byte > outBytes)
Definition
ActivityTraceId.cs:117
System.Diagnostics.ActivityTraceId
Definition
ActivityTraceId.cs:10
System.ReadOnlySpan.Length
int Length
Definition
ReadOnlySpan.cs:70
System.ReadOnlySpan.Slice
ReadOnlySpan< T > Slice(int start)
Definition
ReadOnlySpan.cs:232
System.ReadOnlySpan.ToString
override string ToString()
Definition
ReadOnlySpan.cs:222
System.ReadOnlySpan
Definition
ReadOnlySpan.cs:14
System.Span.Length
int Length
Definition
Span.cs:70
System.Span
Definition
Span.cs:14
source
System.Diagnostics.DiagnosticSource
System.Diagnostics
ActivityTraceId.cs
Generated by
1.10.0