ulid-transform 1.2.1__cp313-cp313-win_amd64.whl → 1.5.2__cp313-cp313-win_amd64.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.
@@ -1,6 +1,7 @@
1
1
  #ifndef ULID_UINT128_HH
2
2
  #define ULID_UINT128_HH
3
3
 
4
+ #include <array>
4
5
  #include <chrono>
5
6
  #include <cstdlib>
6
7
  #include <ctime>
@@ -10,7 +11,7 @@
10
11
 
11
12
  #if _MSC_VER > 0
12
13
  typedef uint32_t rand_t;
13
- # else
14
+ #else
14
15
  typedef uint8_t rand_t;
15
16
  #endif
16
17
 
@@ -24,211 +25,71 @@ typedef __uint128_t ULID;
24
25
  /**
25
26
  * EncodeTimestamp will encode the int64_t timestamp to the passed ulid
26
27
  * */
27
- inline void EncodeTimestamp(int64_t timestamp, ULID& ulid) {
28
- ULID t = static_cast<uint8_t>(timestamp >> 40);
28
+ inline void EncodeTimestamp(int64_t timestamp, ULID& ulid)
29
+ {
30
+ ULID t = static_cast<uint8_t>(timestamp >> 40);
29
31
 
30
- t <<= 8;
31
- t |= static_cast<uint8_t>(timestamp >> 32);
32
+ t <<= 8;
33
+ t |= static_cast<uint8_t>(timestamp >> 32);
32
34
 
33
- t <<= 8;
34
- t |= static_cast<uint8_t>(timestamp >> 24);
35
+ t <<= 8;
36
+ t |= static_cast<uint8_t>(timestamp >> 24);
35
37
 
36
- t <<= 8;
37
- t |= static_cast<uint8_t>(timestamp >> 16);
38
+ t <<= 8;
39
+ t |= static_cast<uint8_t>(timestamp >> 16);
38
40
 
39
- t <<= 8;
40
- t |= static_cast<uint8_t>(timestamp >> 8);
41
+ t <<= 8;
42
+ t |= static_cast<uint8_t>(timestamp >> 8);
41
43
 
42
- t <<= 8;
43
- t |= static_cast<uint8_t>(timestamp);
44
+ t <<= 8;
45
+ t |= static_cast<uint8_t>(timestamp);
44
46
 
45
- t <<= 80;
47
+ t <<= 80;
46
48
 
47
- ULID mask = 1;
48
- mask <<= 80;
49
- mask--;
49
+ ULID mask = 1;
50
+ mask <<= 80;
51
+ mask--;
50
52
 
51
- ulid = t | (ulid & mask);
53
+ ulid = t | (ulid & mask);
52
54
  }
53
55
 
54
56
  /**
55
57
  * EncodeTime will encode the time point to the passed ulid
56
58
  * */
57
- inline void EncodeTime(std::chrono::time_point<std::chrono::system_clock> time_point, ULID& ulid) {
58
- auto time_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(time_point);
59
- int64_t timestamp = time_ms.time_since_epoch().count();
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();
60
63
  EncodeTimestamp(timestamp, ulid);
61
64
  }
62
65
 
63
- /**
64
- * EncodeTimeNow will encode a ULID using the time obtained using std::time(nullptr)
65
- * */
66
- inline void EncodeTimeNow(ULID& ulid) {
67
- auto time_now = std::chrono::system_clock::from_time_t(time(nullptr));
68
- EncodeTime(time_now, ulid);
69
- }
70
-
71
66
  /**
72
67
  * EncodeTimeSystemClockNow will encode a ULID using the time obtained using
73
68
  * std::chrono::system_clock::now() by taking the timestamp in milliseconds.
74
69
  * */
75
- inline void EncodeTimeSystemClockNow(ULID& ulid) {
76
- EncodeTime(std::chrono::system_clock::now(), ulid);
77
- }
78
-
79
- /**
80
- * EncodeEntropy will encode the last 10 bytes of the passed uint8_t array with
81
- * the values generated using the passed random number generator.
82
- * */
83
- inline void EncodeEntropy(const std::function<uint8_t()>& rng, ULID& ulid) {
84
- ulid = (ulid >> 80) << 80;
85
-
86
- ULID e = rng();
87
-
88
- e <<= 8;
89
- e |= rng();
90
-
91
- e <<= 8;
92
- e |= rng();
93
-
94
- e <<= 8;
95
- e |= rng();
96
-
97
- e <<= 8;
98
- e |= rng();
99
-
100
- e <<= 8;
101
- e |= rng();
102
-
103
- e <<= 8;
104
- e |= rng();
105
-
106
- e <<= 8;
107
- e |= rng();
108
-
109
- e <<= 8;
110
- e |= rng();
111
-
112
- e <<= 8;
113
- e |= rng();
114
-
115
- ulid |= e;
116
- }
117
-
118
- /**
119
- * EncodeEntropyRand will encode a ulid using std::rand
120
- *
121
- * std::rand returns values in [0, RAND_MAX]
122
- * */
123
- inline void EncodeEntropyRand(ULID& ulid) {
124
- ulid = (ulid >> 80) << 80;
125
-
126
- ULID e = (std::rand() * 255ull) / RAND_MAX;
127
-
128
- e <<= 8;
129
- e |= (std::rand() * 255ull) / RAND_MAX;
130
-
131
- e <<= 8;
132
- e |= (std::rand() * 255ull) / RAND_MAX;
133
-
134
- e <<= 8;
135
- e |= (std::rand() * 255ull) / RAND_MAX;
136
-
137
- e <<= 8;
138
- e |= (std::rand() * 255ull) / RAND_MAX;
139
-
140
- e <<= 8;
141
- e |= (std::rand() * 255ull) / RAND_MAX;
142
-
143
- e <<= 8;
144
- e |= (std::rand() * 255ull) / RAND_MAX;
145
-
146
- e <<= 8;
147
- e |= (std::rand() * 255ull) / RAND_MAX;
148
-
149
- e <<= 8;
150
- e |= (std::rand() * 255ull) / RAND_MAX;
151
-
152
- e <<= 8;
153
- e |= (std::rand() * 255ull) / RAND_MAX;
154
-
155
- ulid |= e;
70
+ inline void EncodeTimeSystemClockNow(ULID& ulid)
71
+ {
72
+ EncodeTime(std::chrono::system_clock::now(), ulid);
156
73
  }
157
74
 
158
- static std::uniform_int_distribution<rand_t> Distribution_0_255(0, 255);
159
-
160
75
  /**
161
- * EncodeEntropyMt19937 will encode a ulid using std::mt19937
162
- *
163
- * It also creates a std::uniform_int_distribution to generate values in [0, 255]
76
+ * EncodeEntropyMt19937Fast will encode using std::mt19937
77
+ * with only 3 generated values.
164
78
  * */
165
- inline void EncodeEntropyMt19937(std::mt19937& generator, ULID& ulid) {
166
- ulid = (ulid >> 80) << 80;
167
-
168
- ULID e = Distribution_0_255(generator);
169
-
170
- e <<= 8;
171
- e |= Distribution_0_255(generator);
172
-
173
- e <<= 8;
174
- e |= Distribution_0_255(generator);
175
-
176
- e <<= 8;
177
- e |= Distribution_0_255(generator);
178
-
179
- e <<= 8;
180
- e |= Distribution_0_255(generator);
181
-
182
- e <<= 8;
183
- e |= Distribution_0_255(generator);
184
-
185
- e <<= 8;
186
- e |= Distribution_0_255(generator);
187
-
188
- e <<= 8;
189
- e |= Distribution_0_255(generator);
190
-
191
- e <<= 8;
192
- e |= Distribution_0_255(generator);
193
-
194
- e <<= 8;
195
- e |= Distribution_0_255(generator);
196
-
197
- ulid |= e;
198
- }
199
-
200
- /**
201
- * Encode will create an encoded ULID with a timestamp and a generator.
202
- * */
203
- inline void Encode(std::chrono::time_point<std::chrono::system_clock> timestamp, const std::function<uint8_t()>& rng, ULID& ulid) {
204
- EncodeTime(timestamp, ulid);
205
- EncodeEntropy(rng, ulid);
206
- }
207
-
208
- /**
209
- * EncodeNowRand = EncodeTimeNow + EncodeEntropyRand.
210
- * */
211
- inline void EncodeNowRand(ULID& ulid) {
212
- EncodeTimeNow(ulid);
213
- EncodeEntropyRand(ulid);
214
- }
215
-
216
- /**
217
- * Create will create a ULID with a timestamp and a generator.
218
- * */
219
- inline ULID Create(std::chrono::time_point<std::chrono::system_clock> timestamp, const std::function<uint8_t()>& rng) {
220
- ULID ulid = 0;
221
- Encode(timestamp, rng, ulid);
222
- return ulid;
223
- }
224
-
225
- /**
226
- * CreateNowRand:EncodeNowRand = Create:Encode.
227
- * */
228
- inline ULID CreateNowRand() {
229
- ULID ulid = 0;
230
- EncodeNowRand(ulid);
231
- return ulid;
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);
232
93
  }
233
94
 
234
95
  /**
@@ -257,80 +118,63 @@ static const char Encoding[33] = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
257
118
  * entropy:
258
119
  * follows similarly, except now all components are set to 5 bits.
259
120
  * */
260
- inline void MarshalTo(const ULID& ulid, char dst[26]) {
261
- // 10 byte timestamp
262
- dst[0] = Encoding[(static_cast<uint8_t>(ulid >> 120) & 224) >> 5];
263
- dst[1] = Encoding[static_cast<uint8_t>(ulid >> 120) & 31];
264
- dst[2] = Encoding[(static_cast<uint8_t>(ulid >> 112) & 248) >> 3];
265
- dst[3] = Encoding[((static_cast<uint8_t>(ulid >> 112) & 7) << 2) | ((static_cast<uint8_t>(ulid >> 104) & 192) >> 6)];
266
- dst[4] = Encoding[(static_cast<uint8_t>(ulid >> 104) & 62) >> 1];
267
- dst[5] = Encoding[((static_cast<uint8_t>(ulid >> 104) & 1) << 4) | ((static_cast<uint8_t>(ulid >> 96) & 240) >> 4)];
268
- dst[6] = Encoding[((static_cast<uint8_t>(ulid >> 96) & 15) << 1) | ((static_cast<uint8_t>(ulid >> 88) & 128) >> 7)];
269
- dst[7] = Encoding[(static_cast<uint8_t>(ulid >> 88) & 124) >> 2];
270
- dst[8] = Encoding[((static_cast<uint8_t>(ulid >> 88) & 3) << 3) | ((static_cast<uint8_t>(ulid >> 80) & 224) >> 5)];
271
- dst[9] = Encoding[static_cast<uint8_t>(ulid >> 80) & 31];
272
-
273
- // 16 bytes of entropy
274
- dst[10] = Encoding[(static_cast<uint8_t>(ulid >> 72) & 248) >> 3];
275
- dst[11] = Encoding[((static_cast<uint8_t>(ulid >> 72) & 7) << 2) | ((static_cast<uint8_t>(ulid >> 64) & 192) >> 6)];
276
- dst[12] = Encoding[(static_cast<uint8_t>(ulid >> 64) & 62) >> 1];
277
- dst[13] = Encoding[((static_cast<uint8_t>(ulid >> 64) & 1) << 4) | ((static_cast<uint8_t>(ulid >> 56) & 240) >> 4)];
278
- dst[14] = Encoding[((static_cast<uint8_t>(ulid >> 56) & 15) << 1) | ((static_cast<uint8_t>(ulid >> 48) & 128) >> 7)];
279
- dst[15] = Encoding[(static_cast<uint8_t>(ulid >> 48) & 124) >> 2];
280
- dst[16] = Encoding[((static_cast<uint8_t>(ulid >> 48) & 3) << 3) | ((static_cast<uint8_t>(ulid >> 40) & 224) >> 5)];
281
- dst[17] = Encoding[static_cast<uint8_t>(ulid >> 40) & 31];
282
- dst[18] = Encoding[(static_cast<uint8_t>(ulid >> 32) & 248) >> 3];
283
- dst[19] = Encoding[((static_cast<uint8_t>(ulid >> 32) & 7) << 2) | ((static_cast<uint8_t>(ulid >> 24) & 192) >> 6)];
284
- dst[20] = Encoding[(static_cast<uint8_t>(ulid >> 24) & 62) >> 1];
285
- dst[21] = Encoding[((static_cast<uint8_t>(ulid >> 24) & 1) << 4) | ((static_cast<uint8_t>(ulid >> 16) & 240) >> 4)];
286
- dst[22] = Encoding[((static_cast<uint8_t>(ulid >> 16) & 15) << 1) | ((static_cast<uint8_t>(ulid >> 8) & 128) >> 7)];
287
- dst[23] = Encoding[(static_cast<uint8_t>(ulid >> 8) & 124) >> 2];
288
- dst[24] = Encoding[((static_cast<uint8_t>(ulid >> 8) & 3) << 3) | (((static_cast<uint8_t>(ulid)) & 224) >> 5)];
289
- dst[25] = Encoding[(static_cast<uint8_t>(ulid)) & 31];
290
- }
291
-
292
- /**
293
- * Marshal will marshal a ULID to a std::string.
294
- * */
295
- inline std::string Marshal(const ULID& ulid) {
296
- char data[27];
297
- data[26] = '\0';
298
- MarshalTo(ulid, data);
299
- return std::string(data);
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];
300
152
  }
