forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsEngineIdGenerator.cs
More file actions
44 lines (36 loc) · 1.52 KB
/
JsEngineIdGenerator.cs
File metadata and controls
44 lines (36 loc) · 1.52 KB
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
using System;
using System.Threading;
namespace JavaScriptEngineSwitcher.Node
{
internal static class JsEngineIdGenerator
{
// Base32 encoding - in ascii sort order for easy text based sorting
private static readonly char[] _encode32Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUV".ToCharArray();
// Seed the `_lastId` for this application instance with
// the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001
// for a roughly increasing `_lastId` over restarts
private static long _lastId = DateTime.UtcNow.Ticks;
public static string GetNextId() => GenerateId(Interlocked.Increment(ref _lastId));
private static unsafe string GenerateId(long id)
{
char[] encode32Chars = _encode32Chars;
// stackalloc to allocate array on stack rather than heap
char* buffer = stackalloc char[13];
buffer[12] = encode32Chars[id & 31];
buffer[11] = encode32Chars[(id >> 5) & 31];
buffer[10] = encode32Chars[(id >> 10) & 31];
buffer[9] = encode32Chars[(id >> 15) & 31];
buffer[8] = encode32Chars[(id >> 20) & 31];
buffer[7] = encode32Chars[(id >> 25) & 31];
buffer[6] = encode32Chars[(id >> 30) & 31];
buffer[5] = encode32Chars[(id >> 35) & 31];
buffer[4] = encode32Chars[(id >> 40) & 31];
buffer[3] = encode32Chars[(id >> 45) & 31];
buffer[2] = encode32Chars[(id >> 50) & 31];
buffer[1] = encode32Chars[(id >> 55) & 31];
buffer[0] = encode32Chars[(id >> 60) & 31];
// string `ctor` overload that takes `char*`
return new string(buffer, 0, 13);
}
}
}