-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnowflakeEncoder.cs
309 lines (285 loc) · 10.8 KB
/
SnowflakeEncoder.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
using System.Diagnostics;
namespace Snowflakes;
/// <summary>Converts snowflakes to custom-base-encoded strings.</summary>
[DebuggerDisplay($"{{{nameof(DebuggerDisplay)},nq}}")]
public sealed class SnowflakeEncoder
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly string _digits;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly string _zeroDigit;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly Dictionary<char, long> _digitValues;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly int _maxEncodedLength;
private SnowflakeEncoder(string digits)
{
const int BitsInLong = 64;
_digits = digits;
_zeroDigit = digits[0].ToString();
_digitValues = new Dictionary<char, long>(digits.Length);
for (var i = 0; i < digits.Length; i++)
_digitValues[digits[i]] = i;
_maxEncodedLength = (int)Math.Ceiling(BitsInLong / Math.Log2(digits.Length));
}
/// <summary>
/// Gets an encoder that will convert a snowflake to its base 36 representation
/// using 0-9 and A-Z (in this order) as digits.
/// </summary>
/// <remarks>
/// <para>
/// Values produced by this encoder are safe to use in URIs.
/// </para>
/// <list type="table">
/// <listheader>
/// <term>Integer</term>
/// <description>Encoded</description>
/// </listheader>
/// <item>
/// <term>0</term>
/// <description>0</description>
/// </item>
/// <item>
/// <term>9</term>
/// <description>9</description>
/// </item>
/// <item>
/// <term>10</term>
/// <description>A</description>
/// </item>
/// <item>
/// <term>35</term>
/// <description>Z</description>
/// </item>
/// <item>
/// <term>36</term>
/// <description>10</description>
/// </item>
/// <item>
/// <term>46</term>
/// <description>1A</description>
/// </item>
/// </list>
/// </remarks>
public static SnowflakeEncoder Base36Upper { get; } =
new("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
/// <summary>
/// Gets an encoder that will convert a snowflake to its base 36 representation
/// using 0-9 and a-z (in this order) as digits.
/// </summary>
/// <remarks>
/// <para>
/// Values produced by this encoder are safe to use in URIs.
/// </para>
/// <list type="table">
/// <listheader>
/// <term>Integer</term>
/// <description>Encoded</description>
/// </listheader>
/// <item>
/// <term>0</term>
/// <description>0</description>
/// </item>
/// <item>
/// <term>9</term>
/// <description>9</description>
/// </item>
/// <item>
/// <term>10</term>
/// <description>a</description>
/// </item>
/// <item>
/// <term>35</term>
/// <description>z</description>
/// </item>
/// <item>
/// <term>36</term>
/// <description>10</description>
/// </item>
/// <item>
/// <term>46</term>
/// <description>1a</description>
/// </item>
/// </list>
/// </remarks>
public static SnowflakeEncoder Base36Lower { get; } =
new("0123456789abcdefghijklmnopqrstuvwxyz");
/// <summary>
/// Gets an encoder that will convert a snowflake to its base 62 representation
/// using 0-9, A-Z, and a-z (in this order) as digits.
/// </summary>
/// <remarks>
/// <para>
/// Values produced by this encoder are safe to use in URIs.
/// </para>
/// <list type="table">
/// <listheader>
/// <term>Integer</term>
/// <description>Encoded</description>
/// </listheader>
/// <item>
/// <term>0</term>
/// <description>0</description>
/// </item>
/// <item>
/// <term>9</term>
/// <description>9</description>
/// </item>
/// <item>
/// <term>10</term>
/// <description>A</description>
/// </item>
/// <item>
/// <term>35</term>
/// <description>Z</description>
/// </item>
/// <item>
/// <term>36</term>
/// <description>a</description>
/// </item>
/// <item>
/// <term>61</term>
/// <description>z</description>
/// </item>
/// <item>
/// <term>62</term>
/// <description>10</description>
/// </item>
/// <item>
/// <term>71</term>
/// <description>19</description>
/// </item>
/// <item>
/// <term>72</term>
/// <description>1A</description>
/// </item>
/// </list>
/// </remarks>
public static SnowflakeEncoder Base62 { get; } =
new("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
/// <summary>
/// Gets an encoder that will convert a snowflake to its base 62 representation
/// using -, 0-9, A-Z, _, and a-z (in this order) as digits.
/// </summary>
/// <remarks>
/// <para>
/// Note that this encoder represents an integer identifier in a specific base using
/// extra digits, similar to base 2 (binary digits: 0-1) and base 16 (hexadecimal
/// digits: 0-9, A-F). It should not be confused with the binary-to-text encoding
/// format that is also known as Base64. which is aimed to represent arbitrary
/// bytes rather than being a custom-base representation of numbers.
/// </para>
/// <para>
/// Values produced by this encoder are safe to use in URIs.
/// </para>
/// <list type="table">
/// <listheader>
/// <term>Integer</term>
/// <description>Encoded</description>
/// </listheader>
/// <item>
/// <term>0</term>
/// <description>-</description>
/// </item>
/// <item>
/// <term>1</term>
/// <description>0</description>
/// </item>
/// <item>
/// <term>10</term>
/// <description>9</description>
/// </item>
/// <item>
/// <term>11</term>
/// <description>A</description>
/// </item>
/// <item>
/// <term>36</term>
/// <description>Z</description>
/// </item>
/// <item>
/// <term>37</term>
/// <description>_</description>
/// </item>
/// <item>
/// <term>38</term>
/// <description>a</description>
/// </item>
/// <item>
/// <term>63</term>
/// <description>z</description>
/// </item>
/// <item>
/// <term>64</term>
/// <description>0-</description>
/// </item>
/// <item>
/// <term>65</term>
/// <description>00</description>
/// </item>
/// <item>
/// <term>74</term>
/// <description>09</description>
/// </item>
/// <item>
/// <term>75</term>
/// <description>0A</description>
/// </item>
/// </list>
/// </remarks>
public static SnowflakeEncoder Base64Snow { get; } =
new("-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz");
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay => $"Base ({_digits.Length}): {_digits}";
/// <summary>Converts the specified snowflake to a custom-base-encoded string.</summary>
/// <param name="snowflake">The snowflake to convert.</param>
/// <returns>The snowflake's representation in the encoder-specific format.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="snowflake" /> is negative.
/// </exception>
public string Encode(long snowflake)
{
ArgumentOutOfRangeException.ThrowIfNegative(snowflake);
if (snowflake == 0L)
return _zeroDigit;
Span<char> chars = stackalloc char[_maxEncodedLength];
var index = _maxEncodedLength - 1;
do
{
var remainder = (int)(snowflake % _digits.Length);
chars[index--] = _digits[remainder];
snowflake /= _digits.Length;
} while (snowflake != 0);
return chars[(index + 1).._maxEncodedLength].ToString();
}
/// <summary>Converts a custom-base-encoded snowflake back to its original value.</summary>
/// <param name="encodedSnowflake">The encoded snowflake to convert back.</param>
/// <returns>The original snowflake that was encoded.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="encodedSnowflake" /> is null.
/// </exception>
/// <exception cref="FormatException">
/// <paramref name="encodedSnowflake" /> is in an invalid format.
/// </exception>
/// <exception cref="OverflowException">
/// The number <paramref name="encodedSnowflake" /> represents is bigger than
/// <see cref="long.MaxValue" />.
/// </exception>
public long Decode(string encodedSnowflake)
{
ArgumentNullException.ThrowIfNull(encodedSnowflake);
if (encodedSnowflake.Length == 0)
throw new FormatException("Empty encoded snowflake.");
var result = 0L;
var multiplier = 1L;
for (var i = encodedSnowflake.Length - 1; i >= 0; i--)
{
if (!_digitValues.TryGetValue(encodedSnowflake[i], out var digit))
throw new FormatException(
$"Invalid character, '{encodedSnowflake[i]}' in encoded snowflake.");
result = checked(result + (digit * multiplier));
multiplier *= _digits.Length;
}
return result;
}
}