SIMPLE_UUID

API Reference

Class Overview

SIMPLE_UUID generates RFC 9562 compliant UUIDs. It supports UUID v4 (random) and UUID v7 (timestamp-based), with full Design by Contract support.

Creation

create gen.make

Initialize the UUID generator with a time-based random seed.

Contracts

ensure: random_created: random /= Void

UUID v4 (Random)

UUID v4 uses 122 random bits, providing maximum uniqueness without timestamp information.

new_v4: ARRAY [NATURAL_8]

uuid_bytes := gen.new_v4

Generate a new random UUID v4 as a 16-byte array.

Contracts

ensure: correct_length: Result.count = 16
ensure: version_4: (Result [7] & 0xF0) = 0x40
ensure: variant_rfc4122: (Result [9] & 0xC0) = 0x80

new_v4_string: STRING

uuid := gen.new_v4_string

Generate a new random UUID v4 as a hyphenated string (36 characters).

Example: 550e8400-e29b-41d4-a716-446655440000

Contracts

ensure: valid_format: is_valid_uuid (Result)
ensure: correct_length: Result.count = 36

new_v4_compact: STRING

uuid := gen.new_v4_compact

Generate a new random UUID v4 without hyphens (32 characters).

Example: 550e8400e29b41d4a716446655440000

Contracts

ensure: no_hyphens: not Result.has ('-')
ensure: correct_length: Result.count = 32

UUID v7 (Timestamp)

UUID v7 embeds a Unix millisecond timestamp in the first 48 bits, making UUIDs sortable by creation time.

new_v7: ARRAY [NATURAL_8]

uuid_bytes := gen.new_v7

Generate a new timestamp-based UUID v7 as a 16-byte array.

Contracts

ensure: correct_length: Result.count = 16
ensure: version_7: (Result [7] & 0xF0) = 0x70
ensure: variant_rfc4122: (Result [9] & 0xC0) = 0x80

new_v7_string: STRING

uuid := gen.new_v7_string

Generate a new timestamp-based UUID v7 as a hyphenated string.

Example: 0190a926-3b00-7abc-8def-123456789abc

Contracts

ensure: valid_format: is_valid_uuid (Result)
ensure: correct_length: Result.count = 36

new_v7_compact: STRING

uuid := gen.new_v7_compact

Generate a new timestamp-based UUID v7 without hyphens.

Contracts

ensure: no_hyphens: not Result.has ('-')
ensure: correct_length: Result.count = 32

Formatting Features

to_string (a_uuid: ARRAY [NATURAL_8]): STRING

formatted := gen.to_string (uuid_bytes)

Convert UUID bytes to hyphenated string format.

Format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Contracts

require: valid_uuid: a_uuid.count = 16
ensure: correct_format: Result.count = 36
ensure: has_hyphens: Result.occurrences ('-') = 4

to_compact_string (a_uuid: ARRAY [NATURAL_8]): STRING

compact := gen.to_compact_string (uuid_bytes)

Convert UUID bytes to string without hyphens.

Contracts

require: valid_uuid: a_uuid.count = 16
ensure: correct_length: Result.count = 32
ensure: no_hyphens: not Result.has ('-')

Parsing Features

from_string (a_string: STRING): ARRAY [NATURAL_8]

uuid_bytes := gen.from_string ("550e8400-e29b-41d4-a716-446655440000")

Parse a UUID string (with or without hyphens) to a 16-byte array.

Contracts

require: valid_input: is_valid_uuid (a_string) or is_valid_uuid_compact (a_string)
ensure: correct_length: Result.count = 16

Validation Features

is_valid_uuid (a_string: STRING): BOOLEAN

if gen.is_valid_uuid (input) then ...

Check if a string is a valid hyphenated UUID (36 characters with hyphens at positions 9, 14, 19, 24).

Contracts

require: string_not_void: a_string /= Void

is_valid_uuid_compact (a_string: STRING): BOOLEAN

if gen.is_valid_uuid_compact (input) then ...

Check if a string is a valid compact UUID (32 hexadecimal characters, no hyphens).

Contracts

require: string_not_void: a_string /= Void

Comparison Features

is_nil (a_uuid: ARRAY [NATURAL_8]): BOOLEAN

if gen.is_nil (uuid_bytes) then ...

Check if the UUID is the nil UUID (all zeros).

Contracts

require: valid_uuid: a_uuid.count = 16

nil_uuid: ARRAY [NATURAL_8]

null_uuid := gen.nil_uuid

Return the nil UUID (all zeros) as a byte array.

Contracts

ensure: correct_length: Result.count = 16
ensure: is_nil: is_nil (Result)

nil_uuid_string: STRING

Nil_uuid_string = "00000000-0000-0000-0000-000000000000"

The nil UUID as a constant string.

Version Detection

version (a_uuid: ARRAY [NATURAL_8]): INTEGER

v := gen.version (uuid_bytes)

Get the version number (0-15) from UUID bytes. Common values: 4 (random), 7 (timestamp).

Contracts

require: valid_uuid: a_uuid.count = 16
ensure: valid_version: Result >= 0 and Result <= 15

version_from_string (a_string: STRING): INTEGER

v := gen.version_from_string ("550e8400-e29b-41d4-a716-446655440000")

Get the version number from a UUID string.

Contracts

require: valid_uuid: is_valid_uuid (a_string) or is_valid_uuid_compact (a_string)
ensure: valid_version: Result >= 0 and Result <= 15

Constants

Constant Value Description
Hex_chars 0123456789abcdef Hexadecimal characters for formatting
Nil_uuid_string 00000000-0000-0000-0000-000000000000 The nil UUID as string

Class Invariants

invariant: random_exists: random /= Void