core/encoding/uuid
uuid
Types
3Identifier
Identifier :: [16]u8SourceA RFC 4122 Universally Unique Identifier
Read_Error
Read_Error :: enum int {
None = 0,
Invalid_Length = 1,
Invalid_Hexadecimal = 2,
Invalid_Separator = 3,
}SourceVariant_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
}SourceConstants
8EXPECTED_LENGTH
EXPECTED_LENGTH :: 8 + 4 + 4 + 4 + 12 + 4SourceHNS_INTERVALS_BETWEEN_GREG_AND_UNIX
HNS_INTERVALS_BETWEEN_GREG_AND_UNIX :: 141427 * 24 * 60 * 60 * 1000 * 1000 * 10SourceThe number of 100-nanosecond intervals between 1582-10-15 and 1970-01-01.
VARIANT_BYTE_INDEX
VARIANT_BYTE_INDEX :: 8SourceVERSION_7_COUNTER_MASK
VERSION_7_COUNTER_MASK :: 0x00000000_00000fff_00000000_00000000SourceVERSION_7_COUNTER_SHIFT
VERSION_7_COUNTER_SHIFT :: 64SourceVERSION_7_TIME_MASK
VERSION_7_TIME_MASK :: 0xffffffff_ffff0000_00000000_00000000SourceVERSION_7_TIME_SHIFT
VERSION_7_TIME_SHIFT :: 80SourceVERSION_BYTE_INDEX
VERSION_BYTE_INDEX :: 6SourceVariables
4Namespace_DNS
Namespace_DNS :: Identifier = Identifier {
0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1,
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8,
}SourceName string is a fully-qualified domain name.
Namespace_OID
Namespace_OID :: Identifier = Identifier {
0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1,
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8,
}SourceName string is an ISO OID.
Namespace_URL
Namespace_URL :: Identifier = Identifier {
0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1,
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8,
}SourceName string is a URL.
Namespace_X500
Namespace_X500 :: Identifier = Identifier {
0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1,
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8,
}SourceName string is an X.500 DN (in DER or a text output format).
Procedures
26clock_seq
clock_seq :: proc(id: Identifier) -> (clock_seq: u16)SourceGet the clock sequence of a version 1 or version 6 UUID.
Inputs:
- id: The identifier.
Returns:
- clock_seq: The 14-bit clock sequence field.
counter_v7
counter_v7 :: proc(id: Identifier) -> (counter: u16)SourceGet 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.
Inputs:
- id: The identifier.
Returns:
- counter: The 12-bit counter value.
generate_v1
generate_v1 :: proc(clock_seq: u16, node: Maybe([6]u8), timestamp: Maybe(time.Time)) -> (result: Identifier)SourceGenerate a version 1 UUID.
Inputs:
- 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:timepackage, ornilto use the current time.
Returns:
- result: The generated UUID.
generate_v4
generate_v4 :: proc() -> (result: Identifier)SourceGenerate a version 4 UUID.
This UUID will be pseudorandom, save for 6 pre-determined version and variant bits.
Returns:
- result: The generated UUID.
generate_v6
generate_v6 :: proc(clock_seq: Maybe(u16), node: Maybe([6]u8), timestamp: Maybe(time.Time)) -> (result: Identifier)SourceGenerate a version 6 UUID.
Inputs:
- 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:timepackage, ornilto use the current time.
Returns:
- result: The generated UUID.
generate_v7_basic
generate_v7_basic :: proc(timestamp: Maybe(time.Time)) -> (result: Identifier)SourceGenerate 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.
Inputs:
- timestamp: A timestamp from the
core:timepackage, ornilto use the current time.
Returns:
- result: The generated UUID.
generate_v7_with_counter
generate_v7_with_counter :: proc(counter: u16, timestamp: Maybe(time.Time)) -> (result: Identifier)SourceGenerate 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)
}Inputs:
- counter: A 12-bit value which should be incremented each time a UUID is generated in a batch.
- timestamp: A timestamp from the
core:timepackage, ornilto use the current time.
Returns:
- result: The generated UUID.
generate_v8_hash_bytes
generate_v8_hash_bytes :: proc(namespace: Identifier, name: []u8, algorithm: hash.Algorithm) -> (result: Identifier)SourceGenerate 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.
Inputs:
- namespace: An
Identifierthat 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.
Returns:
- 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-74a3960c5703generate_v8_hash_string
generate_v8_hash_string :: proc(namespace: Identifier, name: string, algorithm: hash.Algorithm) -> (result: Identifier)SourceGenerate 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.
Inputs:
- namespace: An
Identifierthat 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.
Returns:
- 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-74a3960c5703node
node :: proc(id: Identifier) -> (node: [6]u8)SourceGet the node of a version 1 or version 6 UUID.
Inputs:
- id: The identifier.
Returns:
- node: The 48-bit spatially unique identifier.
raw_time_v1
raw_time_v1 :: proc(id: Identifier) -> (timestamp: u64)SourceGet the raw timestamp of a version 1 UUID.
Inputs:
- id: The identifier.
Returns:
- timestamp: The timestamp, in 100-nanosecond intervals since 1582-10-15.
raw_time_v6
raw_time_v6 :: proc(id: Identifier) -> (timestamp: u64)SourceGet the raw timestamp of a version 6 UUID.
Inputs:
- id: The identifier.
Returns:
- timestamp: The timestamp, in 100-nanosecond intervals since 1582-10-15.
raw_time_v7
raw_time_v7 :: proc(id: Identifier) -> (timestamp: u64)SourceGet the raw timestamp of a version 7 UUID.
Inputs:
- id: The identifier.
Returns:
- timestamp: The timestamp, in milliseconds since the UNIX epoch.
read
read :: proc(str: string) -> (id: Identifier, error: Read_Error)SourceConvert a string to a UUID.
Inputs:
- str: A string in the 8-4-4-4-12 format.
Returns:
- id: The converted identifier, or
nilif there is an error. - error: A description of the error, or
nilif successful.
stamp_v8_array
stamp_v8_array :: proc(array: [16]u8) -> (result: Identifier)SourceStamp 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.
Inputs:
- array: An array of 16 bytes.
Returns:
- result: A valid version 8 UUID.
stamp_v8_int
stamp_v8_int :: proc(integer: u128) -> (result: Identifier)SourceStamp 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.
Inputs:
- integer: Any integer type.
Returns:
- result: A valid version 8 UUID.
stamp_v8_slice
stamp_v8_slice :: proc(slice: []u8) -> (result: Identifier)SourceStamp 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.
Inputs:
- slice: A slice of bytes.
Returns:
- result: A valid version 8 UUID.
time_v1
time_v1 :: proc(id: Identifier) -> (timestamp: time.Time)SourceGet the timestamp of a version 1 UUID.
Inputs:
- id: The identifier.
Returns:
- timestamp: The timestamp of the UUID.
time_v6
time_v6 :: proc(id: Identifier) -> (timestamp: time.Time)SourceGet the timestamp of a version 6 UUID.
Inputs:
- id: The identifier.
Returns:
- timestamp: The timestamp, in 100-nanosecond intervals since 1582-10-15.
time_v7
time_v7 :: proc(id: Identifier) -> (timestamp: time.Time)SourceGet the timestamp of a version 7 UUID.
Inputs:
- id: The identifier.
Returns:
- 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)SourceConvert a UUID to a string in the 8-4-4-4-12 format.
Allocates Using Provided Allocator
Inputs:
- id: The identifier to convert.
- allocator: (default: context.allocator)
- loc: The caller location for debugging purposes (default: #caller_location)
Returns:
- str: The allocated and converted string.
- error: An optional allocator error if one occured,
nilotherwise.
to_string_buffer
to_string_buffer :: proc(id: Identifier, buffer: []u8, loc = #caller_location) -> (str: string)SourceConvert a UUID to a string in the 8-4-4-4-12 format.
Inputs:
- 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)
Returns:
- str: The converted string which will be stored in
buffer.
unsafe_write
unsafe_write :: proc(w: io.Writer, id: Identifier)SourceWrite a UUID in the 8-4-4-4-12 format.
This procedure performs no error checking on the underlying stream.
Inputs:
- w: A writable stream.
- id: The identifier to convert.
variant
variant :: proc(id: Identifier) -> (variant: Variant_Type)SourceGet the variant of a UUID.
Inputs:
- id: The identifier.
Returns:
- variant: The variant type.
version
version :: proc(id: Identifier) -> (number: int)SourceGet the version of a UUID.
Inputs:
- id: The identifier.
Returns:
- number: The version number.
write
write :: proc(w: io.Writer, id: Identifier) -> (error: io.Error)SourceWrite 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.
Inputs:
- w: A writable stream.
- id: The identifier to convert.
Returns:
- error: An
ioerror, if one occurred, otherwisenil.
Procedure Groups
4generate_v7
generate_v7 :: proc{generate_v7_basic, generate_v7_with_counter}Sourcegenerate_v8_hash
generate_v8_hash :: proc{generate_v8_hash_bytes, generate_v8_hash_string}Sourcestamp_v8
stamp_v8 :: proc{stamp_v8_int, stamp_v8_array, stamp_v8_slice}Sourceto_string
to_string :: proc{to_string_allocated, to_string_buffer}Source