301
153
 
302
154
  /**
303
155
  * MarshalBinaryTo will Marshal a ULID to the passed byte array
304
156
  * */
305
- inline void MarshalBinaryTo(const ULID& ulid, uint8_t dst[16]) {
306
- // timestamp
307
- dst[0] = static_cast<uint8_t>(ulid >> 120);
308
- dst[1] = static_cast<uint8_t>(ulid >> 112);
309
- dst[2] = static_cast<uint8_t>(ulid >> 104);
310
- dst[3] = static_cast<uint8_t>(ulid >> 96);
311
- dst[4] = static_cast<uint8_t>(ulid >> 88);
312
- dst[5] = static_cast<uint8_t>(ulid >> 80);
313
-
314
- // entropy
315
- dst[6] = static_cast<uint8_t>(ulid >> 72);
316
- dst[7] = static_cast<uint8_t>(ulid >> 64);
317
- dst[8] = static_cast<uint8_t>(ulid >> 56);
318
- dst[9] = static_cast<uint8_t>(ulid >> 48);
319
- dst[10] = static_cast<uint8_t>(ulid >> 40);
320
- dst[11] = static_cast<uint8_t>(ulid >> 32);
321
- dst[12] = static_cast<uint8_t>(ulid >> 24);
322
- dst[13] = static_cast<uint8_t>(ulid >> 16);
323
- dst[14] = static_cast<uint8_t>(ulid >> 8);
324
- dst[15] = static_cast<uint8_t>(ulid);
325
- }
326
-
327
- /**
328
- * MarshalBinary will Marshal a ULID to a byte vector.
329
- * */
330
- inline std::vector<uint8_t> MarshalBinary(const ULID& ulid) {
331
- std::vector<uint8_t> dst(16);
332
- MarshalBinaryTo(ulid, dst.data());
333
- return dst;
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);
334
178
  }
