ulid-transform 1.4.0__tar.gz → 1.4.1__tar.gz

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,8 +1,9 @@
1
- Metadata-Version: 2.3
1
+ Metadata-Version: 2.4
2
2
  Name: ulid-transform
3
- Version: 1.4.0
3
+ Version: 1.4.1
4
4
  Summary: Create and transform ULIDs
5
5
  License: MIT
6
+ License-File: LICENSE
6
7
  Author: J. Nick Koston
7
8
  Author-email: nick@koston.org
8
9
  Requires-Python: >=3.11,<4.0
@@ -15,6 +16,7 @@ Classifier: Programming Language :: Python :: 3
15
16
  Classifier: Programming Language :: Python :: 3.11
16
17
  Classifier: Programming Language :: Python :: 3.12
17
18
  Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Programming Language :: Python :: 3.14
18
20
  Classifier: Topic :: Software Development :: Libraries
19
21
  Project-URL: Bug Tracker, https://github.com/bluetooth-devices/ulid-transform/issues
20
22
  Project-URL: Changelog, https://github.com/bluetooth-devices/ulid-transform/blob/main/CHANGELOG.md
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "ulid-transform"
3
- version = "1.4.0"
3
+ version = "1.4.1"
4
4
  description = "Create and transform ULIDs"
5
5
  authors = ["J. Nick Koston <nick@koston.org>"]
6
6
  license = "MIT"
@@ -33,10 +33,10 @@ python = "^3.11"
33
33
 
34
34
  [tool.poetry.group.dev.dependencies]
35
35
  pytest = ">=7,<9"
36
- pytest-cov = ">=3,<7"
36
+ pytest-cov = ">=3,<8"
37
37
  Cython = ">=3.0.5"
38
- setuptools = ">=65.4.1,<76.0.0"
39
- pytest-codspeed = "^3.1.2"
38
+ setuptools = ">=65.4.1,<80.0.0"
39
+ pytest-codspeed = ">=3.1.2,<5.0.0"
40
40
 
41
41
 
42
42
  [tool.poetry.group.benchmark.dependencies]
@@ -66,10 +66,6 @@ exclude_lines = [
66
66
  'if __name__ == "__main__":',
67
67
  ]
68
68
 
69
- [tool.isort]
70
- profile = "black"
71
- known_first_party = ["ulid_transform", "tests"]
72
-
73
69
  [tool.mypy]
74
70
  check_untyped_defs = true
75
71
  disallow_any_generics = true
@@ -92,6 +88,21 @@ allow_untyped_defs = true
92
88
  module = "bench.*"
93
89
  allow_untyped_defs = true
94
90
 
91
+ [tool.ruff.lint]
92
+ extend-select = [
93
+ "B",
94
+ "I",
95
+ "S",
96
+ "UP",
97
+ ]
98
+
99
+ [tool.ruff.lint.isort]
100
+ known-first-party = ["ulid_transform", "tests"]
101
+
102
+ [tool.ruff.lint.per-file-ignores]
103
+ "bench/*" = ["S101"]
104
+ "tests/*" = ["S101"]
105
+
95
106
  [build-system]
96
107
  requires = ['setuptools>=65.4.1', 'wheel', 'Cython>=3.0.2', "poetry-core>=1.0.0"]
97
108
  build-backend = "poetry.core.masonry.api"
@@ -12,7 +12,7 @@ package_data = \
12
12
 
