hendriknielaender/ulid.zig
Zig Universally Unique Lexicographically Sortable Identifier
A Universally Unique Lexicographically Sortable Identifier (ULID) implementation for Zig, providing a robust and efficient way to generate unique identifiers that are both time-based and random.
UUID can be suboptimal for many uses-cases because:
For detailed information on the ULID specification, refer to the ULID Specification.
This library provides an implementation of ULID (Universally Unique Lexicographically Sortable Identifier) generation and decoding in Zig.
You can generate a new ULID as a 26-character Crockford's Base32 string:
const ulid = try Ulid.generate();
std.debug.print("Generated ULID: {s}\n", .{ulid});
This will output a unique, lexicographically sortable string.
To decode a ULID string into its components (timestamp and randomness):
const ulid_str = "01AN4Z07BY79KA1307SR9X4MV3";
var decoded_ulid: Ulid = undefined;
try Ulid.decode(ulid_str[0..], &decoded_ulid);
std.debug.print("Decoded ULID: timestamp={d}, randomness={d}\n", .{decoded_ulid.timestamp, decoded_ulid.randomness});
To generate ULIDs with guaranteed monotonicity within the same millisecond, use the UlidGenerator
:
var generator = Ulid.monotonic_factory();
const ulid = try generator.generate(null); // Passing `null` uses the current timestamp.
std.debug.print("Generated monotonic ULID: {s}\n", .{ulid});
This will ensure that if multiple ULIDs are generated in the same millisecond, their randomness will be incremented to preserve order.
This library defines several error types for ULID encoding/decoding, such as:
invalid_length
– when the provided ULID string is not 26 characters long.invalid_character
– when the ULID string contains invalid Base32 characters.overflow
– when the timestamp exceeds the maximum allowable value (48 bits).