335
179
 
336
180
  /**
@@ -340,216 +184,163 @@ inline std::vector<uint8_t> MarshalBinary(const ULID& ulid) {
340
184
  * 65-90 are capital alphabets.
341
185
  * */
342
186
  static const uint8_t dec[256] = {
343
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
344
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
345
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
346
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
347
-
348
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
349
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
350
- /* 0 1 2 3 4 5 6 7 */
351
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
352
- /* 8 9 */
353
- 0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
354
-
355
- /* 10(A) 11(B) 12(C) 13(D) 14(E) 15(F) 16(G) */
356
- 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
357
- /*17(H) 18(J) 19(K) 20(M) 21(N) */
358
- 0x11, 0xFF, 0x12, 0x13, 0xFF, 0x14, 0x15, 0xFF,
359
- /*22(P)23(Q)24(R) 25(S) 26(T) 27(V) 28(W) */
360
- 0x16, 0x17, 0x18, 0x19, 0x1A, 0xFF, 0x1B, 0x1C,
361
- /*29(X)30(Y)31(Z) */
362
- 0x1D, 0x1E, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
363
-
364
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
365
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
366
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
367
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
368
-
369
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
370
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
371
- 0xFF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 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
+ 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
388
232
  };
389
233
 
390
234
  /**
391
235
  * UnmarshalFrom will unmarshal a ULID from the passed character array.
392
236
  * */