13
13
  setup_kwargs = {
14
14
  'name': 'ulid-transform',
15
- 'version': '1.4.0',
15
+ 'version': '1.4.1',
16
16
  'description': 'Create and transform ULIDs',
17
17
  'long_description': '# Fast ULID transformations\n\n<p align="center">\n <a href="https://github.com/bluetooth-devices/ulid-transform/actions/workflows/ci.yml?query=branch%3Amain">\n <img src="https://img.shields.io/github/actions/workflow/status/bluetooth-devices/ulid-transform/ci.yml?branch=main&label=CI&logo=github&style=flat-square" alt="CI Status" >\n </a>\n <a href="https://codecov.io/gh/bluetooth-devices/ulid-transform">\n <img src="https://img.shields.io/codecov/c/github/bluetooth-devices/ulid-transform.svg?logo=codecov&logoColor=fff&style=flat-square" alt="Test coverage percentage">\n </a>\n</p>\n<p align="center">\n <a href="https://python-poetry.org/">\n <img src="https://img.shields.io/badge/packaging-poetry-299bd7?style=flat-square&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAASCAYAAABrXO8xAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAJJSURBVHgBfZLPa1NBEMe/s7tNXoxW1KJQKaUHkXhQvHgW6UHQQ09CBS/6V3hKc/AP8CqCrUcpmop3Cx48eDB4yEECjVQrlZb80CRN8t6OM/teagVxYZi38+Yz853dJbzoMV3MM8cJUcLMSUKIE8AzQ2PieZzFxEJOHMOgMQQ+dUgSAckNXhapU/NMhDSWLs1B24A8sO1xrN4NECkcAC9ASkiIJc6k5TRiUDPhnyMMdhKc+Zx19l6SgyeW76BEONY9exVQMzKExGKwwPsCzza7KGSSWRWEQhyEaDXp6ZHEr416ygbiKYOd7TEWvvcQIeusHYMJGhTwF9y7sGnSwaWyFAiyoxzqW0PM/RjghPxF2pWReAowTEXnDh0xgcLs8l2YQmOrj3N7ByiqEoH0cARs4u78WgAVkoEDIDoOi3AkcLOHU60RIg5wC4ZuTC7FaHKQm8Hq1fQuSOBvX/sodmNJSB5geaF5CPIkUeecdMxieoRO5jz9bheL6/tXjrwCyX/UYBUcjCaWHljx1xiX6z9xEjkYAzbGVnB8pvLmyXm9ep+W8CmsSHQQY77Zx1zboxAV0w7ybMhQmfqdmmw3nEp1I0Z+FGO6M8LZdoyZnuzzBdjISicKRnpxzI9fPb+0oYXsNdyi+d3h9bm9MWYHFtPeIZfLwzmFDKy1ai3p+PDls1Llz4yyFpferxjnyjJDSEy9CaCx5m2cJPerq6Xm34eTrZt3PqxYO1XOwDYZrFlH1fWnpU38Y9HRze3lj0vOujZcXKuuXm3jP+s3KbZVra7y2EAAAAAASUVORK5CYII=" alt="Poetry">\n </a>\n <a href="https://github.com/ambv/black">\n <img src="https://img.shields.io/badge/code%20style-black-000000.svg?style=flat-square" alt="black">\n </a>\n <a href="https://github.com/pre-commit/pre-commit">\n <img src="https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=flat-square" alt="pre-commit">\n </a>\n <a href="https://codspeed.io/bluetooth-devices/ulid-transform"><img src="https://img.shields.io/endpoint?url=https://codspeed.io/badge.json" alt="CodSpeed Badge"/></a>\n</p>\n<p align="center">\n <a href="https://pypi.org/project/ulid-transform/">\n <img src="https://img.shields.io/pypi/v/ulid-transform.svg?logo=python&logoColor=fff&style=flat-square" alt="PyPI Version">\n </a>\n <img src="https://img.shields.io/pypi/pyversions/ulid-transform.svg?style=flat-square&logo=python&amp;logoColor=fff" alt="Supported Python versions">\n <img src="https://img.shields.io/pypi/l/ulid-transform.svg?style=flat-square" alt="License">\n</p>\n\nCreate and transform ULIDs\n\nThis library will use the CPP implementation from https://github.com/suyash/ulid if cython is available, and will fallback to pure python if it is not.\n\n## Example\n\n```python\n>>> import ulid_transform\n>>> ulid_transform.ulid_hex()\n\'01869a2ea5fb0b43aa056293e47c0a35\'\n>>> ulid_transform.ulid_now()\n\'0001HZX0NW00GW0X476W5TVBFE\'\n>>> ulid_transform.ulid_at_time(1234)\n\'000000016JC62D620DGYNG2R8H\'\n>>> ulid_transform.ulid_to_bytes(\'0001HZX0NW00GW0X476W5TVBFE\')\nb\'\\x00\\x00c\\xfe\\x82\\xbc\\x00!\\xc0t\\x877\\x0b\\xad\\xad\\xee\'\n>> ulid_transform.bytes_to_ulid(b"\\x01\\x86\\x99?\\xe8\\xf3\\x11\\xbc\\xed\\xef\\x86U.9\\x03z")\n\'01GTCKZT7K26YEVVW6AMQ3J0VT\'\n>>> ulid_transform.ulid_to_bytes_or_none(\'0001HZX0NW00GW0X476W5TVBFE\')\nb\'\\x00\\x00c\\xfe\\x82\\xbc\\x00!\\xc0t\\x877\\x0b\\xad\\xad\\xee\'\n>>> ulid_transform.ulid_to_bytes_or_none(None)\n>>> ulid_transform.bytes_to_ulid_or_none(b\'\\x00\\x00c\\xfe\\x82\\xbc\\x00!\\xc0t\\x877\\x0b\\xad\\xad\\xee\')\n\'0001HZX0NW00GW0X476W5TVBFE\'\n>>> ulid_transform.bytes_to_ulid_or_none(None)\n```\n\n## Installation\n\nInstall this via pip (or your favourite package manager):\n\n`pip install ulid-transform`\n\n## Contributors ✨\n\nThanks to https://github.com/suyash/ulid which provides the cython implementation guts.\n\nThanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):\n\n<!-- prettier-ignore-start -->\n<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->\n<!-- markdownlint-disable -->\n<!-- markdownlint-enable -->\n<!-- ALL-CONTRIBUTORS-LIST:END -->\n<!-- prettier-ignore-end -->\n\nThis project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!\n\n## Credits\n\nThis package was created with\n[Copier](https://copier.readthedocs.io/) and the\n[browniebroke/pypackage-template](https://github.com/browniebroke/pypackage-template)\nproject template.\n',
18
18
  'author': 'J. Nick Koston',
@@ -1,4 +1,4 @@
1
- __version__ = "1.4.0"
1
+ __version__ = "1.4.1"
2
2
 
3
3
  try:
4
4
  from ._ulid_impl import (
@@ -0,0 +1,282 @@
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 <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
+ struct ULID {
24
+ uint8_t data[16];
25
+ };
26
+
27
+ /**
28
+ * EncodeTimestamp will encode the int64_t timestamp to the passed ulid
29
+ * */
30
+ inline void EncodeTimestamp(int64_t timestamp, ULID& ulid)
31
+ {
32
+ ulid.data[0] = static_cast<uint8_t>(timestamp >> 40);
33
+ ulid.data[1] = static_cast<uint8_t>(timestamp >> 32);
34
+ ulid.data[2] = static_cast<uint8_t>(timestamp >> 24);
35
+ ulid.data[3] = static_cast<uint8_t>(timestamp >> 16);
36
+ ulid.data[4] = static_cast<uint8_t>(timestamp >> 8);
37
+ ulid.data[5] = static_cast<uint8_t>(timestamp);
38
+ }
39
+
40
+ /**
41
+ * EncodeTime will encode the time point to the passed ulid
42
+ * */
43
+ inline void EncodeTime(std::chrono::time_point<std::chrono::system_clock> time_point, ULID& ulid)
44
+ {
45
+ auto time_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(time_point);
46
+ int64_t timestamp = time_ms.time_since_epoch().count();
47
+ EncodeTimestamp(timestamp, ulid);
48
+ }
49
+
50
+ /**
51
+ * EncodeTimeSystemClockNow will encode a ULID using the time obtained using
52
+ * std::chrono::system_clock::now() by taking the timestamp in milliseconds.
53
+ * */
54
+ inline void EncodeTimeSystemClockNow(ULID& ulid)
55
+ {
56
+ EncodeTime(std::chrono::system_clock::now(), ulid);
57
+ }
58
+
59
+ /**
60
+ * EncodeEntropyMt19937Fast will encode using std::mt19937
61
+ * with only 3 generated values.
62
+ * */
63
+ inline void EncodeEntropyMt19937Fast(ULID& ulid)
64
+ {
65
+ static thread_local std::mt19937 gen([]() {
66
+ // Use multiple entropy sources for seeding
67
+ std::array<uint32_t, 3> seed_data = {
68
+ static_cast<uint32_t>(std::chrono::high_resolution_clock::now().time_since_epoch().count()),
69
+ static_cast<uint32_t>(std::random_device {}()),
70
+ static_cast<uint32_t>(reinterpret_cast<uintptr_t>(&gen) & 0xFFFFFFFF)
71
+ };
72
+ std::seed_seq seed_seq(seed_data.begin(), seed_data.end());
73
+ return std::mt19937(seed_seq);
74
+ }());
75
+ uint64_t high = (static_cast<uint64_t>(gen()) << 32) | gen();
76
+ uint32_t low = gen();
77
+ ulid.data[6] = (high >> 40) & 0xFF;
78
+ ulid.data[7] = (high >> 32) & 0xFF;
79
+ ulid.data[8] = (high >> 24) & 0xFF;
80
+ ulid.data[9] = (high >> 16) & 0xFF;
81
+ ulid.data[10] = (high >> 8) & 0xFF;
82
+ ulid.data[11] = high & 0xFF;
83
+ ulid.data[12] = (low >> 24) & 0xFF;
84
+ ulid.data[13] = (low >> 16) & 0xFF;
85
+ ulid.data[14] = (low >> 8) & 0xFF;
86
+ ulid.data[15] = low & 0xFF;
87
+ }
88
+
89
+ /**
90
+ * Crockford's Base32
91
+ * */
92
+ static const char Encoding[33] = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
93
+
94
+ /**
95
+ * MarshalTo will marshal a ULID to the passed character array.
96
+ *
97
+ * Implementation taken directly from oklog/ulid
98
+ * (https://sourcegraph.com/github.com/oklog/ulid@0774f81f6e44af5ce5e91c8d7d76cf710e889ebb/-/blob/ulid.go#L162-190)
99
+ *
100
+ * timestamp:<br>
101
+ * dst[0]: first 3 bits of data[0]<br>
102
+ * dst[1]: last 5 bits of data[0]<br>
103
+ * dst[2]: first 5 bits of data[1]<br>
104
+ * dst[3]: last 3 bits of data[1] + first 2 bits of data[2]<br>
105
+ * dst[4]: bits 3-7 of data[2]<br>
106
+ * dst[5]: last bit of data[2] + first 4 bits of data[3]<br>
107
+ * dst[6]: last 4 bits of data[3] + first bit of data[4]<br>
108
+ * dst[7]: bits 2-6 of data[4]<br>
109
+ * dst[8]: last 2 bits of data[4] + first 3 bits of data[5]<br>
110
+ * dst[9]: last 5 bits of data[5]<br>
111
+ *
112
+ * entropy:
113
+ * follows similarly, except now all components are set to 5 bits.
114
+ * */
115
+ inline void MarshalTo(const ULID& ulid, char dst[26])
116
+ {
117
+ // 10 byte timestamp
118
+ dst[0] = Encoding[(ulid.data[0] & 224) >> 5];
119
+ dst[1] = Encoding[ulid.data[0] & 31];
120
+ dst[2] = Encoding[(ulid.data[1] & 248) >> 3];
121
+ dst[3] = Encoding[((ulid.data[1] & 7) << 2) | ((ulid.data[2] & 192) >> 6)];
122
+ dst[4] = Encoding[(ulid.data[2] & 62) >> 1];
123
+ dst[5] = Encoding[((ulid.data[2] & 1) << 4) | ((ulid.data[3] & 240) >> 4)];
124
+ dst[6] = Encoding[((ulid.data[3] & 15) << 1) | ((ulid.data[4] & 128) >> 7)];
125
+ dst[7] = Encoding[(ulid.data[4] & 124) >> 2];
126
+ dst[8] = Encoding[((ulid.data[4] & 3) << 3) | ((ulid.data[5] & 224) >> 5)];
127
+ dst[9] = Encoding[ulid.data[5] & 31];
128
+
129
+ // 16 bytes of entropy
130
+ dst[10] = Encoding[(ulid.data[6] & 248) >> 3];
131
+ dst[11] = Encoding[((ulid.data[6] & 7) << 2) | ((ulid.data[7] & 192) >> 6)];
132
+ dst[12] = Encoding[(ulid.data[7] & 62) >> 1];
133
+ dst[13] = Encoding[((ulid.data[7] & 1) << 4) | ((ulid.data[8] & 240) >> 4)];
134
+ dst[14] = Encoding[((ulid.data[8] & 15) << 1) | ((ulid.data[9] & 128) >> 7)];
135
+ dst[15] = Encoding[(ulid.data[9] & 124) >> 2];
136
+ dst[16] = Encoding[((ulid.data[9] & 3) << 3) | ((ulid.data[10] & 224) >> 5)];
137
+ dst[17] = Encoding[ulid.data[10] & 31];
138
+ dst[18] = Encoding[(ulid.data[11] & 248) >> 3];
139
+ dst[19] = Encoding[((ulid.data[11] & 7) << 2) | ((ulid.data[12] & 192) >> 6)];
140
+ dst[20] = Encoding[(ulid.data[12] & 62) >> 1];
141
+ dst[21] = Encoding[((ulid.data[12] & 1) << 4) | ((ulid.data[13] & 240) >> 4)];
142
+ dst[22] = Encoding[((ulid.data[13] & 15) << 1) | ((ulid.data[14] & 128) >> 7)];
143
+ dst[23] = Encoding[(ulid.data[14] & 124) >> 2];
144
+ dst[24] = Encoding[((ulid.data[14] & 3) << 3) | ((ulid.data[15] & 224) >> 5)];
145
+ dst[25] = Encoding[ulid.data[15] & 31];
146
+ }
147
+
148
+ /**
149
+ * MarshalBinaryTo will Marshal a ULID to the passed byte array
150
+ * */
151
+ inline void MarshalBinaryTo(const ULID& ulid, uint8_t dst[16])
152
+ {
153
+ // timestamp
154
+ dst[0] = ulid.data[0];
155
+ dst[1] = ulid.data[1];
156
+ dst[2] = ulid.data[2];
157
+ dst[3] = ulid.data[3];
158
+ dst[4] = ulid.data[4];
159
+ dst[5] = ulid.data[5];
160
+
161
+ // entropy
162
+ dst[6] = ulid.data[6];
163
+ dst[7] = ulid.data[7];
164
+ dst[8] = ulid.data[8];
165
+ dst[9] = ulid.data[9];
166
+ dst[10] = ulid.data[10];
167
+ dst[11] = ulid.data[11];
168
+ dst[12] = ulid.data[12];
169
+ dst[13] = ulid.data[13];
170
+ dst[14] = ulid.data[14];
171
+ dst[15] = ulid.data[15];
172
+ }
173
+
174
+ /**
175
+ * dec storesdecimal encodings for characters.
176
+ * 0xFF indicates invalid character.
177
+ * 48-57 are digits.
178
+ * 65-90 are capital alphabets.
179
+ * */
180
+ static const uint8_t dec[256] = {
181
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
182
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
183
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
184
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
185
+
186
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
187
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
188
+ /* 0 1 2 3 4 5 6 7 */
189
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
190
+ /* 8 9 */
191
+ 0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
192
+
193
+ /* 10(A) 11(B) 12(C) 13(D) 14(E) 15(F) 16(G) */
194
+ 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
195
+ /*17(H) 18(J) 19(K) 20(M) 21(N) */
196
+ 0x11, 0xFF, 0x12, 0x13, 0xFF, 0x14, 0x15, 0xFF,
197
+ /*22(P)23(Q)24(R) 25(S) 26(T) 27(V) 28(W) */
198
+ 0x16, 0x17, 0x18, 0x19, 0x1A, 0xFF, 0x1B, 0x1C,
199
+ /*29(X)30(Y)31(Z) */
200
+ 0x1D, 0x1E, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
201
+
202
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
203
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
204
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
205
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
206
+
207
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
208
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
209
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
210
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
211
+
212
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
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
+
217
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
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
+
222
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
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
+ };
227
+
228
+ /**
229
+ * UnmarshalFrom will unmarshal a ULID from the passed character array.
230
+ * */
231
+ inline void UnmarshalFrom(const char str[26], ULID& ulid)
232
+ {
233
+ // timestamp
234
+ ulid.data[0] = (dec[int(str[0])] << 5) | dec[int(str[1])];
235
+ ulid.data[1] = (dec[int(str[2])] << 3) | (dec[int(str[3])] >> 2);
236
+ ulid.data[2] = (dec[int(str[3])] << 6) | (dec[int(str[4])] << 1) | (dec[int(str[5])] >> 4);
237
+ ulid.data[3] = (dec[int(str[5])] << 4) | (dec[int(str[6])] >> 1);
238
+ ulid.data[4] = (dec[int(str[6])] << 7) | (dec[int(str[7])] << 2) | (dec[int(str[8])] >> 3);
239
+ ulid.data[5] = (dec[int(str[8])] << 5) | dec[int(str[9])];
240
+
241
+ // entropy
242
+ ulid.data[6] = (dec[int(str[10])] << 3) | (dec[int(str[11])] >> 2);
243
+ ulid.data[7] = (dec[int(str[11])] << 6) | (dec[int(str[12])] << 1) | (dec[int(str[13])] >> 4);
244
+ ulid.data[8] = (dec[int(str[13])] << 4) | (dec[int(str[14])] >> 1);
245
+ ulid.data[9] = (dec[int(str[14])] << 7) | (dec[int(str[15])] << 2) | (dec[int(str[16])] >> 3);
246
+ ulid.data[10] = (dec[int(str[16])] << 5) | dec[int(str[17])];
247
+ ulid.data[11] = (dec[int(str[18])] << 3) | (dec[int(str[19])] >> 2);
248
+ ulid.data[12] = (dec[int(str[19])] << 6) | (dec[int(str[20])] << 1) | (dec[int(str[21])] >> 4);
249
+ ulid.data[13] = (dec[int(str[21])] << 4) | (dec[int(str[22])] >> 1);
250
+ ulid.data[14] = (dec[int(str[22])] << 7) | (dec[int(str[23])] << 2) | (dec[int(str[24])] >> 3);
251
+ ulid.data[15] = (dec[int(str[24])] << 5) | dec[int(str[25])];
252
+ }
253
+
254
+ /**
255
+ * UnmarshalBinaryFrom will unmarshal a ULID from the passed byte array.
256
+ * */
257
+ inline void UnmarshalBinaryFrom(const uint8_t b[16], ULID& ulid)
258
+ {
259
+ // timestamp
260
+ ulid.data[0] = b[0];
261
+ ulid.data[1] = b[1];
262
+ ulid.data[2] = b[2];
263
+ ulid.data[3] = b[3];
264
+ ulid.data[4] = b[4];
265
+ ulid.data[5] = b[5];
266
+
267
+ // entropy
268
+ ulid.data[6] = b[6];
269
+ ulid.data[7] = b[7];
270
+ ulid.data[8] = b[8];
271
+ ulid.data[9] = b[9];
272
+ ulid.data[10] = b[10];
273
+ ulid.data[11] = b[11];
274
+ ulid.data[12] = b[12];
275
+ ulid.data[13] = b[13];
276
+ ulid.data[14] = b[14];
277
+ ulid.data[15] = b[15];
278
+ }
279
+
280
+ }; // namespace ulid
281
+
282
+ #endif // ULID_STRUCT_HH
@@ -63,15 +63,6 @@ inline void EncodeTime(std::chrono::time_point<std::chrono::system_clock> time_p
63
63
  EncodeTimestamp(timestamp, ulid);
64
64
  }
65
65
 
66
- /**
67
- * EncodeTimeNow will encode a ULID using the time obtained using std::time(nullptr)
68
- * */
69
- inline void EncodeTimeNow(ULID& ulid)
70
- {
71
- auto time_now = std::chrono::system_clock::from_time_t(time(nullptr));
72
- EncodeTime(time_now, ulid);
73
- }
74
-
75
66
  /**
76
67
  * EncodeTimeSystemClockNow will encode a ULID using the time obtained using
77
68
  * std::chrono::system_clock::now() by taking the timestamp in milliseconds.
@@ -81,87 +72,6 @@ inline void EncodeTimeSystemClockNow(ULID& ulid)
81
72
  EncodeTime(std::chrono::system_clock::now(), ulid);
82
73
  }
83
74
 
84
- /**
85
- * EncodeEntropy will encode the last 10 bytes of the passed uint8_t array with
86
- * the values generated using the passed random number generator.
87
- * */
88
- inline void EncodeEntropy(const std::function<uint8_t()>& rng, ULID& ulid)
89
- {
90
- ulid = (ulid >> 80) << 80;
91
-
92
- ULID 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
- e <<= 8;
116
- e |= rng();
117
-
118
- e <<= 8;
119
- e |= rng();
120
-
121
- ulid |= e;
122
- }
123
-
124
- /**
125
- * EncodeEntropyRand will encode a ulid using std::rand
126
- *
127
- * std::rand returns values in [0, RAND_MAX]
128
- * */
129
- inline void EncodeEntropyRand(ULID& ulid)
130
- {
131
- ulid = (ulid >> 80) << 80;
132
-
133
- ULID e = (std::rand() * 255ull) / RAND_MAX;
134
-
135
- e <<= 8;
136
- e |= (std::rand() * 255ull) / RAND_MAX;
137
-
138
- e <<= 8;
139
- e |= (std::rand() * 255ull) / RAND_MAX;
140
-
141
- e <<= 8;
142
- e |= (std::rand() * 255ull) / RAND_MAX;
143
-
144
- e <<= 8;
145
- e |= (std::rand() * 255ull) / RAND_MAX;
146
-
147
- e <<= 8;
148
- e |= (std::rand() * 255ull) / RAND_MAX;
149
-
150
- e <<= 8;
151
- e |= (std::rand() * 255ull) / RAND_MAX;
152
-
153
- e <<= 8;
154
- e |= (std::rand() * 255ull) / RAND_MAX;
155
-
156
- e <<= 8;
157
- e |= (std::rand() * 255ull) / RAND_MAX;
158
-
159
- e <<= 8;
160
- e |= (std::rand() * 255ull) / RAND_MAX;
161
-
162
- ulid |= e;
163
- }
164
-
165
75
  /**
166
76
  * EncodeEntropyMt19937Fast will encode using std::mt19937
167
77
  * with only 3 generated values.
@@ -182,87 +92,6 @@ inline void EncodeEntropyMt19937Fast(ULID& ulid)
182
92
  ulid |= (static_cast<ULID>((static_cast<uint64_t>(gen()) << 32) | gen()) << 16) | (gen() & 0xFFFF);
183
93
  }
184
94
 
185
- static std::uniform_int_distribution<rand_t> Distribution_0_255(0, 255);
186
-
187
- /**
188
- * EncodeEntropyMt19937 will encode a ulid using std::mt19937
189
- *
190
- * It also creates a std::uniform_int_distribution to generate values in [0, 255]
191
- * */
192
- inline void EncodeEntropyMt19937(std::mt19937& generator, ULID& ulid)
193
- {
194
- ulid = (ulid >> 80) << 80;
195
-
196
- ULID e = Distribution_0_255(generator);
197
-
198
- e <<= 8;
199
- e |= Distribution_0_255(generator);
200
-
201
- e <<= 8;
202
- e |= Distribution_0_255(generator);
203
-
204
- e <<= 8;
205
- e |= Distribution_0_255(generator);
206
-
207
- e <<= 8;
208
- e |= Distribution_0_255(generator);
209
-
210
- e <<= 8;
211
- e |= Distribution_0_255(generator);
212
-
213
- e <<= 8;
214
- e |= Distribution_0_255(generator);
215
-
216
- e <<= 8;
217
- e |= Distribution_0_255(generator);
218
-
219
- e <<= 8;
220
- e |= Distribution_0_255(generator);
221
-
222
- e <<= 8;
223
- e |= Distribution_0_255(generator);
224
-
225
- ulid |= e;
226
- }
227
-
228
- /**
229
- * Encode will create an encoded ULID with a timestamp and a generator.
230
- * */
231
- inline void Encode(std::chrono::time_point<std::chrono::system_clock> timestamp, const std::function<uint8_t()>& rng, ULID& ulid)
232
- {
233
- EncodeTime(timestamp, ulid);
234
- EncodeEntropy(rng, ulid);
235
- }
236
-
237
- /**
238
- * EncodeNowRand = EncodeTimeNow + EncodeEntropyRand.
239
- * */
240
- inline void EncodeNowRand(ULID& ulid)
241
- {
242
- EncodeTimeNow(ulid);
243
- EncodeEntropyRand(ulid);
244
- }
245
-
246
- /**
247
- * Create will create a ULID with a timestamp and a generator.
248
- * */
249
- inline ULID Create(std::chrono::time_point<std::chrono::system_clock> timestamp, const std::function<uint8_t()>& rng)
250
- {
251
- ULID ulid = 0;
252
- Encode(timestamp, rng, ulid);
253
- return ulid;
254
- }
255
-
256
- /**
257
- * CreateNowRand:EncodeNowRand = Create:Encode.
258
- * */
259
- inline ULID CreateNowRand()
260
- {
261
- ULID ulid = 0;
262
- EncodeNowRand(ulid);
263
- return ulid;
264
- }
265
-
266
95
  /**
267
96
  * Crockford's Base32
268
97
  * */
@@ -322,17 +151,6 @@ inline void MarshalTo(const ULID& ulid, char dst[26])
322
151
  dst[25] = Encoding[(static_cast<uint8_t>(ulid)) & 31];
323
152
  }
