Esc

Uuid

moduleextensionSource
  • macOS: Everything works on macOS
  • Linux: Everything works on Linux
  • Windows: Everything works on Windows
  • Wasm: Everything works on Wasm
·since 0.36.1

#import "Extensions/Uuid";

UUIDs as defined by RFC 9562: parsing, formatting, comparing and generating versions 3, 4, 5 and 7. A Uuid is 16 bytes with no owned memory, stored in network byte order.

Example↗
#import "Extensions/Uuid";

id, error := parse_uuid("123e4567-e89b-12d3-a456-426614174000");
assert(error.kind == .NONE && uuid_version(id) == 1);
assert(format_uuid(id) == "123e4567-e89b-12d3-a456-426614174000");

Procedures#

parse_uuidsince 0.36.1Source
(text: string) -> (Uuid, Uuid_Error)

Reads canonical hex, urn:uuid:hex, or {hex}; e.g. id, err := parse_uuid(text). Fixed maximum input size is 45 bytes; parsing never allocates.

format_uuidsince 0.36.1Source
(value: Uuid, allocator := context.allocator) -> string

Returns owned canonical text; e.g. text := format_uuid(id); defer free(text).

uuid_versionsince 0.36.1Source
(value: Uuid) -> u8

Reads the version nibble even for reserved variants; e.g. uuid_version(UUID_NIL) == 0.

uuid_comparesince 0.36.1Source
(a: Uuid, b: Uuid) -> int

Network-byte lexicographic order (-1/0/1); e.g. uuid_compare(UUID_NIL, UUID_MAX) == -1.

uuid_equalsince 0.36.1Source
(a: Uuid, b: Uuid) -> bool

Value equality; e.g. Map.Map(Uuid, int, uuid_hash, uuid_equal).

uuid_hashsince 0.36.1Source
(value: Uuid) -> u32

Hashes all 16 bytes; e.g. Map.Map(Uuid, int, uuid_hash, uuid_equal).

uuid_v7since 0.36.1Source
() -> (Uuid, Uuid_Error)

Process-monotonic v7, RFC method 1; e.g. id, err := uuid_v7(). A 12-bit counter follows the timestamp; exhaustion returns an error without waiting.

Types#

Uuidstructsince 0.36.1Source

A 128-bit universally unique identifier.

Uuid :: struct {
    bytes: [16] u8;
}
Uuid_Error_Kindenumsince 0.36.1Source

Why parsing or generating a UUID failed.

Uuid_Error_Kind :: enum u8 {
    NONE;
    LENGTH;
    SYNTAX;
    RANDOM;
    CLOCK;
    EXHAUSTED;
}
Uuid_Errorstructsince 0.36.1Source

Messages are static; offsets are zero-based bytes. No error needs freeing.

Uuid_Error :: struct {
    kind: Uuid_Error_Kind;
    message: string;
    offset: int;
}
Uuid_Variantenumsince 0.36.1Source

The layout family a UUID belongs to, from its top bits. RFC 9562 UUIDs are .RFC.

Uuid_Variant :: enum u8 {
    NCS;
    RFC;
    MICROSOFT;
    FUTURE;
}

Constants#

UUID_MAXsince 0.36.1Source
:: Uuid.{ .[255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255] }

The all-ones UUID.