393
- inline void UnmarshalFrom(const char str[26], ULID& ulid) {
394
- // timestamp
395
- ulid = (dec[int(str[0])] << 5) | dec[int(str[1])];
237
+ inline void UnmarshalFrom(const char str[26], ULID& ulid)
238
+ {
239
+ // timestamp
240
+ ulid = (dec[int(str[0])] << 5) | dec[int(str[1])];
396
241
 
397
- ulid <<= 8;
398
- ulid |= (dec[int(str[2])] << 3) | (dec[int(str[3])] >> 2);
242
+ ulid <<= 8;
243
+ ulid |= (dec[int(str[2])] << 3) | (dec[int(str[3])] >> 2);
399
244
 
400
- ulid <<= 8;
401
- ulid |= (dec[int(str[3])] << 6) | (dec[int(str[4])] << 1) | (dec[int(str[5])] >> 4);
245
+ ulid <<= 8;
246
+ ulid |= (dec[int(str[3])] << 6) | (dec[int(str[4])] << 1) | (dec[int(str[5])] >> 4);
402
247
 
403
- ulid <<= 8;
404
- ulid |= (dec[int(str[5])] << 4) | (dec[int(str[6])] >> 1);
248
+ ulid <<= 8;
249
+ ulid |= (dec[int(str[5])] << 4) | (dec[int(str[6])] >> 1);
405
250
 
406
- ulid <<= 8;
407
- ulid |= (dec[int(str[6])] << 7) | (dec[int(str[7])] << 2) | (dec[int(str[8])] >> 3);
251
+ ulid <<= 8;
252
+ ulid |= (dec[int(str[6])] << 7) | (dec[int(str[7])] << 2) | (dec[int(str[8])] >> 3);
408
253
 
409
- ulid <<= 8;
410
- ulid |= (dec[int(str[8])] << 5) | dec[int(str[9])];
254
+ ulid <<= 8;
255
+ ulid |= (dec[int(str[8])] << 5) | dec[int(str[9])];
411
256
 
412
- // entropy
413
- ulid <<= 8;
414
- ulid |= (dec[int(str[10])] << 3) | (dec[int(str[11])] >> 2);
257
+ // entropy
258
+ ulid <<= 8;
259
+ ulid |= (dec[int(str[10])] << 3) | (dec[int(str[11])] >> 2);
415
260
 
416
- ulid <<= 8;
417
- ulid |= (dec[int(str[11])] << 6) | (dec[int(str[12])] << 1) | (dec[int(str[13])] >> 4);
261
+ ulid <<= 8;
262
+ ulid |= (dec[int(str[11])] << 6) | (dec[int(str[12])] << 1) | (dec[int(str[13])] >> 4);
418
263
 
419
- ulid <<= 8;
420
- ulid |= (dec[int(str[13])] << 4) | (dec[int(str[14])] >> 1);
264
+ ulid <<= 8;
265
+ ulid |= (dec[int(str[13])] << 4) | (dec[int(str[14])] >> 1);
421
266
 
422
- ulid <<= 8;
423
- ulid |= (dec[int(str[14])] << 7) | (dec[int(str[15])] << 2) | (dec[int(str[16])] >> 3);
267
+ ulid <<= 8;
268
+ ulid |= (dec[int(str[14])] << 7) | (dec[int(str[15])] << 2) | (dec[int(str[16])] >> 3);
424
269
 
425
- ulid <<= 8;
426
- ulid |= (dec[int(str[16])] << 5) | dec[int(str[17])];
270
+ ulid <<= 8;
271
+ ulid |= (dec[int(str[16])] << 5) | dec[int(str[17])];
427
272
 
428
- ulid <<= 8;
429
- ulid |= (dec[int(str[18])] << 3) | (dec[int(str[19])] >> 2);
273
+ ulid <<= 8;
274
+ ulid |= (dec[int(str[18])] << 3) | (dec[int(str[19])] >> 2);
430
275
 
431
- ulid <<= 8;
432
- ulid |= (dec[int(str[19])] << 6) | (dec[int(str[20])] << 1) | (dec[int(str[21])] >> 4);
276
+ ulid <<= 8;
277
+ ulid |= (dec[int(str[19])] << 6) | (dec[int(str[20])] << 1) | (dec[int(str[21])] >> 4);
433
278
 
434
- ulid <<= 8;
435
- ulid |= (dec[int(str[21])] << 4) | (dec[int(str[22])] >> 1);
279
+ ulid <<= 8;
280
+ ulid |= (dec[int(str[21])] << 4) | (dec[int(str[22])] >> 1);
436
281
 
437
- ulid <<= 8;
438
- ulid |= (dec[int(str[22])] << 7) | (dec[int(str[23])] << 2) | (dec[int(str[24])] >> 3);
282
+ ulid <<= 8;
283
+ ulid |= (dec[int(str[22])] << 7) | (dec[int(str[23])] << 2) | (dec[int(str[24])] >> 3);
439
284
 
440
- ulid <<= 8;
441
- ulid |= (dec[int(str[24])] << 5) | dec[int(str[25])];
442
- }
443
-
444
- /**
445
- * Unmarshal will create a new ULID by unmarshaling the passed string.
446
- * */
447
- inline ULID Unmarshal(const std::string& str) {
448
- ULID ulid;
449
- UnmarshalFrom(str.c_str(), ulid);
450
- return ulid;
285
+ ulid <<= 8;
286
+ ulid |= (dec[int(str[24])] << 5) | dec[int(str[25])];
451
287
  }
452
288
 
453
289
  /**
454
290
  * UnmarshalBinaryFrom will unmarshal a ULID from the passed byte array.
455
291
  * */
456
- inline void UnmarshalBinaryFrom(const uint8_t b[16], ULID& ulid) {
457
- // timestamp
458
- ulid = b[0];
459
-
460
- ulid <<= 8;
461
- ulid |= b[1];
462
-
463
- ulid <<= 8;
464
- ulid |= b[2];
292
+ inline void UnmarshalBinaryFrom(const uint8_t b[16], ULID& ulid)
293
+ {
294
+ // timestamp
295
+ ulid = b[0];
465
296
 
466
- ulid <<= 8;
467
- ulid |= b[3];
297
+ ulid <<= 8;
298
+ ulid |= b[1];
468
299
 
469
- ulid <<= 8;
470
- ulid |= b[4];
300
+ ulid <<= 8;
301
+ ulid |= b[2];
471
302
 
472
- ulid <<= 8;
473
- ulid |= b[5];
303
+ ulid <<= 8;
304
+ ulid |= b[3];
474
305
 
475
- // entropy
476
- ulid <<= 8;
477
- ulid |= b[6];
306
+ ulid <<= 8;
307
+ ulid |= b[4];
478
308
 
479
- ulid <<= 8;
480
- ulid |= b[7];
309
+ ulid <<= 8;
310
+ ulid |= b[5];
481
311
 
482
- ulid <<= 8;
483
- ulid |= b[8];
312
+ // entropy
313
+ ulid <<= 8;
314
+ ulid |= b[6];
484
315
 
485
- ulid <<= 8;
486
- ulid |= b[9];
316
+ ulid <<= 8;
317
+ ulid |= b[7];
487
318
 
488
- ulid <<= 8;
489
- ulid |= b[10];
490
-
491
- ulid <<= 8;
492
- ulid |= b[11];
493
-
494
- ulid <<= 8;
495
- ulid |= b[12];
496
-
497
- ulid <<= 8;
498
- ulid |= b[13];
499
-
500
- ulid <<= 8;
501
- ulid |= b[14];
502
-
503
- ulid <<= 8;
504
- ulid |= b[15];
505
- }
506
-
507
- /**
508
- * Unmarshal will create a new ULID by unmarshaling the passed byte vector.
509
- * */
510
- inline ULID UnmarshalBinary(const std::vector<uint8_t>& b) {
511
- ULID ulid;
512
- UnmarshalBinaryFrom(b.data(), ulid);
513
- return ulid;
514
- }
515
-
516
- /**
517
- * CompareULIDs will compare two ULIDs.
518
- * returns:
519
- * -1 if ulid1 is Lexicographically before ulid2
520
- * 1 if ulid1 is Lexicographically after ulid2
521
- * 0 if ulid1 is same as ulid2
522
- * */
523
- inline int CompareULIDs(const ULID& ulid1, const ULID& ulid2) {
524
- return -2 * (ulid1 < ulid2) - 1 * (ulid1 == ulid2) + 1;
525
- }
526
-
527
- /**
528
- * Time will extract the timestamp used to generate a ULID
529
- * */
530
- inline std::chrono::time_point<std::chrono::system_clock> Time(const ULID& ulid) {
531
- int64_t ans = 0;
319
+ ulid <<= 8;
320
+ ulid |= b[8];
532
321
 
533
- ans |= static_cast<uint8_t>(ulid >> 120);
322
+ ulid <<= 8;
323
+ ulid |= b[9];
534
324
 
535
- ans <<= 8;
536
- ans |= static_cast<uint8_t>(ulid >> 112);
325
+ ulid <<= 8;
326
+ ulid |= b[10];
537
327
 
538
- ans <<= 8;
539
- ans |= static_cast<uint8_t>(ulid >> 104);
328
+ ulid <<= 8;
329
+ ulid |= b[11];
540
330
 
541
- ans <<= 8;
542
- ans |= static_cast<uint8_t>(ulid >> 96);
331
+ ulid <<= 8;
332
+ ulid |= b[12];
543
333
 
544
- ans <<= 8;
545
- ans |= static_cast<uint8_t>(ulid >> 88);
334
+ ulid <<= 8;
335
+ ulid |= b[13];
546
336
 
547
- ans <<= 8;
548
- ans |= static_cast<uint8_t>(ulid >> 80);
337
+ ulid <<= 8;
338
+ ulid |= b[14];
549
339
 
550
- return std::chrono::time_point<std::chrono::system_clock>(std::chrono::milliseconds{ans});
340
+ ulid <<= 8;
341
+ ulid |= b[15];
551
342
  }
552
343
 
553
- }; // namespace ulid
344
+ }; // namespace ulid
554
345
 
555
346
  #endif // ULID_UINT128_HH