-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquid.cs
executable file
·418 lines (354 loc) · 13.4 KB
/
Squid.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
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
// // ***********************************************************************
// // Solution : Inno.Api.v2
// // Assembly : FCS.Lib.Utility
// // Filename : Squid.cs
// // Created : 2025-01-03 14:01
// // Last Modified By : dev
// // Last Modified On : 2025-01-04 12:01
// // ***********************************************************************
// // <copyright company="Frede Hundewadt">
// // Copyright (C) 2010-2025 Frede Hundewadt
// // This program is free software: you can redistribute it and/or modify
// // it under the terms of the GNU Affero General Public License as
// // published by the Free Software Foundation, either version 3 of the
// // License, or (at your option) any later version.
// //
// // This program is distributed in the hope that it will be useful,
// // but WITHOUT ANY WARRANTY; without even the implied warranty of
// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// // GNU Affero General Public License for more details.
// //
// // You should have received a copy of the GNU Affero General Public License
// // along with this program. If not, see [https://www.gnu.org/licenses]
// // </copyright>
// // <summary></summary>
// // ***********************************************************************
using System;
using System.Diagnostics;
namespace FCS.Lib.Utility;
/// <summary>
/// Represents a globally unique identifier (GUID) encoded in a custom format.
/// </summary>
/// <remarks>
/// The <see cref="FCS.Lib.Utility.Squid" /> struct provides functionality for encoding and decoding GUIDs
/// into a custom string format, as well as creating new GUIDs and performing various operations
/// such as equality checks and conversions.
/// </remarks>
[DebuggerDisplay("{" + nameof(Value) + "}")]
public readonly struct Squid : IEquatable<Squid>
{
/// <summary>
/// A read-only object of the Squid struct.
/// Value is guaranteed to be all zeroes.
/// </summary>
public static readonly Squid Empty = new(Guid.Empty);
/// <summary>
/// Creates a new Squid from a Squid encoded string.
/// </summary>
/// <param name="value">A valid Squid encoded string.</param>
public Squid(string value)
{
Value = value;
Guid = DecodeSquid(value);
}
/// <summary>
/// Creates a new Squid with the given <see cref="System.Guid" />.
/// </summary>
/// <param name="obj">A valid System.Guid object.</param>
public Squid(Guid obj)
{
Value = EncodeGuid(obj);
Guid = obj;
}
/// <summary>
/// Gets the underlying <see cref="System.Guid" /> for the encoded Squid.
/// </summary>
/// <value>The unique identifier.</value>
public Guid Guid { get; }
/// <summary>
/// The encoded string value of the <see cref="Guid" />
/// as an URL-safe Base64 string.
/// </summary>
/// <value>The value.</value>
public string Value { get; }
/// <summary>
/// Returns the encoded URL-safe Base64 string.
/// </summary>
/// <returns>A <see cref="string" /> that represents this instance.</returns>
public override string ToString()
{
return Value;
}
/// <summary>
/// Returns a value indicating whether this object and a specified object represent the same type and value.
/// Compares for equality against other string, Guid and Squid types.
/// </summary>
/// <param name="obj">A System.String, System.Guid or Squid object</param>
/// <returns><c>true</c> if the specified <see cref="object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
public override bool Equals(object obj)
{
return obj is Squid other && Equals(other);
}
/// <summary>
/// Equality comparison
/// </summary>
/// <param name="obj">A valid Squid object</param>
/// <returns>A boolean indicating equality.</returns>
public bool Equals(Squid obj)
{
return Guid.Equals(obj.Guid) && Value == obj.Value;
}
/// <summary>
/// Returns the hash code for the underlying <see cref="System.Guid" />.
/// </summary>
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
public override int GetHashCode()
{
unchecked
{
return (Guid.GetHashCode() * 397) ^ (Value != null ? Value.GetHashCode() : 0);
}
}
/// <summary>
/// Initialises a new object of the Squid using <see cref="Guid.NewGuid()" />.
/// </summary>
/// <returns>New Squid object</returns>
public static Squid NewGuid()
{
return new Squid(Guid.NewGuid());
}
/// <summary>
/// Encode string as a new Squid encoded string.
/// The encoding is similar to Base64 with
/// non-URL safe characters replaced, and padding removed.
/// </summary>
/// <param name="value">A valid <see cref="System.Guid" />.Tostring().</param>
/// <returns>A 22 character URL-safe Base64 string.</returns>
public static string EncodeString(string value)
{
var guid = new Guid(value);
return EncodeGuid(guid);
}
/// <summary>
/// Encode a <see cref="System.Guid" /> object to Squid.
/// The encoding is similar to Base64 with
/// non-URL safe characters replaced, and padding removed.
/// </summary>
/// <param name="obj">A valid <see cref="System.Guid" /> object.</param>
/// <returns>A 22 character URL-safe Base64 string.</returns>
public static string EncodeGuid(Guid obj)
{
var encoded = Convert.ToBase64String(obj.ToByteArray());
encoded = encoded
.Replace("/", "_")
.Replace("+", "-");
return encoded.Substring(0, 22);
}
/// <summary>
/// Decode Squid string to a <see cref="System.Guid" />.
/// See also <seealso cref="TryDecode(string, out System.Guid)" /> or
/// <seealso cref="TryParse(string, out System.Guid)" />.
/// </summary>
/// <param name="value">A valid Squid encoded string.</param>
/// <returns>A new <see cref="System.Guid" /> object from the parsed string.</returns>
public static Guid DecodeSquid(string value)
{
if (value == null) return Empty;
value = value
.Replace("_", "/")
.Replace("-", "+");
var blob = Convert.FromBase64String(value + "==");
return new Guid(blob);
}
/// <summary>
/// Squid to Guid.
/// </summary>
/// <param name="obj">A valid Squid object.</param>
/// <returns>System.Guid object.</returns>
public static Guid FromSquid(Squid obj)
{
return obj.Guid;
}
/// <summary>
/// String to Squid.
/// </summary>
/// <param name="value">String value to convert</param>
/// <returns>A Squid object.</returns>
public static Squid FromString(string value)
{
if (string.IsNullOrEmpty(value))
return Empty;
return TryParse(value, out Squid obj) ? obj : Empty;
}
/// <summary>
/// Decodes the given value to a <see cref="System.Guid" />.
/// </summary>
/// <param name="value">The Squid encoded string to decode.</param>
/// <param name="obj">A new <see cref="System.Guid" /> object from the parsed string.</param>
/// <returns>A boolean indicating if the decode was successful.</returns>
public static bool TryDecode(string value, out Guid obj)
{
try
{
// Decode as Squid
obj = DecodeSquid(value);
return true;
}
catch (Exception)
{
// Return empty Guid
obj = Guid.Empty;
return false;
}
}
/// <summary>
/// Tries to parse the given string value and
/// outputs the <see cref="Squid" /> object.
/// </summary>
/// <param name="value">The Squid encoded string or string representation of a Guid.</param>
/// <param name="obj">A new <see cref="Squid" /> object from the parsed string.</param>
/// <returns>A boolean indicating if the parse was successful.</returns>
public static bool TryParse(string value, out Squid obj)
{
// Parse as Squid string.
if (TryDecode(value, out var oGuid))
{
obj = oGuid;
return true;
}
// Parse as Guid string.
if (Guid.TryParse(value, out oGuid))
{
obj = oGuid;
return true;
}
obj = Empty;
return false;
}
/// <summary>
/// Tries to parse the string value and
/// outputs the underlying <see cref="System.Guid" /> object.
/// </summary>
/// <param name="value">The Squid encoded string or string representation of a Guid.</param>
/// <param name="obj">A new <see cref="System.Guid" /> object from the parsed string.</param>
/// <returns>A boolean indicating if the parse was successful.</returns>
public static bool TryParse(string value, out Guid obj)
{
// Try a Squid string.
if (TryDecode(value, out obj))
return true;
// Try a Guid string.
if (Guid.TryParse(value, out obj))
return true;
obj = Guid.Empty;
return false;
}
#region Operators
/// <summary>
/// Determines if both Squid objects have the same
/// underlying <see cref="System.Guid" /> value.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(Squid x, Squid y)
{
return x.Guid == y.Guid;
}
/// <summary>
/// Determines if both objects have the same
/// underlying <see cref="System.Guid" /> value.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(Squid x, Guid y)
{
return x.Guid == y;
}
/// <summary>
/// Determines if both objects have the same
/// underlying <see cref="System.Guid" /> value.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(Guid x, Squid y)
{
return y == x; // NB: order of arguments
}
/// <summary>
/// Determines if both Squid objects do not have the same
/// underlying <see cref="System.Guid" /> value.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(Squid x, Squid y)
{
return !(x == y);
}
/// <summary>
/// Determines if both objects do not have the same
/// underlying <see cref="System.Guid" /> value.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(Squid x, Guid y)
{
return !(x == y);
}
/// <summary>
/// Determines if both objects do not have the same
/// underlying <see cref="System.Guid" /> value.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(Guid x, Squid y)
{
return !(x == y);
}
/// <summary>
/// Implicitly converts the Squid to
/// its string equivalent.
/// </summary>
/// <param name="oSquid">The o squid.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator string(Squid oSquid)
{
return oSquid.Value;
}
/// <summary>
/// Implicitly converts the Squid to
/// its <see cref="System.Guid" /> equivalent.
/// </summary>
/// <param name="oSquid">The o squid.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Guid(Squid oSquid)
{
return oSquid.Guid;
}
/// <summary>
/// Implicitly converts the string to a Squid.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Squid(string value)
{
if (string.IsNullOrEmpty(value))
return Empty;
return TryParse(value, out Squid oSquid) ? oSquid : Empty;
}
/// <summary>
/// Implicitly converts the <see cref="System.Guid" /> to a Squid.
/// </summary>
/// <param name="oGuid">The o unique identifier.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Squid(Guid oGuid)
{
return oGuid == Guid.Empty ? Empty : new Squid(oGuid);
}
#endregion
}