ulid-transform 1.5.2__cp314-cp314-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,283 @@
1
+ #ifndef ULID_STRUCT_HH
2
+ #define ULID_STRUCT_HH
3
+
4
+ #include <array>
5
+ #include <chrono>
6
+ #include <cstdlib>
7
+ #include <ctime>
8
+ #include <functional>
9
+ #include <random>
10
+ #include <thread>
11
+ #include <vector>
12
+
13
+ #if _MSC_VER > 0
14
+ typedef uint32_t rand_t;
15
+ #else
16
+ typedef uint8_t rand_t;
17
+ #endif
18
+
19
+ namespace ulid {
20
+
21
+ /**
22
+ * ULID is a 16 byte Universally Unique Lexicographically Sortable Identifier
23
+ * */
24
+ struct ULID {
25
+ uint8_t data[16];
26
+ };
27
+
28
+ /**
29
+ * EncodeTimestamp will encode the int64_t timestamp to the passed ulid
30
+ * */
31
+ inline void EncodeTimestamp(int64_t timestamp, ULID& ulid)
32
+ {
33
+ ulid.data[0] = static_cast<uint8_t>(timestamp >> 40);
34
+ ulid.data[1] = static_cast<uint8_t>(timestamp >> 32);
35
+ ulid.data[2] = static_cast<uint8_t>(timestamp >> 24);
36
+ ulid.data[3] = static_cast<uint8_t>(timestamp >> 16);
37
+ ulid.data[4] = static_cast<uint8_t>(timestamp >> 8);
38
+ ulid.data[5] = static_cast<uint8_t>(timestamp);
39
+ }
40
+
41
+ /**
42
+ * EncodeTime will encode the time point to the passed ulid
43
+ * */
44
+ inline void EncodeTime(std::chrono::time_point<std::chrono::system_clock> time_point, ULID& ulid)
45
+ {
46
+ auto time_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(time_point);
47
+ int64_t timestamp = time_ms.time_since_epoch().count();
48
+ EncodeTimestamp(timestamp, ulid);
49
+ }
50
+
51
+ /**
52
+ * EncodeTimeSystemClockNow will encode a ULID using the time obtained using
53
+ * std::chrono::system_clock::now() by taking the timestamp in milliseconds.
54
+ * */
55
+ inline void EncodeTimeSystemClockNow(ULID& ulid)
56
+ {
57
+ EncodeTime(std::chrono::system_clock::now(), ulid);
58
+ }
59
+
60
+ /**
61
+ * EncodeEntropyMt19937Fast will encode using std::mt19937
62
+ * with only 3 generated values.
63
+ * */
64
+ inline void EncodeEntropyMt19937Fast(ULID& ulid)
65
+ {
66
+ static thread_local std::mt19937 gen([]() {
67
+ // Use multiple entropy sources for seeding
68
+ std::array<uint32_t, 3> seed_data = {
69
+ static_cast<uint32_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count()),
70
+ static_cast<uint32_t>(std::random_device {}()),
71
+ static_cast<uint32_t>(std::hash<std::thread::id> {}(std::this_thread::get_id()))
72
+ };
73
+ std::seed_seq seed_seq(seed_data.begin(), seed_data.end());
74
+ return std::mt19937(seed_seq);
75
+ }());
76
+ uint64_t high = (static_cast<uint64_t>(gen()) << 32) | gen();
77
+ uint32_t low = gen();
78
+ ulid.data[6] = (high >> 40) & 0xFF;
79
+ ulid.data[7] = (high >> 32) & 0xFF;
80
+ ulid.data[8] = (high >> 24) & 0xFF;
81
+ ulid.data[9] = (high >> 16) & 0xFF;
82
+ ulid.data[10] = (high >> 8) & 0xFF;
83
+ ulid.data[11] = high & 0xFF;
84
+ ulid.data[12] = (low >> 24) & 0xFF;
85
+ ulid.data[13] = (low >> 16) & 0xFF;
86
+ ulid.data[14] = (low >> 8) & 0xFF;
87
+ ulid.data[15] = low & 0xFF;
88
+ }
89
+
90
+ /**
91
+ * Crockford's Base32
92
+ * */
93
+ static const char Encoding[33] = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
94
+
95
+ /**
96
+ * MarshalTo will marshal a ULID to the passed character array.
97
+ *
98
+ * Implementation taken directly from oklog/ulid
99
+ * (https://sourcegraph.com/github.com/oklog/ulid@0774f81f6e44af5ce5e91c8d7d76cf710e889ebb/-/blob/ulid.go#L162-190)
100
+ *
101
+ * timestamp:<br>
102
+ * dst[0]: first 3 bits of data[0]<br>
103
+ * dst[1]: last 5 bits of data[0]<br>
104
+ * dst[2]: first 5 bits of data[1]<br>
105
+ * dst[3]: last 3 bits of data[1] + first 2 bits of data[2]<br>
106
+ * dst[4]: bits 3-7 of data[2]<br>
107
+ * dst[5]: last bit of data[2] + first 4 bits of data[3]<br>
108
+ * dst[6]: last 4 bits of data[3] + first bit of data[4]<br>
109
+ * dst[7]: bits 2-6 of data[4]<br>
110
+ * dst[8]: last 2 bits of data[4] + first 3 bits of data[5]<br>
111
+ * dst[9]: last 5 bits of data[5]<br>
112
+ *
113
+ * entropy:
114
+ * follows similarly, except now all components are set to 5 bits.
115
+ * */
116
+ inline void MarshalTo(const ULID& ulid, char dst[26])
117
+ {
118
+ // 10 byte timestamp
119
+ dst[0] = Encoding[(ulid.data[0] & 224) >> 5];
120
+ dst[1] = Encoding[ulid.data[0] & 31];
121
+ dst[2] = Encoding[(ulid.data[1] & 248) >> 3];
122
+ dst[3] = Encoding[((ulid.data[1] & 7) << 2) | ((ulid.data[2] & 192) >> 6)];
123
+ dst[4] = Encoding[(ulid.data[2] & 62) >> 1];
124
+ dst[5] = Encoding[((ulid.data[2] & 1) << 4) | ((ulid.data[3] & 240) >> 4)];
125
+ dst[6] = Encoding[((ulid.data[3] & 15) << 1) | ((ulid.data[4] & 128) >> 7)];
126
+ dst[7] = Encoding[(ulid.data[4] & 124) >> 2];
127
+ dst[8] = Encoding[((ulid.data[4] & 3) << 3) | ((ulid.data[5] & 224) >> 5)];
128
+ dst[9] = Encoding[ulid.data[5] & 31];
129
+
130
+ // 16 bytes of entropy
131
+ dst[10] = Encoding[(ulid.data[6] & 248) >> 3];
132
+ dst[11] = Encoding[((ulid.data[6] & 7) << 2) | ((ulid.data[7] & 192) >> 6)];
133
+ dst[12] = Encoding[(ulid.data[7] & 62) >> 1];
134
+ dst[13] = Encoding[((ulid.data[7] & 1) << 4) | ((ulid.data[8] & 240) >> 4)];
135
+ dst[14] = Encoding[((ulid.data[8] & 15) << 1) | ((ulid.data[9] & 128) >> 7)];
136
+ dst[15] = Encoding[(ulid.data[9] & 124) >> 2];
137
+ dst[16] = Encoding[((ulid.data[9] & 3) << 3) | ((ulid.data[10] & 224) >> 5)];
138
+ dst[17] = Encoding[ulid.data[10] & 31];
139
+ dst[18] = Encoding[(ulid.data[11] & 248) >> 3];
140
+ dst[19] = Encoding[((ulid.data[11] & 7) << 2) | ((ulid.data[12] & 192) >> 6)];
141
+ dst[20] = Encoding[(ulid.data[12] & 62) >> 1];
142
+ dst[21] = Encoding[((ulid.data[12] & 1) << 4) | ((ulid.data[13] & 240) >> 4)];
143
+ dst[22] = Encoding[((ulid.data[13] & 15) << 1) | ((ulid.data[14] & 128) >> 7)];
144
+ dst[23] = Encoding[(ulid.data[14] & 124) >> 2];
145
+ dst[24] = Encoding[((ulid.data[14] & 3) << 3) | ((ulid.data[15] & 224) >> 5)];
146
+ dst[25] = Encoding[ulid.data[15] & 31];
147
+ }
148
+
149
+ /**
150
+ * MarshalBinaryTo will Marshal a ULID to the passed byte array
151
+ * */
152
+ inline void MarshalBinaryTo(const ULID& ulid, uint8_t dst[16])
153
+ {
154
+ // timestamp
155
+ dst[0] = ulid.data[0];
156
+ dst[1] = ulid.data[1];
157
+ dst[2] = ulid.data[2];
158
+ dst[3] = ulid.data[3];
159
+ dst[4] = ulid.data[4];
160
+ dst[5] = ulid.data[5];
161
+
162
+ // entropy
163
+ dst[6] = ulid.data[6];
164
+ dst[7] = ulid.data[7];
165
+ dst[8] = ulid.data[8];
166
+ dst[9] = ulid.data[9];
167
+ dst[10] = ulid.data[10];
168
+ dst[11] = ulid.data[11];
169
+ dst[12] = ulid.data[12];
170
+ dst[13] = ulid.data[13];
171
+ dst[14] = ulid.data[14];
172
+ dst[15] = ulid.data[15];
173
+ }
174
+
175
+ /**
176
+ * dec storesdecimal encodings for characters.
177
+ * 0xFF indicates invalid character.
178
+ * 48-57 are digits.
179
+ * 65-90 are capital alphabets.
180
+ * */
181
+ static const uint8_t dec[256] = {
182
+ 0xFF, 0xFF, 0xFF, 0xFF, 0x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0xFF, 0xFF, 0xFF,
209
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
210
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
211
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
212
+
213
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
214
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
215
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
216
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
217
+
218
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
219
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
220
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
221
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
222
+
223
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
224
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
225
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
226
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
227
+ };
228
+
229
+ /**
230
+ * UnmarshalFrom will unmarshal a ULID from the passed character array.
231
+ * */
232
+ inline void UnmarshalFrom(const char str[26], ULID& ulid)
233
+ {
234
+ // timestamp
235
+ ulid.data[0] = (dec[int(str[0])] << 5) | dec[int(str[1])];
236
+ ulid.data[1] = (dec[int(str[2])] << 3) | (dec[int(str[3])] >> 2);
237
+ ulid.data[2] = (dec[int(str[3])] << 6) | (dec[int(str[4])] << 1) | (dec[int(str[5])] >> 4);
238
+ ulid.data[3] = (dec[int(str[5])] << 4) | (dec[int(str[6])] >> 1);
239
+ ulid.data[4] = (dec[int(str[6])] << 7) | (dec[int(str[7])] << 2) | (dec[int(str[8])] >> 3);
240
+ ulid.data[5] = (dec[int(str[8])] << 5) | dec[int(str[9])];
241
+
242
+ // entropy
243
+ ulid.data[6] = (dec[int(str[10])] << 3) | (dec[int(str[11])] >> 2);
244
+ ulid.data[7] = (dec[int(str[11])] << 6) | (dec[int(str[12])] << 1) | (dec[int(str[13])] >> 4);
245
+ ulid.data[8] = (dec[int(str[13])] << 4) | (dec[int(str[14])] >> 1);
246
+ ulid.data[9] = (dec[int(str[14])] << 7) | (dec[int(str[15])] << 2) | (dec[int(str[16])] >> 3);
247
+ ulid.data[10] = (dec[int(str[16])] << 5) | dec[int(str[17])];
248
+ ulid.data[11] = (dec[int(str[18])] << 3) | (dec[int(str[19])] >> 2);
249
+ ulid.data[12] = (dec[int(str[19])] << 6) | (dec[int(str[20])] << 1) | (dec[int(str[21])] >> 4);
250
+ ulid.data[13] = (dec[int(str[21])] << 4) | (dec[int(str[22])] >> 1);
251
+ ulid.data[14] = (dec[int(str[22])] << 7) | (dec[int(str[23])] << 2) | (dec[int(str[24])] >> 3);
252
+ ulid.data[15] = (dec[int(str[24])] << 5) | dec[int(str[25])];
253
+ }
254
+
255
+ /**
256
+ * UnmarshalBinaryFrom will unmarshal a ULID from the passed byte array.
257
+ * */
258
+ inline void UnmarshalBinaryFrom(const uint8_t b[16], ULID& ulid)
259
+ {
260
+ // timestamp
261
+ ulid.data[0] = b[0];
262
+ ulid.data[1] = b[1];
263
+ ulid.data[2] = b[2];
264
+ ulid.data[3] = b[3];
265
+ ulid.data[4] = b[4];
266
+ ulid.data[5] = b[5];
267
+
268
+ // entropy
269
+ ulid.data[6] = b[6];
270
+ ulid.data[7] = b[7];
271
+ ulid.data[8] = b[8];
272
+ ulid.data[9] = b[9];
273
+ ulid.data[10] = b[10];
274
+ ulid.data[11] = b[11];
275
+ ulid.data[12] = b[12];
276
+ ulid.data[13] = b[13];
277
+ ulid.data[14] = b[14];
278
+ ulid.data[15] = b[15];
279
+ }
280
+
281
+ }; // namespace ulid
282
+
283
+ #endif // ULID_STRUCT_HH
@@ -0,0 +1,346 @@
1
+ #ifndef ULID_UINT128_HH
2
+ #define ULID_UINT128_HH
3
+
4
+ #include <array>
5
+ #include <chrono>
6
+ #include <cstdlib>
7
+ #include <ctime>
8
+ #include <functional>
9
+ #include <random>
10
+ #include <vector>
11
+
12
+ #if _MSC_VER > 0
13
+ typedef uint32_t rand_t;
14
+ #else
15
+ typedef uint8_t rand_t;
16
+ #endif
17
+
18
+ namespace ulid {
19
+
20
+ /**
21
+ * ULID is a 16 byte Universally Unique Lexicographically Sortable Identifier
22
+ * */
23
+ typedef __uint128_t ULID;
24
+
25
+ /**
26
+ * EncodeTimestamp will encode the int64_t timestamp to the passed ulid
27
+ * */
28
+ inline void EncodeTimestamp(int64_t timestamp, ULID& ulid)
29
+ {
30
+ ULID t = static_cast<uint8_t>(timestamp >> 40);
31
+
32
+ t <<= 8;
33
+ t |= static_cast<uint8_t>(timestamp >> 32);
34
+
35
+ t <<= 8;
36
+ t |= static_cast<uint8_t>(timestamp >> 24);
37
+
38
+ t <<= 8;
39
+ t |= static_cast<uint8_t>(timestamp >> 16);
40
+
41
+ t <<= 8;
42
+ t |= static_cast<uint8_t>(timestamp >> 8);
43
+
44
+ t <<= 8;
45
+ t |= static_cast<uint8_t>(timestamp);
46
+
47
+ t <<= 80;
48
+
49
+ ULID mask = 1;
50
+ mask <<= 80;
51
+ mask--;
52
+
53
+ ulid = t | (ulid & mask);
54
+ }
55
+
56
+ /**
57
+ * EncodeTime will encode the time point to the passed ulid
58
+ * */
59
+ inline void EncodeTime(std::chrono::time_point<std::chrono::system_clock> time_point, ULID& ulid)
60
+ {
61
+ auto time_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(time_point);
62
+ int64_t timestamp = time_ms.time_since_epoch().count();
63
+ EncodeTimestamp(timestamp, ulid);
64
+ }
65
+
66
+ /**
67
+ * EncodeTimeSystemClockNow will encode a ULID using the time obtained using
68
+ * std::chrono::system_clock::now() by taking the timestamp in milliseconds.
69
+ * */
70
+ inline void EncodeTimeSystemClockNow(ULID& ulid)
71
+ {
72
+ EncodeTime(std::chrono::system_clock::now(), ulid);
73
+ }
74
+
75
+ /**
76
+ * EncodeEntropyMt19937Fast will encode using std::mt19937
77
+ * with only 3 generated values.
78
+ * */
79
+ inline void EncodeEntropyMt19937Fast(ULID& ulid)
80
+ {
81
+ static thread_local std::mt19937 gen([]() {
82
+ // Use multiple entropy sources for seeding
83
+ std::array<uint32_t, 3> seed_data = {
84
+ static_cast<uint32_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count()),
85
+ static_cast<uint32_t>(std::random_device {}()),
86
+ static_cast<uint32_t>(reinterpret_cast<uintptr_t>(&gen) & 0xFFFFFFFF)
87
+ };
88
+ std::seed_seq seed_seq(seed_data.begin(), seed_data.end());
89
+ return std::mt19937(seed_seq);
90
+ }());
91
+ ulid = (ulid >> 80) << 80; // Clear lower 80 bits
92
+ ulid |= (static_cast<ULID>((static_cast<uint64_t>(gen()) << 32) | gen()) << 16) | (gen() & 0xFFFF);
93
+ }
94
+
95
+ /**
96
+ * Crockford's Base32
97
+ * */
98
+ static const char Encoding[33] = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
99
+
100
+ /**
101
+ * MarshalTo will marshal a ULID to the passed character array.
102
+ *
103
+ * Implementation taken directly from oklog/ulid
104
+ * (https://sourcegraph.com/github.com/oklog/ulid@0774f81f6e44af5ce5e91c8d7d76cf710e889ebb/-/blob/ulid.go#L162-190)
105
+ *
106
+ * timestamp:
107
+ * dst[0]: first 3 bits of data[0]
108
+ * dst[1]: last 5 bits of data[0]
109
+ * dst[2]: first 5 bits of data[1]
110
+ * dst[3]: last 3 bits of data[1] + first 2 bits of data[2]
111
+ * dst[4]: bits 3-7 of data[2]
112
+ * dst[5]: last bit of data[2] + first 4 bits of data[3]
113
+ * dst[6]: last 4 bits of data[3] + first bit of data[4]
114
+ * dst[7]: bits 2-6 of data[4]
115
+ * dst[8]: last 2 bits of data[4] + first 3 bits of data[5]
116
+ * dst[9]: last 5 bits of data[5]
117
+ *
118
+ * entropy:
119
+ * follows similarly, except now all components are set to 5 bits.
120
+ * */
121
+ inline void MarshalTo(const ULID& ulid, char dst[26])
122
+ {
123
+ // 10 byte timestamp
124
+ dst[0] = Encoding[(static_cast<uint8_t>(ulid >> 120) & 224) >> 5];
125
+ dst[1] = Encoding[static_cast<uint8_t>(ulid >> 120) & 31];
126
+ dst[2] = Encoding[(static_cast<uint8_t>(ulid >> 112) & 248) >> 3];
127
+ dst[3] = Encoding[((static_cast<uint8_t>(ulid >> 112) & 7) << 2) | ((static_cast<uint8_t>(ulid >> 104) & 192) >> 6)];
128
+ dst[4] = Encoding[(static_cast<uint8_t>(ulid >> 104) & 62) >> 1];
129
+ dst[5] = Encoding[((static_cast<uint8_t>(ulid >> 104) & 1) << 4) | ((static_cast<uint8_t>(ulid >> 96) & 240) >> 4)];
130
+ dst[6] = Encoding[((static_cast<uint8_t>(ulid >> 96) & 15) << 1) | ((static_cast<uint8_t>(ulid >> 88) & 128) >> 7)];
131
+ dst[7] = Encoding[(static_cast<uint8_t>(ulid >> 88) & 124) >> 2];
132
+ dst[8] = Encoding[((static_cast<uint8_t>(ulid >> 88) & 3) << 3) | ((static_cast<uint8_t>(ulid >> 80) & 224) >> 5)];
133
+ dst[9] = Encoding[static_cast<uint8_t>(ulid >> 80) & 31];
134
+
135
+ // 16 bytes of entropy
136
+ dst[10] = Encoding[(static_cast<uint8_t>(ulid >> 72) & 248) >> 3];
137
+ dst[11] = Encoding[((static_cast<uint8_t>(ulid >> 72) & 7) << 2) | ((static_cast<uint8_t>(ulid >> 64) & 192) >> 6)];
138
+ dst[12] = Encoding[(static_cast<uint8_t>(ulid >> 64) & 62) >> 1];
139
+ dst[13] = Encoding[((static_cast<uint8_t>(ulid >> 64) & 1) << 4) | ((static_cast<uint8_t>(ulid >> 56) & 240) >> 4)];
140
+ dst[14] = Encoding[((static_cast<uint8_t>(ulid >> 56) & 15) << 1) | ((static_cast<uint8_t>(ulid >> 48) & 128) >> 7)];
141
+ dst[15] = Encoding[(static_cast<uint8_t>(ulid >> 48) & 124) >> 2];
142
+ dst[16] = Encoding[((static_cast<uint8_t>(ulid >> 48) & 3) << 3) | ((static_cast<uint8_t>(ulid >> 40) & 224) >> 5)];
143
+ dst[17] = Encoding[static_cast<uint8_t>(ulid >> 40) & 31];
144
+ dst[18] = Encoding[(static_cast<uint8_t>(ulid >> 32) & 248) >> 3];
145
+ dst[19] = Encoding[((static_cast<uint8_t>(ulid >> 32) & 7) << 2) | ((static_cast<uint8_t>(ulid >> 24) & 192) >> 6)];
146
+ dst[20] = Encoding[(static_cast<uint8_t>(ulid >> 24) & 62) >> 1];
147
+ dst[21] = Encoding[((static_cast<uint8_t>(ulid >> 24) & 1) << 4) | ((static_cast<uint8_t>(ulid >> 16) & 240) >> 4)];
148
+ dst[22] = Encoding[((static_cast<uint8_t>(ulid >> 16) & 15) << 1) | ((static_cast<uint8_t>(ulid >> 8) & 128) >> 7)];
149
+ dst[23] = Encoding[(static_cast<uint8_t>(ulid >> 8) & 124) >> 2];
150
+ dst[24] = Encoding[((static_cast<uint8_t>(ulid >> 8) & 3) << 3) | (((static_cast<uint8_t>(ulid)) & 224) >> 5)];
151
+ dst[25] = Encoding[(static_cast<uint8_t>(ulid)) & 31];
152
+ }
153
+
154
+ /**
155
+ * MarshalBinaryTo will Marshal a ULID to the passed byte array
156
+ * */
157
+ inline void MarshalBinaryTo(const ULID& ulid, uint8_t dst[16])
158
+ {
159
+ // timestamp
160
+ dst[0] = static_cast<uint8_t>(ulid >> 120);
161
+ dst[1] = static_cast<uint8_t>(ulid >> 112);
162
+ dst[2] = static_cast<uint8_t>(ulid >> 104);
163
+ dst[3] = static_cast<uint8_t>(ulid >> 96);
164
+ dst[4] = static_cast<uint8_t>(ulid >> 88);
165
+ dst[5] = static_cast<uint8_t>(ulid >> 80);
166
+
167
+ // entropy
168
+ dst[6] = static_cast<uint8_t>(ulid >> 72);
169
+ dst[7] = static_cast<uint8_t>(ulid >> 64);
170
+ dst[8] = static_cast<uint8_t>(ulid >> 56);
171
+ dst[9] = static_cast<uint8_t>(ulid >> 48);
172
+ dst[10] = static_cast<uint8_t>(ulid >> 40);
173
+ dst[11] = static_cast<uint8_t>(ulid >> 32);
174
+ dst[12] = static_cast<uint8_t>(ulid >> 24);
175
+ dst[13] = static_cast<uint8_t>(ulid >> 16);
176
+ dst[14] = static_cast<uint8_t>(ulid >> 8);
177
+ dst[15] = static_cast<uint8_t>(ulid);
178
+ }
179
+
180
+ /**
181
+ * dec storesdecimal encodings for characters.
182
+ * 0xFF indicates invalid character.
183
+ * 48-57 are digits.
184
+ * 65-90 are capital alphabets.
185
+ * */
186
+ static const uint8_t dec[256] = {
187
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
188
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
189
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
190
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
191
+
192
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
193
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
194
+ /* 0 1 2 3 4 5 6 7 */
195
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
196
+ /* 8 9 */
197
+ 0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
198
+
199
+ /* 10(A) 11(B) 12(C) 13(D) 14(E) 15(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FF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
225
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
226
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
227
+
228
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
229
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
230
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
231
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
232
+ };
233
+
234
+ /**
235
+ * UnmarshalFrom will unmarshal a ULID from the passed character array.
236
+ * */
237
+ inline void UnmarshalFrom(const char str[26], ULID& ulid)
238
+ {
239
+ // timestamp
240
+ ulid = (dec[int(str[0])] << 5) | dec[int(str[1])];
241
+
242
+ ulid <<= 8;
243
+ ulid |= (dec[int(str[2])] << 3) | (dec[int(str[3])] >> 2);
244
+
245
+ ulid <<= 8;
246
+ ulid |= (dec[int(str[3])] << 6) | (dec[int(str[4])] << 1) | (dec[int(str[5])] >> 4);
247
+
248
+ ulid <<= 8;
249
+ ulid |= (dec[int(str[5])] << 4) | (dec[int(str[6])] >> 1);
250
+
251
+ ulid <<= 8;
252
+ ulid |= (dec[int(str[6])] << 7) | (dec[int(str[7])] << 2) | (dec[int(str[8])] >> 3);
253
+
254
+ ulid <<= 8;
255
+ ulid |= (dec[int(str[8])] << 5) | dec[int(str[9])];
256
+
257
+ // entropy
258
+ ulid <<= 8;
259
+ ulid |= (dec[int(str[10])] << 3) | (dec[int(str[11])] >> 2);
260
+
261
+ ulid <<= 8;
262
+ ulid |= (dec[int(str[11])] << 6) | (dec[int(str[12])] << 1) | (dec[int(str[13])] >> 4);
263
+
264
+ ulid <<= 8;
265
+ ulid |= (dec[int(str[13])] << 4) | (dec[int(str[14])] >> 1);
266
+
267
+ ulid <<= 8;
268
+ ulid |= (dec[int(str[14])] << 7) | (dec[int(str[15])] << 2) | (dec[int(str[16])] >> 3);
269
+
270
+ ulid <<= 8;
271
+ ulid |= (dec[int(str[16])] << 5) | dec[int(str[17])];
272
+
273
+ ulid <<= 8;
274
+ ulid |= (dec[int(str[18])] << 3) | (dec[int(str[19])] >> 2);
275
+
276
+ ulid <<= 8;
277
+ ulid |= (dec[int(str[19])] << 6) | (dec[int(str[20])] << 1) | (dec[int(str[21])] >> 4);
278
+
279
+ ulid <<= 8;
280
+ ulid |= (dec[int(str[21])] << 4) | (dec[int(str[22])] >> 1);
281
+
282
+ ulid <<= 8;
283
+ ulid |= (dec[int(str[22])] << 7) | (dec[int(str[23])] << 2) | (dec[int(str[24])] >> 3);
284
+
285
+ ulid <<= 8;
286
+ ulid |= (dec[int(str[24])] << 5) | dec[int(str[25])];
287
+ }
288
+
289
+ /**
290
+ * UnmarshalBinaryFrom will unmarshal a ULID from the passed byte array.
291
+ * */
292
+ inline void UnmarshalBinaryFrom(const uint8_t b[16], ULID& ulid)
293
+ {
294
+ // timestamp
295
+ ulid = b[0];
296
+
297
+ ulid <<= 8;
298
+ ulid |= b[1];
299
+
300
+ ulid <<= 8;
301
+ ulid |= b[2];
302
+
303
+ ulid <<= 8;
304
+ ulid |= b[3];
305
+
306
+ ulid <<= 8;
307
+ ulid |= b[4];
308
+
309
+ ulid <<= 8;
310
+ ulid |= b[5];
311
+
312
+ // entropy
313
+ ulid <<= 8;
314
+ ulid |= b[6];
315
+
316
+ ulid <<= 8;
317
+ ulid |= b[7];
318
+
319
+ ulid <<= 8;
320
+ ulid |= b[8];
321
+
322
+ ulid <<= 8;
323
+ ulid |= b[9];
324
+
325
+ ulid <<= 8;
326
+ ulid |= b[10];
327
+
328
+ ulid <<= 8;
329
+ ulid |= b[11];
330
+
331
+ ulid <<= 8;
332
+ ulid |= b[12];
333
+
334
+ ulid <<= 8;
335
+ ulid |= b[13];
336
+
337
+ ulid <<= 8;
338
+ ulid |= b[14];
339
+
340
+ ulid <<= 8;
341
+ ulid |= b[15];
342
+ }
343
+
344
+ }; // namespace ulid
345
+
346
+ #endif // ULID_UINT128_HH