|
|
@ -1,4 +1,5 @@ |
|
|
|
#pragma once |
|
|
|
#ifndef ULID_STRUCT_HH |
|
|
|
#define ULID_STRUCT_HH |
|
|
|
|
|
|
|
#include <chrono> |
|
|
|
#include <cstdlib> |
|
|
@ -7,6 +8,8 @@ |
|
|
|
#include <random> |
|
|
|
#include <vector> |
|
|
|
|
|
|
|
extern long intrand(); |
|
|
|
|
|
|
|
namespace ulid { |
|
|
|
|
|
|
|
/**
|
|
|
@ -248,7 +251,7 @@ struct ULID { |
|
|
|
* EncodeTime will encode the first 6 bytes of a uint8_t array to the passed |
|
|
|
* timestamp |
|
|
|
* */ |
|
|
|
void EncodeTime(time_t timestamp, ULID& ulid) { |
|
|
|
inline void EncodeTime(time_t timestamp, ULID& ulid) { |
|
|
|
ulid.data[0] = static_cast<uint8_t>(timestamp >> 40); |
|
|
|
ulid.data[1] = static_cast<uint8_t>(timestamp >> 32); |
|
|
|
ulid.data[2] = static_cast<uint8_t>(timestamp >> 24); |
|
|
@ -260,7 +263,7 @@ void EncodeTime(time_t timestamp, ULID& ulid) { |
|
|
|
/**
|
|
|
|
* EncodeTimeNow will encode a ULID using the time obtained using std::time(nullptr) |
|
|
|
* */ |
|
|
|
void EncodeTimeNow(ULID& ulid) { |
|
|
|
inline void EncodeTimeNow(ULID& ulid) { |
|
|
|
EncodeTime(std::time(nullptr), ulid); |
|
|
|
} |
|
|
|
|
|
|
@ -268,7 +271,7 @@ void EncodeTimeNow(ULID& ulid) { |
|
|
|
* EncodeTimeSystemClockNow will encode a ULID using the time obtained using |
|
|
|
* std::chrono::system_clock::now() by taking the timestamp in milliseconds. |
|
|
|
* */ |
|
|
|
void EncodeTimeSystemClockNow(ULID& ulid) { |
|
|
|
inline void EncodeTimeSystemClockNow(ULID& ulid) { |
|
|
|
auto now = std::chrono::system_clock::now(); |
|
|
|
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()); |
|
|
|
EncodeTime(ms.count(), ulid); |
|
|
@ -278,7 +281,7 @@ void EncodeTimeSystemClockNow(ULID& ulid) { |
|
|
|
* EncodeEntropy will encode the last 10 bytes of the passed uint8_t array with |
|
|
|
* the values generated using the passed random number generator. |
|
|
|
* */ |
|
|
|
void EncodeEntropy(const std::function<uint8_t()>& rng, ULID& ulid) { |
|
|
|
inline void EncodeEntropy(const std::function<uint8_t()>& rng, ULID& ulid) { |
|
|
|
ulid.data[6] = rng(); |
|
|
|
ulid.data[7] = rng(); |
|
|
|
ulid.data[8] = rng(); |
|
|
@ -297,26 +300,36 @@ void EncodeEntropy(const std::function<uint8_t()>& rng, ULID& ulid) { |
|
|
|
* std::rand returns values in [0, RAND_MAX] |
|
|
|
* */ |
|
|
|
void EncodeEntropyRand(ULID& ulid) { |
|
|
|
ulid.data[6] = (std::rand() * 255ull) / RAND_MAX; |
|
|
|
ulid.data[7] = (std::rand() * 255ull) / RAND_MAX; |
|
|
|
ulid.data[8] = (std::rand() * 255ull) / RAND_MAX; |
|
|
|
ulid.data[9] = (std::rand() * 255ull) / RAND_MAX; |
|
|
|
ulid.data[10] = (std::rand() * 255ull) / RAND_MAX; |
|
|
|
ulid.data[11] = (std::rand() * 255ull) / RAND_MAX; |
|
|
|
ulid.data[12] = (std::rand() * 255ull) / RAND_MAX; |
|
|
|
ulid.data[13] = (std::rand() * 255ull) / RAND_MAX; |
|
|
|
ulid.data[14] = (std::rand() * 255ull) / RAND_MAX; |
|
|
|
ulid.data[15] = (std::rand() * 255ull) / RAND_MAX; |
|
|
|
ulid.data[6] = (intrand() * 255ull) / RAND_MAX; |
|
|
|
ulid.data[7] = (intrand() * 255ull) / RAND_MAX; |
|
|
|
ulid.data[8] = (intrand() * 255ull) / RAND_MAX; |
|
|
|
ulid.data[9] = (intrand() * 255ull) / RAND_MAX; |
|
|
|
ulid.data[10] = (intrand() * 255ull) / RAND_MAX; |
|
|
|
ulid.data[11] = (intrand() * 255ull) / RAND_MAX; |
|
|
|
ulid.data[12] = (intrand() * 255ull) / RAND_MAX; |
|
|
|
ulid.data[13] = (intrand() * 255ull) / RAND_MAX; |
|
|
|
ulid.data[14] = (intrand() * 255ull) / RAND_MAX; |
|
|
|
ulid.data[15] = (intrand() * 255ull) / RAND_MAX; |
|
|
|
// ulid.data[6] = (std::rand() * 255ull) / RAND_MAX;
|
|
|
|
// ulid.data[7] = (std::rand() * 255ull) / RAND_MAX;
|
|
|
|
// ulid.data[8] = (std::rand() * 255ull) / RAND_MAX;
|
|
|
|
// ulid.data[9] = (std::rand() * 255ull) / RAND_MAX;
|
|
|
|
// ulid.data[10] = (std::rand() * 255ull) / RAND_MAX;
|
|
|
|
// ulid.data[11] = (std::rand() * 255ull) / RAND_MAX;
|
|
|
|
// ulid.data[12] = (std::rand() * 255ull) / RAND_MAX;
|
|
|
|
// ulid.data[13] = (std::rand() * 255ull) / RAND_MAX;
|
|
|
|
// ulid.data[14] = (std::rand() * 255ull) / RAND_MAX;
|
|
|
|
// ulid.data[15] = (std::rand() * 255ull) / RAND_MAX;
|
|
|
|
} |
|
|
|
|
|
|
|
std::uniform_int_distribution<uint8_t> Distribution_0_255(0, 255); |
|
|
|
static std::uniform_int_distribution<uint8_t> Distribution_0_255(0, 255); |
|
|
|
|
|
|
|
/**
|
|
|
|
* EncodeEntropyMt19937 will encode a ulid using std::mt19937 |
|
|
|
* |
|
|
|
* It also creates a std::uniform_int_distribution to generate values in [0, 255] |
|
|
|
* */ |
|
|
|
void EncodeEntropyMt19937(std::mt19937& generator, ULID& ulid) { |
|
|
|
inline void EncodeEntropyMt19937(std::mt19937& generator, ULID& ulid) { |
|
|
|
ulid.data[6] = Distribution_0_255(generator); |
|
|
|
ulid.data[7] = Distribution_0_255(generator); |
|
|
|
ulid.data[8] = Distribution_0_255(generator); |
|
|
@ -332,7 +345,7 @@ void EncodeEntropyMt19937(std::mt19937& generator, ULID& ulid) { |
|
|
|
/**
|
|
|
|
* Encode will create an encoded ULID with a timestamp and a generator. |
|
|
|
* */ |
|
|
|
void Encode(time_t timestamp, const std::function<uint8_t()>& rng, ULID& ulid) { |
|
|
|
inline void Encode(time_t timestamp, const std::function<uint8_t()>& rng, ULID& ulid) { |
|
|
|
EncodeTime(timestamp, ulid); |
|
|
|
EncodeEntropy(rng, ulid); |
|
|
|
} |
|
|
@ -340,7 +353,7 @@ void Encode(time_t timestamp, const std::function<uint8_t()>& rng, ULID& ulid) { |
|
|
|
/**
|
|
|
|
* EncodeNowRand = EncodeTimeNow + EncodeEntropyRand. |
|
|
|
* */ |
|
|
|
void EncodeNowRand(ULID& ulid) { |
|
|
|
inline void EncodeNowRand(ULID& ulid) { |
|
|
|
EncodeTimeNow(ulid); |
|
|
|
EncodeEntropyRand(ulid); |
|
|
|
} |
|
|
@ -348,7 +361,7 @@ void EncodeNowRand(ULID& ulid) { |
|
|
|
/**
|
|
|
|
* Create will create a ULID with a timestamp and a generator. |
|
|
|
* */ |
|
|
|
ULID Create(time_t timestamp, const std::function<uint8_t()>& rng) { |
|
|
|
inline ULID Create(time_t timestamp, const std::function<uint8_t()>& rng) { |
|
|
|
ULID ulid; |
|
|
|
Encode(timestamp, rng, ulid); |
|
|
|
return ulid; |
|
|
@ -357,7 +370,7 @@ ULID Create(time_t timestamp, const std::function<uint8_t()>& rng) { |
|
|
|
/**
|
|
|
|
* CreateNowRand:EncodeNowRand = Create:Encode. |
|
|
|
* */ |
|
|
|
ULID CreateNowRand() { |
|
|
|
inline ULID CreateNowRand() { |
|
|
|
ULID ulid; |
|
|
|
EncodeNowRand(ulid); |
|
|
|
return ulid; |
|
|
@ -366,7 +379,7 @@ ULID CreateNowRand() { |
|
|
|
/**
|
|
|
|
* Crockford's Base32 |
|
|
|
* */ |
|
|
|
const char Encoding[33] = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; |
|
|
|
static const char Encoding[33] = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; |
|
|
|
|
|
|
|
/**
|
|
|
|
* MarshalTo will marshal a ULID to the passed character array. |
|
|
@ -389,7 +402,7 @@ const char Encoding[33] = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; |
|
|
|
* entropy: |
|
|
|
* follows similarly, except now all components are set to 5 bits. |
|
|
|
* */ |
|
|
|
void MarshalTo(const ULID& ulid, char dst[26]) { |
|
|
|
inline void MarshalTo(const ULID& ulid, char dst[26]) { |
|
|
|
// 10 byte timestamp
|
|
|
|
dst[0] = Encoding[(ulid.data[0] & 224) >> 5]; |
|
|
|
dst[1] = Encoding[ulid.data[0] & 31]; |
|
|
@ -424,7 +437,7 @@ void MarshalTo(const ULID& ulid, char dst[26]) { |
|
|
|
/**
|
|
|
|
* Marshal will marshal a ULID to a std::string. |
|
|
|
* */ |
|
|
|
std::string Marshal(const ULID& ulid) { |
|
|
|
inline std::string Marshal(const ULID& ulid) { |
|
|
|
char data[27]; |
|
|
|
data[26] = '\0'; |
|
|
|
MarshalTo(ulid, data); |
|
|
@ -434,7 +447,7 @@ std::string Marshal(const ULID& ulid) { |
|
|
|
/**
|
|
|
|
* MarshalBinaryTo will Marshal a ULID to the passed byte array |
|
|
|
* */ |
|
|
|
void MarshalBinaryTo(const ULID& ulid, uint8_t dst[16]) { |
|
|
|
inline void MarshalBinaryTo(const ULID& ulid, uint8_t dst[16]) { |
|
|
|
// timestamp
|
|
|
|
dst[0] = ulid.data[0]; |
|
|
|
dst[1] = ulid.data[1]; |
|
|
@ -459,7 +472,7 @@ void MarshalBinaryTo(const ULID& ulid, uint8_t dst[16]) { |
|
|
|
/**
|
|
|
|
* MarshalBinary will Marshal a ULID to a byte vector. |
|
|
|
* */ |
|
|
|
std::vector<uint8_t> MarshalBinary(const ULID& ulid) { |
|
|
|
inline std::vector<uint8_t> MarshalBinary(const ULID& ulid) { |
|
|
|
std::vector<uint8_t> dst(16); |
|
|
|
MarshalBinaryTo(ulid, dst.data()); |
|
|
|
return dst; |
|
|
@ -471,7 +484,7 @@ std::vector<uint8_t> MarshalBinary(const ULID& ulid) { |
|
|
|
* 48-57 are digits. |
|
|
|
* 65-90 are capital alphabets. |
|
|
|
* */ |
|
|
|
const uint8_t dec[256] = { |
|
|
|
static const uint8_t dec[256] = { |
|
|
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
|
|
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
|
|
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
|
|
@ -522,7 +535,7 @@ const uint8_t dec[256] = { |
|
|
|
/**
|
|
|
|
* UnmarshalFrom will unmarshal a ULID from the passed character array. |
|
|
|
* */ |
|
|
|
void UnmarshalFrom(const char str[26], ULID& ulid) { |
|
|
|
inline void UnmarshalFrom(const char str[26], ULID& ulid) { |
|
|
|
// timestamp
|
|
|
|
ulid.data[0] = (dec[int(str[0])] << 5) | dec[int(str[1])]; |
|
|
|
ulid.data[1] = (dec[int(str[2])] << 3) | (dec[int(str[3])] >> 2); |
|
|
@ -547,7 +560,7 @@ void UnmarshalFrom(const char str[26], ULID& ulid) { |
|
|
|
/**
|
|
|
|
* Unmarshal will create a new ULID by unmarshaling the passed string. |
|
|
|
* */ |
|
|
|
ULID Unmarshal(const std::string& str) { |
|
|
|
inline ULID Unmarshal(const std::string& str) { |
|
|
|
ULID ulid; |
|
|
|
UnmarshalFrom(str.c_str(), ulid); |
|
|
|
return ulid; |
|
|
@ -556,7 +569,7 @@ ULID Unmarshal(const std::string& str) { |
|
|
|
/**
|
|
|
|
* UnmarshalBinaryFrom will unmarshal a ULID from the passed byte array. |
|
|
|
* */ |
|
|
|
void UnmarshalBinaryFrom(const uint8_t b[16], ULID& ulid) { |
|
|
|
inline void UnmarshalBinaryFrom(const uint8_t b[16], ULID& ulid) { |
|
|
|
// timestamp
|
|
|
|
ulid.data[0] = b[0]; |
|
|
|
ulid.data[1] = b[1]; |
|
|
@ -581,7 +594,7 @@ void UnmarshalBinaryFrom(const uint8_t b[16], ULID& ulid) { |
|
|
|
/**
|
|
|
|
* Unmarshal will create a new ULID by unmarshaling the passed byte vector. |
|
|
|
* */ |
|
|
|
ULID UnmarshalBinary(const std::vector<uint8_t>& b) { |
|
|
|
inline ULID UnmarshalBinary(const std::vector<uint8_t>& b) { |
|
|
|
ULID ulid; |
|
|
|
UnmarshalBinaryFrom(b.data(), ulid); |
|
|
|
return ulid; |
|
|
@ -594,7 +607,7 @@ ULID UnmarshalBinary(const std::vector<uint8_t>& b) { |
|
|
|
* 1 if ulid1 is Lexicographically after ulid2 |
|
|
|
* 0 if ulid1 is same as ulid2 |
|
|
|
* */ |
|
|
|
int CompareULIDs(const ULID& ulid1, const ULID& ulid2) { |
|
|
|
inline int CompareULIDs(const ULID& ulid1, const ULID& ulid2) { |
|
|
|
// for (int i = 0 ; i < 16 ; i++) {
|
|
|
|
// if (ulid1.data[i] != ulid2.data[i]) {
|
|
|
|
// return (ulid1.data[i] < ulid2.data[i]) * -2 + 1;
|
|
|
@ -673,7 +686,7 @@ int CompareULIDs(const ULID& ulid1, const ULID& ulid2) { |
|
|
|
/**
|
|
|
|
* Time will extract the timestamp used to generate a ULID |
|
|
|
* */ |
|
|
|
time_t Time(const ULID& ulid) { |
|
|
|
inline time_t Time(const ULID& ulid) { |
|
|
|
time_t ans = 0; |
|
|
|
|
|
|
|
ans |= ulid.data[0]; |
|
|
@ -697,3 +710,5 @@ time_t Time(const ULID& ulid) { |
|
|
|
} |
|
|
|
|
|
|
|
}; // namespace ulid
|
|
|
|
|
|
|
|
#endif // ULID_STRUCT_HH
|