ulid-transform 0.12.0__tar.gz → 0.13.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.
- {ulid_transform-0.12.0 → ulid_transform-0.13.1}/PKG-INFO +1 -1
- {ulid_transform-0.12.0 → ulid_transform-0.13.1}/pyproject.toml +1 -1
- {ulid_transform-0.12.0 → ulid_transform-0.13.1}/setup.py +1 -1
- {ulid_transform-0.12.0 → ulid_transform-0.13.1}/src/ulid_transform/__init__.py +4 -1
- ulid_transform-0.13.1/src/ulid_transform/__init__.pyi +10 -0
- {ulid_transform-0.12.0 → ulid_transform-0.13.1}/src/ulid_transform/_py_ulid_impl.py +12 -0
- ulid_transform-0.13.1/src/ulid_transform/_ulid_impl.pyx +136 -0
- ulid_transform-0.13.1/src/ulid_transform/ulid_wrapper.cpp +94 -0
- ulid_transform-0.13.1/src/ulid_transform/ulid_wrapper.h +13 -0
- ulid_transform-0.12.0/src/ulid_transform/_ulid_impl.pyx +0 -91
- ulid_transform-0.12.0/src/ulid_transform/ulid_wrapper.cpp +0 -46
- ulid_transform-0.12.0/src/ulid_transform/ulid_wrapper.h +0 -14
- {ulid_transform-0.12.0 → ulid_transform-0.13.1}/LICENSE +0 -0
- {ulid_transform-0.12.0 → ulid_transform-0.13.1}/README.md +0 -0
- {ulid_transform-0.12.0 → ulid_transform-0.13.1}/build_ext.py +0 -0
- {ulid_transform-0.12.0 → ulid_transform-0.13.1}/src/ulid_transform/py.typed +0 -0
- {ulid_transform-0.12.0 → ulid_transform-0.13.1}/src/ulid_transform/ulid.hh +0 -0
- {ulid_transform-0.12.0 → ulid_transform-0.13.1}/src/ulid_transform/ulid_struct.hh +0 -0
- {ulid_transform-0.12.0 → ulid_transform-0.13.1}/src/ulid_transform/ulid_uint128.hh +0 -0
@@ -12,7 +12,7 @@ package_data = \
|
|
12
12
|
|
13
13
|
setup_kwargs = {
|
14
14
|
'name': 'ulid-transform',
|
15
|
-
'version': '0.
|
15
|
+
'version': '0.13.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/bdraco/ulid-transform/actions/workflows/ci.yml?query=branch%3Amain">\n <img src="https://img.shields.io/github/actions/workflow/status/bdraco/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/bdraco/ulid-transform">\n <img src="https://img.shields.io/codecov/c/github/bdraco/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</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&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__ = "0.
|
1
|
+
__version__ = "0.13.1"
|
2
2
|
|
3
3
|
try:
|
4
4
|
from ._ulid_impl import (
|
@@ -11,6 +11,7 @@ try:
|
|
11
11
|
ulid_now_bytes,
|
12
12
|
ulid_to_bytes,
|
13
13
|
ulid_to_bytes_or_none,
|
14
|
+
ulid_to_timestamp,
|
14
15
|
)
|
15
16
|
except ImportError:
|
16
17
|
from ._py_ulid_impl import (
|
@@ -23,6 +24,7 @@ except ImportError:
|
|
23
24
|
ulid_now_bytes,
|
24
25
|
ulid_to_bytes,
|
25
26
|
ulid_to_bytes_or_none,
|
27
|
+
ulid_to_timestamp,
|
26
28
|
)
|
27
29
|
|
28
30
|
__all__ = [
|
@@ -35,4 +37,5 @@ __all__ = [
|
|
35
37
|
"ulid_now_bytes",
|
36
38
|
"ulid_to_bytes",
|
37
39
|
"ulid_to_bytes_or_none",
|
40
|
+
"ulid_to_timestamp",
|
38
41
|
]
|
@@ -0,0 +1,10 @@
|
|
1
|
+
def ulid_hex() -> str: ...
|
2
|
+
def ulid_at_time_bytes(timestamp: float) -> bytes: ...
|
3
|
+
def ulid_now_bytes() -> bytes: ...
|
4
|
+
def ulid_now() -> str: ...
|
5
|
+
def ulid_at_time(timestamp: float) -> str: ...
|
6
|
+
def ulid_to_bytes(value: str) -> bytes: ...
|
7
|
+
def bytes_to_ulid(value: bytes) -> str: ...
|
8
|
+
def ulid_to_bytes_or_none(ulid: str | None) -> bytes | None: ...
|
9
|
+
def bytes_to_ulid_or_none(ulid_bytes: bytes | None) -> str | None: ...
|
10
|
+
def ulid_to_timestamp(ulid: str | bytes) -> int: ...
|
@@ -440,3 +440,15 @@ def bytes_to_ulid_or_none(ulid_bytes: bytes | None) -> str | None:
|
|
440
440
|
return bytes_to_ulid(ulid_bytes)
|
441
441
|
except ValueError:
|
442
442
|
return None
|
443
|
+
|
444
|
+
|
445
|
+
def ulid_to_timestamp(ulid: str | bytes) -> int:
|
446
|
+
"""
|
447
|
+
Get the timestamp from a ULID.
|
448
|
+
The returned value is in milliseconds since the UNIX epoch.
|
449
|
+
"""
|
450
|
+
if not isinstance(ulid, bytes):
|
451
|
+
ulid_bytes = ulid_to_bytes(ulid)
|
452
|
+
else:
|
453
|
+
ulid_bytes = ulid
|
454
|
+
return int.from_bytes(b"\x00\x00" + ulid_bytes[:6], "big")
|
@@ -0,0 +1,136 @@
|
|
1
|
+
# distutils: language = c++
|
2
|
+
# cython: language_level=3, c_string_type=str, c_string_encoding=ascii
|
3
|
+
|
4
|
+
# The `<bytes>xxx[:N]` syntax is required for two reasons:
|
5
|
+
# 1. When working with "ULID bytes", the buffer may contain NULs.
|
6
|
+
# 2. When working with ULID text, the buffer is exactly 26 bytes long and not NUL-terminated.
|
7
|
+
# See https://github.com/cython/cython/issues/3234
|
8
|
+
|
9
|
+
from libc.stdint cimport uint8_t, uint64_t
|
10
|
+
|
11
|
+
|
12
|
+
cdef extern from "ulid_wrapper.h":
|
13
|
+
void _cpp_ulid(char dst[26]) nogil
|
14
|
+
void _cpp_ulid_bytes(uint8_t dst[16]) nogil
|
15
|
+
void _cpp_ulid_at_time(double epoch_time, char dst[26]) nogil
|
16
|
+
void _cpp_ulid_at_time_bytes(double epoch_time, uint8_t dst[16]) nogil
|
17
|
+
void _cpp_ulid_to_bytes(const char ulid_string[26], uint8_t dst[16]) nogil
|
18
|
+
void _cpp_bytes_to_ulid(const uint8_t b[16], char * dst) nogil
|
19
|
+
void _cpp_hexlify_16(const uint8_t b[16], char dst[32]) nogil
|
20
|
+
uint64_t _cpp_bytes_to_timestamp(const uint8_t b[16]) nogil
|
21
|
+
|
22
|
+
|
23
|
+
def ulid_hex() -> str:
|
24
|
+
"""Generate a ULID in lowercase hex that will work for a UUID.
|
25
|
+
|
26
|
+
This ulid should not be used for cryptographically secure
|
27
|
+
operations.
|
28
|
+
|
29
|
+
This string can be converted with https://github.com/ahawker/ulid
|
30
|
+
|
31
|
+
ulid.from_uuid(uuid.UUID(ulid_hex))
|
32
|
+
"""
|
33
|
+
cdef unsigned char ulid_bytes_buf[16]
|
34
|
+
_cpp_ulid_bytes(ulid_bytes_buf)
|
35
|
+
cdef char ulid_hex_buf[32]
|
36
|
+
_cpp_hexlify_16(ulid_bytes_buf, ulid_hex_buf)
|
37
|
+
return <str>ulid_hex_buf[:32]
|
38
|
+
|
39
|
+
|
40
|
+
def ulid_now_bytes() -> bytes:
|
41
|
+
"""Generate an ULID as 16 bytes that will work for a UUID."""
|
42
|
+
cdef unsigned char ulid_bytes_buf[16]
|
43
|
+
_cpp_ulid_bytes(ulid_bytes_buf)
|
44
|
+
return <bytes>ulid_bytes_buf[:16]
|
45
|
+
|
46
|
+
|
47
|
+
def ulid_at_time_bytes(timestamp: float) -> bytes:
|
48
|
+
"""Generate an ULID as 16 bytes that will work for a UUID.
|
49
|
+
|
50
|
+
uuid.UUID(bytes=ulid_bytes)
|
51
|
+
"""
|
52
|
+
cdef unsigned char ulid_bytes_buf[16]
|
53
|
+
_cpp_ulid_at_time_bytes(timestamp, ulid_bytes_buf)
|
54
|
+
return <bytes>ulid_bytes_buf[:16]
|
55
|
+
|
56
|
+
|
57
|
+
def ulid_now() -> str:
|
58
|
+
"""Generate a ULID."""
|
59
|
+
cdef char ulid_text_buf[26]
|
60
|
+
_cpp_ulid(ulid_text_buf)
|
61
|
+
return <str>ulid_text_buf[:26]
|
62
|
+
|
63
|
+
|
64
|
+
def ulid_at_time(timestamp: float) -> str:
|
65
|
+
"""Generate a ULID.
|
66
|
+
|
67
|
+
This ulid should not be used for cryptographically secure
|
68
|
+
operations.
|
69
|
+
|
70
|
+
01AN4Z07BY 79KA1307SR9X4MV3
|
71
|
+
|----------| |----------------|
|
72
|
+
Timestamp Randomness
|
73
|
+
48bits 80bits
|
74
|
+
|
75
|
+
This string can be loaded directly with https://github.com/ahawker/ulid
|
76
|
+
|
77
|
+
import ulid_transform as ulid_util
|
78
|
+
import ulid
|
79
|
+
ulid.parse(ulid_util.ulid())
|
80
|
+
"""
|
81
|
+
cdef char ulid_text_buf[26]
|
82
|
+
_cpp_ulid_at_time(timestamp, ulid_text_buf)
|
83
|
+
return <str>ulid_text_buf[:26]
|
84
|
+
|
85
|
+
|
86
|
+
def ulid_to_bytes(value: str) -> bytes:
|
87
|
+
"""Decode a ulid to bytes."""
|
88
|
+
if len(value) != 26:
|
89
|
+
raise ValueError(f"ULID must be a 26 character string: {value}")
|
90
|
+
cdef unsigned char ulid_bytes_buf[16]
|
91
|
+
_cpp_ulid_to_bytes(value, ulid_bytes_buf)
|
92
|
+
return <bytes>ulid_bytes_buf[:16]
|
93
|
+
|
94
|
+
|
95
|
+
def bytes_to_ulid(value: bytes) -> str:
|
96
|
+
"""Encode bytes to a ulid."""
|
97
|
+
if len(value) != 16:
|
98
|
+
raise ValueError(f"ULID bytes must be 16 bytes: {value!r}")
|
99
|
+
cdef char ulid_text_buf[26]
|
100
|
+
_cpp_bytes_to_ulid(value, ulid_text_buf)
|
101
|
+
return <str>ulid_text_buf[:26]
|
102
|
+
|
103
|
+
|
104
|
+
def ulid_to_bytes_or_none(ulid: str | None) -> bytes | None:
|
105
|
+
"""Convert an ulid to bytes."""
|
106
|
+
if ulid is None or len(ulid) != 26:
|
107
|
+
return None
|
108
|
+
cdef unsigned char ulid_bytes_buf[16]
|
109
|
+
_cpp_ulid_to_bytes(ulid, ulid_bytes_buf)
|
110
|
+
return <bytes>ulid_bytes_buf[:16]
|
111
|
+
|
112
|
+
|
113
|
+
def bytes_to_ulid_or_none(ulid_bytes: bytes | None) -> str | None:
|
114
|
+
"""Convert bytes to a ulid."""
|
115
|
+
if ulid_bytes is None or len(ulid_bytes) != 16:
|
116
|
+
return None
|
117
|
+
cdef char ulid_text_buf[26]
|
118
|
+
_cpp_bytes_to_ulid(ulid_bytes, ulid_text_buf)
|
119
|
+
return <str>ulid_text_buf[:26]
|
120
|
+
|
121
|
+
|
122
|
+
def ulid_to_timestamp(ulid: str | bytes) -> int:
|
123
|
+
"""
|
124
|
+
Get the timestamp from a ULID.
|
125
|
+
The returned value is in milliseconds since the UNIX epoch.
|
126
|
+
"""
|
127
|
+
cdef unsigned char ulid_bytes_buf[16]
|
128
|
+
if not isinstance(ulid, bytes):
|
129
|
+
if len(ulid) != 26:
|
130
|
+
raise ValueError(f"ULID must be a 26 character string: {ulid}")
|
131
|
+
_cpp_ulid_to_bytes(ulid, ulid_bytes_buf)
|
132
|
+
return _cpp_bytes_to_timestamp(ulid_bytes_buf)
|
133
|
+
else:
|
134
|
+
if len(ulid) != 16:
|
135
|
+
raise ValueError(f"ULID bytes must be 16 bytes: {ulid!r}")
|
136
|
+
return _cpp_bytes_to_timestamp(ulid)
|
@@ -0,0 +1,94 @@
|
|
1
|
+
#include "ulid_wrapper.h"
|
2
|
+
#include "ulid.hh"
|
3
|
+
|
4
|
+
/**
|
5
|
+
* Generate a new text ULID and write it to the provided buffer.
|
6
|
+
* The buffer is NOT null-terminated.
|
7
|
+
*/
|
8
|
+
void _cpp_ulid(char dst[26]) {
|
9
|
+
ulid::ULID ulid;
|
10
|
+
ulid::EncodeTimeSystemClockNow(ulid);
|
11
|
+
ulid::EncodeEntropyRand(ulid);
|
12
|
+
ulid::MarshalTo(ulid, dst);
|
13
|
+
}
|
14
|
+
|
15
|
+
/**
|
16
|
+
* Generate a new binary ULID and write it to the provided buffer.
|
17
|
+
*/
|
18
|
+
void _cpp_ulid_bytes(uint8_t dst[16]) {
|
19
|
+
ulid::ULID ulid;
|
20
|
+
ulid::EncodeTimeSystemClockNow(ulid);
|
21
|
+
ulid::EncodeEntropyRand(ulid);
|
22
|
+
ulid::MarshalBinaryTo(ulid, dst);
|
23
|
+
}
|
24
|
+
|
25
|
+
/**
|
26
|
+
* Generate a new text ULID at the provided epoch time and write it to the provided buffer.
|
27
|
+
* The buffer is NOT null-terminated.
|
28
|
+
*/
|
29
|
+
void _cpp_ulid_at_time(double epoch_time, char dst[26]) {
|
30
|
+
ulid::ULID ulid;
|
31
|
+
ulid::EncodeTimestamp(static_cast<int64_t>(epoch_time*1000), ulid);
|
32
|
+
ulid::EncodeEntropyRand(ulid);
|
33
|
+
ulid::MarshalTo(ulid, dst);
|
34
|
+
}
|
35
|
+
|
36
|
+
/**
|
37
|
+
* Generate a new binary ULID at the provided epoch time and write it to the provided buffer.
|
38
|
+
*/
|
39
|
+
void _cpp_ulid_at_time_bytes(double epoch_time, uint8_t dst[16]) {
|
40
|
+
ulid::ULID ulid;
|
41
|
+
ulid::EncodeTimestamp(static_cast<int64_t>(epoch_time*1000), ulid);
|
42
|
+
ulid::EncodeEntropyRand(ulid);
|
43
|
+
ulid::MarshalBinaryTo(ulid, dst);
|
44
|
+
}
|
45
|
+
|
46
|
+
/**
|
47
|
+
* Convert a text ULID to a binary ULID.
|
48
|
+
* The buffer passed in must contain at least 26 bytes.
|
49
|
+
* Invalid data will result in undefined behavior.
|
50
|
+
*/
|
51
|
+
void _cpp_ulid_to_bytes(const char * ulid_string, uint8_t dst[16]) {
|
52
|
+
ulid::ULID ulid;
|
53
|
+
ulid::UnmarshalFrom(ulid_string, ulid);
|
54
|
+
ulid::MarshalBinaryTo(ulid, dst);
|
55
|
+
}
|
56
|
+
|
57
|
+
/**
|
58
|
+
* Convert a binary ULID to a text ULID.
|
59
|
+
* The buffer passed in must contain at least 16 bytes.
|
60
|
+
* The output buffer will NOT be null-terminated.
|
61
|
+
*/
|
62
|
+
void _cpp_bytes_to_ulid(const uint8_t b[16], char dst[26]) {
|
63
|
+
ulid::ULID ulid;
|
64
|
+
ulid::UnmarshalBinaryFrom(b, ulid);
|
65
|
+
ulid::MarshalTo(ulid, dst);
|
66
|
+
}
|
67
|
+
|
68
|
+
/**
|
69
|
+
* Convert a buffer of exactly 16 bytes to 32 hex characters.
|
70
|
+
* The output buffer will NOT be null-terminated.
|
71
|
+
*/
|
72
|
+
void _cpp_hexlify_16(const uint8_t b[16], char dst[32]) {
|
73
|
+
static const char hexdigits[17] = "0123456789abcdef";
|
74
|
+
int in_index, out_index;
|
75
|
+
for (in_index = out_index = 0; in_index < 16; in_index++) {
|
76
|
+
uint8_t c = b[in_index];
|
77
|
+
dst[out_index++] = hexdigits[c >> 4];
|
78
|
+
dst[out_index++] = hexdigits[c & 0x0f];
|
79
|
+
}
|
80
|
+
}
|
81
|
+
|
82
|
+
/**
|
83
|
+
* Interpret the first 6 bytes of a binary ULID as a timestamp.
|
84
|
+
*/
|
85
|
+
uint64_t _cpp_bytes_to_timestamp(const uint8_t b[16]) {
|
86
|
+
uint64_t timestamp = 0;
|
87
|
+
timestamp |= static_cast<uint64_t>(b[0]) << 40;
|
88
|
+
timestamp |= static_cast<uint64_t>(b[1]) << 32;
|
89
|
+
timestamp |= static_cast<uint64_t>(b[2]) << 24;
|
90
|
+
timestamp |= static_cast<uint64_t>(b[3]) << 16;
|
91
|
+
timestamp |= static_cast<uint64_t>(b[4]) << 8;
|
92
|
+
timestamp |= static_cast<uint64_t>(b[5]);
|
93
|
+
return timestamp;
|
94
|
+
}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
#ifndef ULID_WRAPPER_H
|
2
|
+
#define ULID_WRAPPER_H
|
3
|
+
#include <stdint.h>
|
4
|
+
|
5
|
+
void _cpp_ulid(char dst[26]);
|
6
|
+
void _cpp_ulid_bytes(uint8_t dst[16]);
|
7
|
+
void _cpp_ulid_at_time(double epoch_time, char dst[26]);
|
8
|
+
void _cpp_ulid_at_time_bytes(double epoch_time, uint8_t dst[16]);
|
9
|
+
void _cpp_ulid_to_bytes(const char * ulid_string, uint8_t dst[16]);
|
10
|
+
void _cpp_bytes_to_ulid(const uint8_t b[16], char dst[26]);
|
11
|
+
void _cpp_hexlify_16(const uint8_t b[16], char dst[32]);
|
12
|
+
uint64_t _cpp_bytes_to_timestamp(const uint8_t b[16]);
|
13
|
+
#endif
|
@@ -1,91 +0,0 @@
|
|
1
|
-
# distutils: language = c++
|
2
|
-
from libcpp.string cimport string
|
3
|
-
from libcpp.vector cimport vector
|
4
|
-
|
5
|
-
|
6
|
-
cdef extern from "ulid_wrapper.h":
|
7
|
-
string _cpp_ulid_at_time(double timestamp)
|
8
|
-
vector[unsigned char] _cpp_ulid_at_time_bytes(double timestamp)
|
9
|
-
string _cpp_ulid_to_bytes(const char * ulid_string)
|
10
|
-
string _cpp_ulid()
|
11
|
-
vector[unsigned char] _cpp_ulid_bytes()
|
12
|
-
string _cpp_bytes_to_ulid(string ulid_bytes)
|
13
|
-
|
14
|
-
|
15
|
-
def ulid_hex() -> str:
|
16
|
-
"""Generate a ULID in lowercase hex that will work for a UUID.
|
17
|
-
|
18
|
-
This ulid should not be used for cryptographically secure
|
19
|
-
operations.
|
20
|
-
|
21
|
-
This string can be converted with https://github.com/ahawker/ulid
|
22
|
-
|
23
|
-
ulid.from_uuid(uuid.UUID(ulid_hex))
|
24
|
-
"""
|
25
|
-
return bytes(_cpp_ulid_bytes()).hex()
|
26
|
-
|
27
|
-
|
28
|
-
def ulid_now_bytes() -> bytes:
|
29
|
-
"""Generate an ULID as 16 bytes that will work for a UUID."""
|
30
|
-
return bytes(_cpp_ulid_bytes())
|
31
|
-
|
32
|
-
|
33
|
-
def ulid_at_time_bytes(timestamp: float) -> bytes:
|
34
|
-
"""Generate an ULID as 16 bytes that will work for a UUID.
|
35
|
-
|
36
|
-
uuid.UUID(bytes=ulid_bytes)
|
37
|
-
"""
|
38
|
-
return bytes(_cpp_ulid_at_time_bytes(timestamp))
|
39
|
-
|
40
|
-
|
41
|
-
def ulid_now() -> str:
|
42
|
-
"""Generate a ULID."""
|
43
|
-
return _cpp_ulid().decode("ascii")
|
44
|
-
|
45
|
-
|
46
|
-
def ulid_at_time(timestamp: float) -> str:
|
47
|
-
"""Generate a ULID.
|
48
|
-
|
49
|
-
This ulid should not be used for cryptographically secure
|
50
|
-
operations.
|
51
|
-
|
52
|
-
01AN4Z07BY 79KA1307SR9X4MV3
|
53
|
-
|----------| |----------------|
|
54
|
-
Timestamp Randomness
|
55
|
-
48bits 80bits
|
56
|
-
|
57
|
-
This string can be loaded directly with https://github.com/ahawker/ulid
|
58
|
-
|
59
|
-
import ulid_transform as ulid_util
|
60
|
-
import ulid
|
61
|
-
ulid.parse(ulid_util.ulid())
|
62
|
-
"""
|
63
|
-
return _cpp_ulid_at_time(timestamp).decode("ascii")
|
64
|
-
|
65
|
-
|
66
|
-
def ulid_to_bytes(value: str) -> bytes:
|
67
|
-
"""Decode a ulid to bytes."""
|
68
|
-
if len(value) != 26:
|
69
|
-
raise ValueError(f"ULID must be a 26 character string: {value}")
|
70
|
-
return _cpp_ulid_to_bytes(value.encode("ascii"))
|
71
|
-
|
72
|
-
|
73
|
-
def bytes_to_ulid(value: bytes) -> str:
|
74
|
-
"""Encode bytes to a ulid."""
|
75
|
-
if len(value) != 16:
|
76
|
-
raise ValueError(f"ULID bytes must be 16 bytes: {value!r}")
|
77
|
-
return _cpp_bytes_to_ulid(value).decode("ascii")
|
78
|
-
|
79
|
-
|
80
|
-
def ulid_to_bytes_or_none(ulid: str | None) -> bytes | None:
|
81
|
-
"""Convert an ulid to bytes."""
|
82
|
-
if ulid is None or len(ulid) != 26:
|
83
|
-
return None
|
84
|
-
return _cpp_ulid_to_bytes(ulid.encode("ascii"))
|
85
|
-
|
86
|
-
|
87
|
-
def bytes_to_ulid_or_none(ulid_bytes: bytes | None) -> str | None:
|
88
|
-
"""Convert bytes to a ulid."""
|
89
|
-
if ulid_bytes is None or len(ulid_bytes) != 16:
|
90
|
-
return None
|
91
|
-
return _cpp_bytes_to_ulid(ulid_bytes).decode("ascii")
|
@@ -1,46 +0,0 @@
|
|
1
|
-
#include "ulid_wrapper.h"
|
2
|
-
#include "ulid.hh"
|
3
|
-
|
4
|
-
using namespace std;
|
5
|
-
|
6
|
-
std::string _cpp_ulid() {
|
7
|
-
ulid::ULID ulid;
|
8
|
-
ulid::EncodeTimeSystemClockNow(ulid);
|
9
|
-
ulid::EncodeEntropyRand(ulid);
|
10
|
-
return ulid::Marshal(ulid);
|
11
|
-
}
|
12
|
-
|
13
|
-
std::vector<uint8_t> _cpp_ulid_bytes() {
|
14
|
-
ulid::ULID ulid;
|
15
|
-
ulid::EncodeTimeSystemClockNow(ulid);
|
16
|
-
ulid::EncodeEntropyRand(ulid);
|
17
|
-
return ulid::MarshalBinary(ulid);
|
18
|
-
}
|
19
|
-
|
20
|
-
std::string _cpp_ulid_at_time(double epoch_time) {
|
21
|
-
ulid::ULID ulid;
|
22
|
-
ulid::EncodeTimestamp(static_cast<int64_t>(epoch_time*1000), ulid);
|
23
|
-
ulid::EncodeEntropyRand(ulid);
|
24
|
-
return ulid::Marshal(ulid);
|
25
|
-
}
|
26
|
-
|
27
|
-
std::vector<uint8_t> _cpp_ulid_at_time_bytes(double epoch_time) {
|
28
|
-
ulid::ULID ulid;
|
29
|
-
ulid::EncodeTimestamp(static_cast<int64_t>(epoch_time*1000), ulid);
|
30
|
-
ulid::EncodeEntropyRand(ulid);
|
31
|
-
return ulid::MarshalBinary(ulid);
|
32
|
-
}
|
33
|
-
|
34
|
-
std::string _cpp_ulid_to_bytes(const char * ulid_string) {
|
35
|
-
ulid::ULID ulid;
|
36
|
-
ulid::UnmarshalFrom(ulid_string, ulid);
|
37
|
-
std::vector<uint8_t> data = ulid::MarshalBinary(ulid);
|
38
|
-
std::string str(reinterpret_cast<char *>(data.data()), data.size());
|
39
|
-
return str;
|
40
|
-
}
|
41
|
-
|
42
|
-
std::string _cpp_bytes_to_ulid(std::string bytes_string) {
|
43
|
-
std::vector<uint8_t> data(bytes_string.begin(), bytes_string.end());
|
44
|
-
ulid::ULID ulid = ulid::UnmarshalBinary(data);
|
45
|
-
return ulid::Marshal(ulid);
|
46
|
-
}
|
@@ -1,14 +0,0 @@
|
|
1
|
-
#include <string>
|
2
|
-
#include <vector>
|
3
|
-
|
4
|
-
#ifndef ULID_WRAPPER_H
|
5
|
-
#define ULID_WRAPPER_H
|
6
|
-
|
7
|
-
std::string _cpp_ulid();
|
8
|
-
std::vector<uint8_t> _cpp_ulid_bytes();
|
9
|
-
std::string _cpp_ulid_at_time(double timestamp);
|
10
|
-
std::vector<uint8_t> _cpp_ulid_at_time_bytes(double timestamp);
|
11
|
-
std::string _cpp_ulid_to_bytes(const char * ulid_string);
|
12
|
-
std::string _cpp_bytes_to_ulid(std::string bytes_string);
|
13
|
-
|
14
|
-
#endif
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|