jkoop/ulid-zig
A simple ULID helper for Zig
This is a simple package for Zig projects that implements ULID as an enum with convenience methods. Includes methods for std.json
to en-/decode as a string, e.g. "01JT9B27HTN19SEWVMWXRDRVC4"
.
const Ulid = @import("ulid").Ulid;
// generate a new ulid
var ulid = Ulid.generateRandom();
// for monotonic mode, guaranteeing ulid to be "higher" than all previous (see api doc)
var ulid = try Ulid.generateMonotonic();
// parse a ulid from a string
var ulid = try Ulid.fromString("01JT9B27HTN19SEWVMWXRDRVC4");
// encode a ulid to a string
var buffer: [26]u8 = @splat(0);
ulid.toString(&buffer); // overwrites the bytes of the buffer with the string and returns the string
// compare two ulids
if (ulid1 == ulid2) {
// this 👆 is allowed; they're enums after all
}
The normal procedure:
zig fetch --save https://github.com/jkoop/ulid-zig/archive/COMMIT.zip
// add to your build.zig, in your Module's .imports
.{
.name = "ulid",
.module = b.dependency("ulid", .{ .target = target }).module("ulid"),
},