-
Notifications
You must be signed in to change notification settings - Fork 25
/
GuidExtensions.cs
253 lines (238 loc) · 9.72 KB
/
GuidExtensions.cs
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
using System.Collections.Frozen;
namespace OtterGui;
public static class GuidExtensions
{
private const string Chars =
"0123456789"
+ "abcdefghij"
+ "klmnopqrst"
+ "uv";
private static ReadOnlySpan<byte> Bytes
=> "0123456789abcdefghijklmnopqrstuv"u8;
private static readonly FrozenDictionary<char, byte>
ReverseChars = Chars.WithIndex().ToFrozenDictionary(t => t.Value, t => (byte)t.Index);
private static readonly FrozenDictionary<byte, byte> ReverseBytes =
ReverseChars.ToFrozenDictionary(kvp => (byte)kvp.Key, kvp => kvp.Value);
public static unsafe string OptimizedString(this Guid guid)
{
var bytes = stackalloc ulong[2];
if (!guid.TryWriteBytes(new Span<byte>(bytes, 16)))
return guid.ToString("N");
var u1 = bytes[0];
var u2 = bytes[1];
Span<char> text =
[
Chars[(int)(u1 & 0x1F)],
Chars[(int)((u1 >> 5) & 0x1F)],
Chars[(int)((u1 >> 10) & 0x1F)],
Chars[(int)((u1 >> 15) & 0x1F)],
Chars[(int)((u1 >> 20) & 0x1F)],
Chars[(int)((u1 >> 25) & 0x1F)],
Chars[(int)((u1 >> 30) & 0x1F)],
Chars[(int)((u1 >> 35) & 0x1F)],
Chars[(int)((u1 >> 40) & 0x1F)],
Chars[(int)((u1 >> 45) & 0x1F)],
Chars[(int)((u1 >> 50) & 0x1F)],
Chars[(int)((u1 >> 55) & 0x1F)],
Chars[(int)((u1 >> 60) | ((u2 & 0x01) << 4))],
Chars[(int)((u2 >> 1) & 0x1F)],
Chars[(int)((u2 >> 6) & 0x1F)],
Chars[(int)((u2 >> 11) & 0x1F)],
Chars[(int)((u2 >> 16) & 0x1F)],
Chars[(int)((u2 >> 21) & 0x1F)],
Chars[(int)((u2 >> 26) & 0x1F)],
Chars[(int)((u2 >> 31) & 0x1F)],
Chars[(int)((u2 >> 36) & 0x1F)],
Chars[(int)((u2 >> 41) & 0x1F)],
Chars[(int)((u2 >> 46) & 0x1F)],
Chars[(int)((u2 >> 51) & 0x1F)],
Chars[(int)((u2 >> 56) & 0x1F)],
Chars[(int)((u2 >> 61) & 0x1F)],
];
return new string(text);
}
public static unsafe bool FromOptimizedString(ReadOnlySpan<char> text, out Guid guid)
{
if (text.Length != 26)
return Return(out guid);
var bytes = stackalloc ulong[2];
if (!ReverseChars.TryGetValue(text[0], out var b0))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[1], out var b1))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[2], out var b2))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[3], out var b3))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[4], out var b4))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[5], out var b5))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[6], out var b6))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[7], out var b7))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[8], out var b8))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[9], out var b9))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[10], out var b10))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[11], out var b11))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[12], out var b12))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[13], out var b13))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[14], out var b14))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[15], out var b15))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[16], out var b16))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[17], out var b17))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[18], out var b18))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[19], out var b19))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[20], out var b20))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[21], out var b21))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[22], out var b22))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[23], out var b23))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[24], out var b24))
return Return(out guid);
if (!ReverseChars.TryGetValue(text[25], out var b25))
return Return(out guid);
bytes[0] = b0
| ((ulong)b1 << 5)
| ((ulong)b2 << 10)
| ((ulong)b3 << 15)
| ((ulong)b4 << 20)
| ((ulong)b5 << 25)
| ((ulong)b6 << 30)
| ((ulong)b7 << 35)
| ((ulong)b8 << 40)
| ((ulong)b9 << 45)
| ((ulong)b10 << 50)
| ((ulong)b11 << 55)
| ((ulong)b12 << 60);
bytes[1] = ((ulong)b12 >> 4)
| ((ulong)b13 << 1)
| ((ulong)b14 << 6)
| ((ulong)b15 << 11)
| ((ulong)b16 << 16)
| ((ulong)b17 << 21)
| ((ulong)b18 << 26)
| ((ulong)b19 << 31)
| ((ulong)b20 << 36)
| ((ulong)b21 << 41)
| ((ulong)b22 << 46)
| ((ulong)b23 << 51)
| ((ulong)b24 << 56)
| ((ulong)b25 << 61);
guid = new Guid(new Span<byte>(bytes, 16));
return true;
static bool Return(out Guid guid)
{
guid = Guid.Empty;
return false;
}
}
public static unsafe bool FromOptimizedString(ReadOnlySpan<byte> text, out Guid guid)
{
if (text.Length != 26)
return Return(out guid);
var bytes = stackalloc ulong[2];
if (!ReverseBytes.TryGetValue(text[0], out var b0))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[1], out var b1))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[2], out var b2))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[3], out var b3))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[4], out var b4))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[5], out var b5))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[6], out var b6))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[7], out var b7))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[8], out var b8))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[9], out var b9))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[10], out var b10))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[11], out var b11))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[12], out var b12))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[13], out var b13))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[14], out var b14))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[15], out var b15))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[16], out var b16))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[17], out var b17))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[18], out var b18))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[19], out var b19))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[20], out var b20))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[21], out var b21))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[22], out var b22))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[23], out var b23))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[24], out var b24))
return Return(out guid);
if (!ReverseBytes.TryGetValue(text[25], out var b25))
return Return(out guid);
bytes[0] = b0
| ((ulong)b1 << 5)
| ((ulong)b2 << 10)
| ((ulong)b3 << 15)
| ((ulong)b4 << 20)
| ((ulong)b5 << 25)
| ((ulong)b6 << 30)
| ((ulong)b7 << 35)
| ((ulong)b8 << 40)
| ((ulong)b9 << 45)
| ((ulong)b10 << 50)
| ((ulong)b11 << 55)
| ((ulong)b12 << 60);
bytes[1] = ((ulong)b12 >> 4)
| ((ulong)b13 << 1)
| ((ulong)b14 << 6)
| ((ulong)b15 << 11)
| ((ulong)b16 << 16)
| ((ulong)b17 << 21)
| ((ulong)b18 << 26)
| ((ulong)b19 << 31)
| ((ulong)b20 << 36)
| ((ulong)b21 << 41)
| ((ulong)b22 << 46)
| ((ulong)b23 << 51)
| ((ulong)b24 << 56)
| ((ulong)b25 << 61);
guid = new Guid(new Span<byte>(bytes, 16));
return true;
static bool Return(out Guid guid)
{
guid = Guid.Empty;
return false;
}
}
}