Overview / Foundation

uid

128-bit identifiers as values: generate, parse, compare, sort and hash.

Always compiled: part of the core build.Include #include <ethereal/cpp/uid/uid_inc.hpp>C layer

uid_inc.hpp

#include <ethereal/cpp/uid/uid_inc.hpp>

Aggregate entry point of the UID module.

128 bit identifiers as a type, not as text: they generate, parse, compare, sort, hash and format.

Ethereal::Random produces the text form of a UUIDv4 and of other identifiers, and is still the right place when only the string is wanted. This module is the value layer that was missing on top of it.

Which one to use:

  • Ethereal::UID::CUuid : when the identifier must reveal nothing, including the moment it was created.
  • Ethereal::UID::CUlid : when sorting by creation time is worth more than hiding that time, as in database keys and log records.

Including this header brings the whole module. The individual headers can still be used alone, for whoever wants to pay only for what is used.

ulid.hpp

#include <ethereal/cpp/uid/ulid.hpp>

ULID (Universally Unique Lexicographically Sortable Identifier).

class

CUlid

Ethereal::UID#

128 bit identifier that sorts by creation time.

class CUlid final

48 bits of instant, in milliseconds since 1970-01-01 UTC, followed by 80 bits from the cryptographic generator. The text form uses Crockford's base 32, 26 characters, and the alphabet was chosen so that the alphabetical order of the text is the chronological order of creation.

That is what a CUuid does not do. A UUIDv4 is noise: as a primary key it spreads insertions over the whole index tree, and a "last N" listing needs a separate date column. A ULID always inserts at the end and carries the instant with it.

On the other hand it reveals the creation instant, which is information that leaks. Where that matters, prefer CUuid.

Two ULIDs created in the same millisecond sort by their 80 random bits, that is, arbitrarily. This implementation is not monotonic within the same millisecond.
const

ByteSize

#
static constexpr std::size_t ByteSize = 16;

Size of the binary form, in bytes.

const

TextSize

#
static constexpr std::size_t TextSize = 26;

Size of the text form, in characters, without the null terminator.

constructor

CUlid

#
CUlid() noexcept;

Builds the nil ULID, with the 16 bytes at zero.

constructor

CUlid

#
explicit CUlid( std::span< const std::uint8_t, ByteSize > Bytes ) noexcept;

Builds from 16 ready bytes.

function

Generate

#
static CUlid Generate();

Generates a ULID with the current instant.

function

GenerateAt

#
static CUlid GenerateAt( const ETL_UINT64 uTimestampMs );

Generates a ULID with a given instant.

Exists for tests and to rebuild identifiers from old records. In normal use prefer Generate.

uTimestampMs
Milliseconds since 1970-01-01 UTC. Only the low 48 bits are used, the rest is dropped.
The generated identifier.
function

Parse

#
static bool Parse( std::string_view Text, CUlid & OutUlid ) noexcept;

Parses the text form in Crockford's base 32.

Accepts both cases. Also accepts the confusions foreseen by Crockford's alphabet: 'I' and 'L' mean 1, and 'O' means 0.

Refuses text whose size differs from TextSize, characters outside the alphabet, and a value whose first character is above 7, which would overflow 128 bits. On failure OutUlid is left nil.

Text
Text to parse.
OutUlid
Receives the identifier.
true on success.
function

ToString

#
std::string ToString() const;

Returns the text form, 26 characters in Crockford's base 32, upper case.

function

Bytes

#
std::span< const std::uint8_t, ByteSize > Bytes() const noexcept;

Access to the 16 bytes, in network order.

function

TimestampMs

#
ETL_UINT64 TimestampMs() const noexcept;

Creation instant, in milliseconds since 1970-01-01 UTC.

function

IsNil

#
bool IsNil() const noexcept;

Tells whether the identifier is nil, with the 16 bytes at zero.

operator

operator==

#
bool operator==( const CUlid & other ) const noexcept;

Byte equality.

operator

operator!=

#
bool operator!=( const CUlid & other ) const noexcept;
operator

operator<

#
bool operator<( const CUlid & other ) const noexcept;

Total order by the bytes, which is also the chronological order.

The 48 bits of instant take the first bytes, in network order, so comparing byte by byte compares the time first. It is the same order that comparing the text gives.

struct

std