324
153
 
325
- /**
326
- * Marshal will marshal a ULID to a std::string.
327
- * */
328
- inline std::string Marshal(const ULID& ulid)
329
- {
330
- char data[27];
331
- data[26] = '\0';
332
- MarshalTo(ulid, data);
333
- return std::string(data);
334
- }
335
-
336
154
  /**
337
155
  * MarshalBinaryTo will Marshal a ULID to the passed byte array
338
156
  * */
@@ -359,16 +177,6 @@ inline void MarshalBinaryTo(const ULID& ulid, uint8_t dst[16])
359
177
  dst[15] = static_cast<uint8_t>(ulid);
360
178
  }
361
179
 
362
- /**
363
- * MarshalBinary will Marshal a ULID to a byte vector.
364
- * */
365
- inline std::vector<uint8_t> MarshalBinary(const ULID& ulid)
366
- {
367
- std::vector<uint8_t> dst(16);
368
- MarshalBinaryTo(ulid, dst.data());
369
- return dst;
370
- }
371
-
372
180
  /**
373
181
  * dec storesdecimal encodings for characters.
374
182
  * 0xFF indicates invalid character.
@@ -478,16 +286,6 @@ inline void UnmarshalFrom(const char str[26], ULID& ulid)
478
286
  ulid |= (dec[int(str[24])] << 5) | dec[int(str[25])];
479
287
  }
480
288
 
481
- /**
482
- * Unmarshal will create a new ULID by unmarshaling the passed string.
483
- * */
484
- inline ULID Unmarshal(const std::string& str)
485
- {
486
- ULID ulid;
487
- UnmarshalFrom(str.c_str(), ulid);
488
- return ulid;
489
- }
490
-
491
289
  /**
492
290
  * UnmarshalBinaryFrom will unmarshal a ULID from the passed byte array.
493
291
  * */
