core/encoding/uuid

uuid

Types

3

Variant_Type

Variant_Type :: enum int { Unknown = 0, Reserved_Apollo_NCS = 1, // 0b0xx RFC_4122 = 2, // 0b10x Reserved_Microsoft_COM = 3, // 0b110 Reserved_Future = 4, // 0b111 }Source

Constants

8

Variables

4

Namespace_X500

Namespace_X500 :: Identifier = Identifier { 0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8, }Source

Name string is an X.500 DN (in DER or a text output format).

Procedures

26

clock_seq

clock_seq :: proc(id: Identifier) -> (clock_seq: u16)Source

Get the clock sequence of a version 1 or version 6 UUID.

  • id: The identifier.
  • clock_seq: The 14-bit clock sequence field.

counter_v7

counter_v7 :: proc(id: Identifier) -> (counter: u16)Source

Get the 12-bit counter value of a version 7 UUID.

The UUID must have been generated with a counter, otherwise this procedure will return random bits.

  • id: The identifier.
  • counter: The 12-bit counter value.

generate_v1

generate_v1 :: proc(clock_seq: u16, node: Maybe([6]u8), timestamp: Maybe(time.Time)) -> (result: Identifier)Source

Generate a version 1 UUID.

  • clock_seq: The clock sequence, a number which must be initialized to a random number once in the lifetime of a system.
  • node: An optional 48-bit spatially unique identifier, specified to be the IEEE 802 address of the system.

If one is not provided or available, 48 bits of random state will take its place.

  • timestamp: A timestamp from the core:time package, or nil to use the current time.
  • result: The generated UUID.

generate_v4

generate_v4 :: proc() -> (result: Identifier)Source

Generate a version 4 UUID.

This UUID will be pseudorandom, save for 6 pre-determined version and variant bits.

  • result: The generated UUID.

generate_v6

generate_v6 :: proc(clock_seq: Maybe(u16), node: Maybe([6]u8), timestamp: Maybe(time.Time)) -> (result: Identifier)Source

Generate a version 6 UUID.

  • clock_seq: The clock sequence from version 1, now made optional.

If unspecified, it will be replaced with random bits.

  • node: An optional 48-bit spatially unique identifier, specified to be the IEEE 802 address of the system.

If one is not provided or available, 48 bits of random state will take its place.

  • timestamp: A timestamp from the core:time package, or nil to use the current time.
  • result: The generated UUID.

generate_v7_basic

generate_v7_basic :: proc(timestamp: Maybe(time.Time)) -> (result: Identifier)Source

Generate a version 7 UUID.

This UUID will be pseudorandom, save for 6 pre-determined version and variant bits and a 48-bit timestamp.

It is designed with time-based sorting in mind, such as for database usage, as the highest bits are allocated from the timestamp of when it is created.

  • timestamp: A timestamp from the core:time package, or nil to use the current time.
  • result: The generated UUID.

generate_v7_with_counter

generate_v7_with_counter :: proc(counter: u16, timestamp: Maybe(time.Time)) -> (result: Identifier)Source

Generate a version 7 UUID that has an incremented counter.

This UUID will be pseudorandom, save for 6 pre-determined version and variant bits, a 48-bit timestamp, and 12 bits of counter state.

It is designed with time-based sorting in mind, such as for database usage, as the highest bits are allocated from the timestamp of when it is created.

This procedure is preferable if you are generating hundreds or thousands of UUIDs as a batch within the span of a millisecond. Do note that the counter only has 12 bits of state, thus counter cannot exceed the number 4,095.

Example:

import "core:uuid"

// Create a batch of UUIDs all at once.
batch: [dynamic]uuid.Identifier

for i: u16 = 0; i < 1000; i += 1 {
	my_uuid := uuid.generate_v7_counter(i)
	append(&batch, my_uuid)
}
  • counter: A 12-bit value which should be incremented each time a UUID is generated in a batch.
  • timestamp: A timestamp from the core:time package, or nil to use the current time.
  • result: The generated UUID.

generate_v8_hash_bytes

generate_v8_hash_bytes :: proc(namespace: Identifier, name: []u8, algorithm: hash.Algorithm) -> (result: Identifier)Source

Generate a version 8 UUID using a specific hashing algorithm.

This UUID is generated by hashing a name with a namespace.

Note that all version 8 UUIDs are for experimental or vendor-specific use cases, per the specification. This use case in particular is for offering a non-legacy alternative to UUID versions 3 and 5.

  • namespace: An Identifier that is used to represent the underlying namespace.

This can be any one of the Namespace_* values provided in this package.

  • name: The byte slice which will be hashed with the namespace.
  • algorithm: A hashing algorithm from core:crypto/hash.
  • result: The generated UUID.

Example:

import "core:crypto/hash"
import "core:encoding/uuid"
import "core:fmt"

generate_v8_hash_bytes_example :: proc() {
	my_uuid := uuid.generate_v8_hash(uuid.Namespace_DNS, "www.odin-lang.org", .SHA256)
	my_uuid_string := uuid.to_string(my_uuid, context.temp_allocator)
	fmt.println(my_uuid_string)
}

Output:

3730f688-4bff-8dce-9cbf-74a3960c5703

generate_v8_hash_string

generate_v8_hash_string :: proc(namespace: Identifier, name: string, algorithm: hash.Algorithm) -> (result: Identifier)Source

Generate a version 8 UUID using a specific hashing algorithm.

This UUID is generated by hashing a name with a namespace.

Note that all version 8 UUIDs are for experimental or vendor-specific use cases, per the specification. This use case in particular is for offering a non-legacy alternative to UUID versions 3 and 5.

  • namespace: An Identifier that is used to represent the underlying namespace.

This can be any one of the Namespace_* values provided in this package.

  • name: The string which will be hashed with the namespace.
  • algorithm: A hashing algorithm from core:crypto/hash.
  • result: The generated UUID.

Example:

import "core:crypto/hash"
import "core:encoding/uuid"
import "core:fmt"

generate_v8_hash_string_example :: proc() {
	my_uuid := uuid.generate_v8_hash(uuid.Namespace_DNS, "www.odin-lang.org", .SHA256)
	my_uuid_string := uuid.to_string(my_uuid, context.temp_allocator)
	fmt.println(my_uuid_string)
}

Output:

3730f688-4bff-8dce-9cbf-74a3960c5703

node

node :: proc(id: Identifier) -> (node: [6]u8)Source

Get the node of a version 1 or version 6 UUID.

  • id: The identifier.
  • node: The 48-bit spatially unique identifier.

raw_time_v1

raw_time_v1 :: proc(id: Identifier) -> (timestamp: u64)Source

Get the raw timestamp of a version 1 UUID.

  • id: The identifier.
  • timestamp: The timestamp, in 100-nanosecond intervals since 1582-10-15.

raw_time_v6

raw_time_v6 :: proc(id: Identifier) -> (timestamp: u64)Source

Get the raw timestamp of a version 6 UUID.

  • id: The identifier.
  • timestamp: The timestamp, in 100-nanosecond intervals since 1582-10-15.

raw_time_v7

raw_time_v7 :: proc(id: Identifier) -> (timestamp: u64)Source

Get the raw timestamp of a version 7 UUID.

  • id: The identifier.
  • timestamp: The timestamp, in milliseconds since the UNIX epoch.

read

read :: proc(str: string) -> (id: Identifier, error: Read_Error)Source

Convert a string to a UUID.

  • str: A string in the 8-4-4-4-12 format.
  • id: The converted identifier, or nil if there is an error.
  • error: A description of the error, or nil if successful.

stamp_v8_array

stamp_v8_array :: proc(array: [16]u8) -> (result: Identifier)Source

Stamp an array of 16 bytes as being a valid version 8 UUID.

Per the specification, all version 8 UUIDs are either for experimental or vendor-specific purposes. This procedure allows for converting arbitrary data into custom UUIDs.

  • array: An array of 16 bytes.
  • result: A valid version 8 UUID.

stamp_v8_int

stamp_v8_int :: proc(integer: u128) -> (result: Identifier)Source

Stamp a 128-bit integer as being a valid version 8 UUID.

Per the specification, all version 8 UUIDs are either for experimental or vendor-specific purposes. This procedure allows for converting arbitrary data into custom UUIDs.

  • integer: Any integer type.
  • result: A valid version 8 UUID.

stamp_v8_slice

stamp_v8_slice :: proc(slice: []u8) -> (result: Identifier)Source

Stamp a slice of bytes as being a valid version 8 UUID.

If the slice is less than 16 bytes long, the data available will be used. If it is longer than 16 bytes, only the first 16 will be used.

This procedure does not modify the underlying slice.

Per the specification, all version 8 UUIDs are either for experimental or vendor-specific purposes. This procedure allows for converting arbitrary data into custom UUIDs.

  • slice: A slice of bytes.
  • result: A valid version 8 UUID.

time_v1

time_v1 :: proc(id: Identifier) -> (timestamp: time.Time)Source

Get the timestamp of a version 1 UUID.

  • id: The identifier.
  • timestamp: The timestamp of the UUID.

time_v6

time_v6 :: proc(id: Identifier) -> (timestamp: time.Time)Source

Get the timestamp of a version 6 UUID.

  • id: The identifier.
  • timestamp: The timestamp, in 100-nanosecond intervals since 1582-10-15.

time_v7

time_v7 :: proc(id: Identifier) -> (timestamp: time.Time)Source

Get the timestamp of a version 7 UUID.

  • id: The identifier.
  • timestamp: The timestamp, in milliseconds since the UNIX epoch.

to_string_allocated

to_string_allocated :: proc(id: Identifier, allocator: mem.Allocator = context.allocator, loc = #caller_location) -> (str: string, error: runtime.Allocator_Error)Source

Convert a UUID to a string in the 8-4-4-4-12 format.

Allocates Using Provided Allocator

  • id: The identifier to convert.
  • allocator: (default: context.allocator)
  • loc: The caller location for debugging purposes (default: #caller_location)
  • str: The allocated and converted string.
  • error: An optional allocator error if one occured, nil otherwise.

to_string_buffer

to_string_buffer :: proc(id: Identifier, buffer: []u8, loc = #caller_location) -> (str: string)Source

Convert a UUID to a string in the 8-4-4-4-12 format.

  • id: The identifier to convert.
  • buffer: A byte buffer to store the result. Must be at least 36 bytes large.
  • loc: The caller location for debugging purposes (default: #caller_location)
  • str: The converted string which will be stored in buffer.

unsafe_write

unsafe_write :: proc(w: io.Writer, id: Identifier)Source

Write a UUID in the 8-4-4-4-12 format.

This procedure performs no error checking on the underlying stream.

  • w: A writable stream.
  • id: The identifier to convert.

write

write :: proc(w: io.Writer, id: Identifier) -> (error: io.Error)Source

Write a UUID in the 8-4-4-4-12 format.

This procedure performs error checking with every byte written.

If you can guarantee beforehand that your stream has enough space to hold the UUID (36 bytes), then it is better to use unsafe_write instead as that will be faster.

  • w: A writable stream.
  • id: The identifier to convert.
  • error: An io error, if one occurred, otherwise nil.

Procedure Groups

4

Reference search

Find anything

Documentation preferences

Settings

System theme variants

Used only while Theme is set to System.