ulid
Classes | Typedefs | Functions | Variables
ulid Namespace Reference

Classes

struct  ULID
 

Typedefs

typedef __uint128_t ULID
 

Functions

void EncodeTime (time_t timestamp, ULID &ulid)
 
void EncodeTimeNow (ULID &ulid)
 
void EncodeTimeSystemClockNow (ULID &ulid)
 
void EncodeEntropy (const std::function< uint8_t()> &rng, ULID &ulid)
 
void EncodeEntropyRand (ULID &ulid)
 
std::uniform_int_distribution< uint8_t > Distribution_0_255 (0, 255)
 
void EncodeEntropyMt19937 (std::mt19937 &generator, ULID &ulid)
 
void Encode (time_t timestamp, const std::function< uint8_t()> &rng, ULID &ulid)
 
void EncodeNowRand (ULID &ulid)
 
ULID Create (time_t timestamp, const std::function< uint8_t()> &rng)
 
ULID CreateNowRand ()
 
void MarshalTo (const ULID &ulid, char dst[26])
 
std::string Marshal (const ULID &ulid)
 
void MarshalBinaryTo (const ULID &ulid, uint8_t dst[16])
 
std::vector< uint8_t > MarshalBinary (const ULID &ulid)
 
void UnmarshalFrom (const char str[26], ULID &ulid)
 
ULID Unmarshal (const std::string &str)
 
void UnmarshalBinaryFrom (const uint8_t b[16], ULID &ulid)
 
ULID UnmarshalBinary (const std::vector< uint8_t > &b)
 
int CompareULIDs (const ULID &ulid1, const ULID &ulid2)
 
time_t Time (const ULID &ulid)
 

Variables

const char Encoding [33] = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
 
const uint8_t dec [256]
 

Typedef Documentation

typedef __uint128_t ulid::ULID

ULID is a 16 byte Universally Unique Lexicographically Sortable Identifier

Function Documentation

int ulid::CompareULIDs ( const ULID ulid1,
const ULID ulid2 
)

CompareULIDs will compare two ULIDs. returns: -1 if ulid1 is Lexicographically before ulid2 1 if ulid1 is Lexicographically after ulid2 0 if ulid1 is same as ulid2

ULID ulid::Create ( time_t  timestamp,
const std::function< uint8_t()> &  rng 
)

Create will create a ULID with a timestamp and a generator.

ULID ulid::CreateNowRand ( )

CreateNowRand:EncodeNowRand = Create:Encode.

std::uniform_int_distribution< uint8_t > ulid::Distribution_0_255 ( ,
255   
)
void ulid::Encode ( time_t  timestamp,
const std::function< uint8_t()> &  rng,
ULID ulid 
)

Encode will create an encoded ULID with a timestamp and a generator.

void ulid::EncodeEntropy ( const std::function< uint8_t()> &  rng,
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 ulid::EncodeEntropyMt19937 ( std::mt19937 &  generator,
ULID ulid 
)

EncodeEntropyMt19937 will encode a ulid using std::mt19937

It also creates a std::uniform_int_distribution to generate values in [0, 255]

void ulid::EncodeEntropyRand ( ULID ulid)

EncodeEntropyRand will encode a ulid using std::rand

std::rand returns values in [0, RAND_MAX]

void ulid::EncodeNowRand ( ULID ulid)

EncodeNowRand = EncodeTimeNow + EncodeEntropyRand.

void ulid::EncodeTime ( time_t  timestamp,
ULID ulid 
)

EncodeTime will encode the first 6 bytes of a uint8_t array to the passed timestamp

void ulid::EncodeTimeNow ( ULID ulid)

EncodeTimeNow will encode a ULID using the time obtained using std::time(nullptr)

void ulid::EncodeTimeSystemClockNow ( ULID ulid)

EncodeTimeSystemClockNow will encode a ULID using the time obtained using std::chrono::system_clock::now() by taking the timestamp in milliseconds.

std::string ulid::Marshal ( const ULID ulid)

Marshal will marshal a ULID to a std::string.

std::vector< uint8_t > ulid::MarshalBinary ( const ULID ulid)

MarshalBinary will Marshal a ULID to a byte vector.

void ulid::MarshalBinaryTo ( const ULID ulid,
uint8_t  dst[16] 
)

MarshalBinaryTo will Marshal a ULID to the passed byte array

void ulid::MarshalTo ( const ULID ulid,
char  dst[26] 
)

MarshalTo will marshal a ULID to the passed character array.

Implementation taken directly from oklog/ulid (https://sourcegraph.com/github.com/oklog/ulid@0774f81f6e44af5ce5e91c8d7d76cf710e889ebb/-/blob/ulid.go#L162-190)

timestamp:
dst[0]: first 3 bits of data[0]
dst[1]: last 5 bits of data[0]
dst[2]: first 5 bits of data[1]
dst[3]: last 3 bits of data[1] + first 2 bits of data[2]
dst[4]: bits 3-7 of data[2]
dst[5]: last bit of data[2] + first 4 bits of data[3]
dst[6]: last 4 bits of data[3] + first bit of data[4]
dst[7]: bits 2-6 of data[4]
dst[8]: last 2 bits of data[4] + first 3 bits of data[5]
dst[9]: last 5 bits of data[5]

entropy: follows similarly, except now all components are set to 5 bits.

MarshalTo will marshal a ULID to the passed character array.

Implementation taken directly from oklog/ulid (https://sourcegraph.com/github.com/oklog/ulid@0774f81f6e44af5ce5e91c8d7d76cf710e889ebb/-/blob/ulid.go#L162-190)

timestamp: dst[0]: first 3 bits of data[0] dst[1]: last 5 bits of data[0] dst[2]: first 5 bits of data[1] dst[3]: last 3 bits of data[1] + first 2 bits of data[2] dst[4]: bits 3-7 of data[2] dst[5]: last bit of data[2] + first 4 bits of data[3] dst[6]: last 4 bits of data[3] + first bit of data[4] dst[7]: bits 2-6 of data[4] dst[8]: last 2 bits of data[4] + first 3 bits of data[5] dst[9]: last 5 bits of data[5]

entropy: follows similarly, except now all components are set to 5 bits.

time_t ulid::Time ( const ULID ulid)

Time will extract the timestamp used to generate a ULID

ULID ulid::Unmarshal ( const std::string &  str)

Unmarshal will create a new ULID by unmarshaling the passed string.

ULID ulid::UnmarshalBinary ( const std::vector< uint8_t > &  b)

Unmarshal will create a new ULID by unmarshaling the passed byte vector.

void ulid::UnmarshalBinaryFrom ( const uint8_t  b[16],
ULID ulid 
)

UnmarshalBinaryFrom will unmarshal a ULID from the passed byte array.

void ulid::UnmarshalFrom ( const char  str[26],
ULID ulid 
)

UnmarshalFrom will unmarshal a ULID from the passed character array.

Variable Documentation

const uint8_t ulid::dec

dec storesdecimal encodings for characters. 0xFF indicates invalid character. 48-57 are digits. 65-90 are capital alphabets.

const char ulid::Encoding = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"

Crockford's Base32