@@ -543,55 +341,6 @@ inline void UnmarshalBinaryFrom(const uint8_t b[16], ULID& ulid)
543
341
  ulid |= b[15];
544
342
  }
545
343
 
546
- /**
547
- * Unmarshal will create a new ULID by unmarshaling the passed byte vector.
548
- * */
549
- inline ULID UnmarshalBinary(const std::vector<uint8_t>& b)
550
- {
551
- ULID ulid;
552
- UnmarshalBinaryFrom(b.data(), ulid);
553
- return ulid;
554
- }
555
-
556
- /**
557
- * CompareULIDs will compare two ULIDs.
558
- * returns:
559
- * -1 if ulid1 is Lexicographically before ulid2
560
- * 1 if ulid1 is Lexicographically after ulid2
561
- * 0 if ulid1 is same as ulid2
562
- * */
563
- inline int CompareULIDs(const ULID& ulid1, const ULID& ulid2)
564
- {
565
- return -2 * (ulid1 < ulid2) - 1 * (ulid1 == ulid2) + 1;
566
- }
567
-
568
- /**
569
- * Time will extract the timestamp used to generate a ULID
570
- * */
571
- inline std::chrono::time_point<std::chrono::system_clock> Time(const ULID& ulid)
572
- {
573
- int64_t ans = 0;
574
-
575
- ans |= static_cast<uint8_t>(ulid >> 120);
576
-
577
- ans <<= 8;
578
- ans |= static_cast<uint8_t>(ulid >> 112);
579
-
580
- ans <<= 8;
581
- ans |= static_cast<uint8_t>(ulid >> 104);
582
-
583
- ans <<= 8;
584
- ans |= static_cast<uint8_t>(ulid >> 96);
585
-
586
- ans <<= 8;
587
- ans |= static_cast<uint8_t>(ulid >> 88);
588
-
589
- ans <<= 8;
590
- ans |= static_cast<uint8_t>(ulid >> 80);
591
-
592
- return std::chrono::time_point<std::chrono::system_clock>(std::chrono::milliseconds { ans });
593
- }
594
-
595
344
  }; // namespace ulid
596
345
 
597
346
  #endif // ULID_UINT128_HH
@@ -1,5 +1,10 @@
1
1
  #include "ulid_wrapper.h"
2
- #include "ulid.hh"
2
+
3
+ #ifdef __SIZEOF_INT128__ // http://stackoverflow.com/a/23981011
4
+ #include "ulid_uint128.hh"
5
+ #else
6
+ #include "ulid_struct.hh"
7
+ #endif
3
8
 
4
9
  /**
5
10
  * Generate a new text ULID and write it to the provided buffer.