forked from reactjs/React.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReactIdGenerator.cs
68 lines (58 loc) · 2.09 KB
/
ReactIdGenerator.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
/*
* Copyright (c) 2016-Present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
using System;
using System.Threading;
namespace React
{
/// <summary>
/// React ID generator.
/// </summary>
public class ReactIdGenerator : IReactIdGenerator
{
private static readonly string _encode32Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
private static long _random = DateTime.UtcNow.Ticks;
private static readonly char[] reactPrefix = "react_".ToCharArray();
/// <summary>
/// "react_".Length = 6 + 13 random symbols
/// </summary>
private const int reactIdLength = 19;
[ThreadStatic]
private static char[] _chars;
/// <summary>
/// Returns a short react identifier starts with "react_".
/// </summary>
/// <returns></returns>
public string Generate()
{
var chars = _chars;
if (chars == null)
{
_chars = chars = new char[reactIdLength];
Array.Copy(reactPrefix, 0, chars, 0, reactPrefix.Length);
}
var id = Interlocked.Increment(ref _random);
// from 6 because "react_".Length == 6, _encode32Chars.Length == 32 (base32),
// base32 characters are 5 bits in length and from long (64 bits) we can get 13 symbols
chars[6] = _encode32Chars[(int)(id >> 60) & 31];
chars[7] = _encode32Chars[(int)(id >> 55) & 31];
chars[8] = _encode32Chars[(int)(id >> 50) & 31];
chars[9] = _encode32Chars[(int)(id >> 45) & 31];
chars[10] = _encode32Chars[(int)(id >> 40) & 31];
chars[11] = _encode32Chars[(int)(id >> 35) & 31];
chars[12] = _encode32Chars[(int)(id >> 30) & 31];
chars[13] = _encode32Chars[(int)(id >> 25) & 31];
chars[14] = _encode32Chars[(int)(id >> 20) & 31];
chars[15] = _encode32Chars[(int)(id >> 15) & 31];
chars[16] = _encode32Chars[(int)(id >> 10) & 31];
chars[17] = _encode32Chars[(int)(id >> 5) & 31];
chars[18] = _encode32Chars[(int)id & 31];
return new string(chars, 0, reactIdLength);
}
}
}