#
C++
template<>
struct std::hash< Ethereal::UID::CUlid >
{
    std::size_t operator()( const Ethereal::UID::CUlid & Ulid ) const noexcept
    {
        const std::span< const std::uint8_t, Ethereal::UID::CUlid::ByteSize > Bytes = Ulid.Bytes();

        /// Here the hash must really mix, unlike the UUID one: the first bytes of a ULID
        /// are the instant, and identifiers created together share a prefix. Folding the
        /// bytes would put them all in the same bucket.
        /// The sum is always 64 bit. A 32-bit 'size_t' would truncate the FNV constants and
        /// mix worse, so the result is folded at the end instead.
        std::uint64_t uHash = 1469598103934665603ull;        /// 64 bit FNV-1a offset basis.

        for( std::size_t i = 0; i < Bytes.size(); ++i )
        {
            uHash ^= static_cast< std::uint64_t >( Bytes[ i ] );
            uHash *= 1099511628211ull;                    /// 64 bit FNV-1a prime.
        }

        if constexpr( sizeof( std::size_t ) < sizeof( std::uint64_t ) )
            return static_cast< std::size_t >( uHash ^ ( uHash >> 32 ) );
        else
            return static_cast< std::size_t >( uHash );
    }
};

uuid.hpp

#include <ethereal/cpp/uid/uuid.hpp>

UUID (RFC 9562, formerly RFC 4122).

class

CUuid

Ethereal::UID#

128 bit universally unique identifier.

class CUuid final

A value, not a text. Ethereal::Random already produces the text form of a UUIDv4, but text does not compare without care for letter case, does not serve as a key without paying for a string on each lookup, takes 36 bytes where 16 are enough, and is not validated on input. This class is the missing layer: it generates, parses, compares, sorts, hashes and formats.

The 16 bytes are in network order, the same as the text: Bytes()[0] is the first hex pair of ToString. Writing the raw bytes and reading them on another machine gives the same identifier, whatever the native byte order of integers.

The class is trivially copyable and does not allocate.
const

ByteSize

#
static constexpr std::size_t ByteSize = 16;

Size of the binary form, in bytes.

const

TextSize

#
static constexpr std::size_t TextSize = 36;

Size of the canonical text form, in characters, without the null terminator. The format is 8-4-4-4-12, with hyphens.

constructor

CUuid

#
CUuid() noexcept;

Builds the nil UUID, with the 16 bytes at zero.

constructor

CUuid

#
explicit CUuid( std::span< const std::uint8_t, ByteSize > Bytes ) noexcept;

Builds from 16 ready bytes.

Bytes
Exactly ByteSize bytes, in network order.
function

Generate

#
static CUuid Generate();

Generates a random version 4 UUID.

The 128 bits come from the system cryptographic generator, through Ethereal::Random::SecureBytes, and then the version and variant fields are set as the standard requires.

The generated identifier.
function

Parse

#
static bool Parse( std::string_view Text, CUuid & OutUuid ) noexcept;

Parses the text form.

Accepts both cases and the canonical 8-4-4-4-12 format. Also accepts the wrappings seen in practice: braces, as in '{...}', and the form without hyphens, with 32 digits in a row.

Refuses everything else instead of guessing. On failure OutUuid is left nil.

Text
Text to parse.
OutUuid
Receives the identifier.
true on success.
function

ToString

#
std::string ToString( const bool bUpperCase = false ) const;

Returns the canonical text form, 8-4-4-4-12.

bUpperCase
When true uses 'A-F'. The default is 'a-f', as RFC 9562 says.
Text with TextSize characters.
function

Bytes

#
std::span< const std::uint8_t, ByteSize > Bytes() const noexcept;

Access to the 16 bytes, in network order.

function

IsNil

#
bool IsNil() const noexcept;

Tells whether the identifier is nil, with the 16 bytes at zero.

function

Version

#
ETL_UINT8 Version() const noexcept;

Version number, in the 4 high bits of byte 6.

4 for those made by Generate, 0 for the nil one.
function

IsRfcVariant

#
bool IsRfcVariant() const noexcept;

Tells whether the variant is the RFC one, with the high bits of byte 8 at '10'.

operator

operator==

#
bool operator==( const CUuid & other ) const noexcept;

Byte equality.

operator

operator!=

#
bool operator!=( const CUuid & other ) const noexcept;
operator

operator<

#
bool operator<( const CUuid & other ) const noexcept;

Total order by the bytes, from the first to the last.

Exists so the type can be a key in std::map and in ordered containers. It carries no time meaning: for creation order, see CUlid.

struct

std

#
C++
template<>
struct std::hash< Ethereal::UID::CUuid >
{
    std::size_t operator()( const Ethereal::UID::CUuid & Uuid ) const noexcept
    {
        /// The bytes of a UUIDv4 are already random: folding them into the size of size_t
        /// is enough, without a real hash function.
        const std::span< const std::uint8_t, Ethereal::UID::CUuid::ByteSize > Bytes = Uuid.Bytes();

        std::size_t uHash = 0;

        for( std::size_t i = 0; i < Bytes.size(); ++i )
            uHash = ( uHash << 8 ) ^ ( uHash >> ( sizeof( std::size_t ) * 8u - 8u ) ) ^ Bytes[ i ];

        return uHash;
    }
};