Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
Page wiki
View or edit the community-maintained wiki page associated with this page.
std.uuid
Category | Functions |
---|---|
Parsing UUIDs | parseUUID UUID(string) UUIDParsingException uuidRegex |
Generating UUIDs | sha1UUID randomUUID md5UUID |
Using UUIDs | UUID.uuidVersion UUID.variant UUID.toString UUID.data UUID.swap UUID.opEquals UUID.opCmp UUID.toHash |
UUID namespaces | dnsNamespace urlNamespace oidNamespace x500Namespace |
UUID[] ids; ids ~= randomUUID(); ids ~= md5UUID("test.name.123"); ids ~= sha1UUID("test.name.123"); foreach(entry; ids) { assert(entry.variant == UUID.Variant.rfc4122); } assert(ids[0].uuidVersion == UUID.Version.randomNumberBased); assert(ids[1].toString() == "22390768-cced-325f-8f0f-cfeaa19d0ccd"); assert(ids[1].data == [34, 57, 7, 104, 204, 237, 50, 95, 143, 15, 207, 234, 161, 157, 12, 205]); UUID id; assert(id.empty);Standards:
RFC 4122 See Also:
http://en.wikipedia.org/wiki/Universally_unique_identifier License:
Boost License 1.0 Authors:
Johannes Pfau Source:
std/uuid.d
- struct UUID;
- pure @safe UUID md5UUID(const(char[]) name, const UUID namespace = UUID.init);
pure @safe UUID md5UUID(const(ubyte[]) data, const UUID namespace = UUID.init); - This function generates a name based (Version 3) UUID from a namespace UUID and a name.
If no namespace UUID was passed, the empty UUID UUID.init is used.
Note:
The default namespaces (dnsNamespace, ...) defined by this module should be used when appropriate. RFC 4122 recommends to use Version 5 UUIDs (SHA-1) instead of Version 3 UUIDs (MD5) for new applications. CTFE:
CTFE is not supported. Examples://Use default UUID.init namespace auto simpleID = md5UUID("test.uuid.any.string"); //use a name-based id as namespace auto namespace = md5UUID("my.app"); auto id = md5UUID("some-description", namespace);
Note:
RFC 4122 isn't very clear on how UUIDs should be generated from names. It is possible that different implementations return different UUIDs for the same input, so be warned. The implementation for UTF-8 strings and byte arrays used by std.uuid is compatible with Boost's implementation. std.uuid guarantees that the same input to this function will generate the same output at any time, on any system (this especially means endianness doesn't matter). Note:
This function does not provide overloads for wstring and dstring, as there's no clear answer on how that should be implemented. It could be argued, that string, wstring and dstring input should have the same output, but that wouldn't be compatible with Boost, which generates different output for strings and wstrings. It's always possible to pass wstrings and dstrings by using the ubyte[] function overload (but be aware of endianness issues!). - pure @safe UUID sha1UUID(in char[] name, const UUID namespace = UUID.init);
pure @safe UUID sha1UUID(in ubyte[] data, const UUID namespace = UUID.init); - This function generates a name based (Version 5) UUID from a namespace
UUID and a name.
If no namespace UUID was passed, the empty UUID UUID.init is used.
Note:
The default namespaces (dnsNamespace, ...) defined by this module should be used when appropriate. CTFE:
CTFE is not supported. Examples://Use default UUID.init namespace auto simpleID = sha1UUID("test.uuid.any.string"); //use a name-based id as namespace auto namespace = sha1UUID("my.app"); auto id = sha1UUID("some-description", namespace);
Note:
RFC 4122 isn't very clear on how UUIDs should be generated from names. It is possible that different implementations return different UUIDs for the same input, so be warned. The implementation for UTF-8 strings and byte arrays used by std.uuid is compatible with Boost's implementation. std.uuid guarantees that the same input to this function will generate the same output at any time, on any system (this especially means endianness doesn't matter). Note:
This function does not provide overloads for wstring and dstring, as there's no clear answer on how that should be implemented. It could be argued, that string, wstring and dstring input should have the same output, but that wouldn't be compatible with Boost, which generates different output for strings and wstrings. It's always possible to pass wstrings and dstrings by using the ubyte[] function overload (but be aware of endianness issues!). - @trusted UUID randomUUID();
UUID randomUUID(RNG)(ref RNG randomGen) if (isUniformRNG!RNG && isIntegral!(typeof(RNG.front))); - This function generates a random number based UUID from a random
number generator.
CTFE:
This function is not supported at compile time. Examples://simple call auto uuid = randomUUID(); //provide a custom RNG. Must be seeded manually. Xorshift192 gen; gen.seed(unpredictableSeed); auto uuid3 = randomUUID(gen);
- UUID parseUUID(T)(T uuidString) if (isSomeString!T);
UUID parseUUID(Range)(ref Range uuidRange) if (isInputRange!Range && is(Unqual!(ElementType!Range) == dchar)); - This is a less strict parser compared to the parser used in the
UUID constructor. It enforces the following rules:
- hex numbers are always two hexdigits([0-9a-fA-F])
- there must be exactly 16 such pairs in the input, not less, not more
- there can be exactly one dash between two hex-pairs, but not more
- there can be multiple characters enclosing the 16 hex pairs, as long as these characters do not contain [0-9a-fA-F]
UUIDParsingException if the input is invalid CTFE:
This function is supported in CTFE code. Note that error messages caused by a malformed UUID parsed at compile time can be cryptic, but errors are detected and reported at compile time. Examples:auto id = parseUUID("8AB3060E-2CBA-4F23-b74c-B52Db3BDFB46"); //no dashes id = parseUUID("8ab3060e2cba4f23b74cb52db3bdfb46"); //dashes at different positions id = parseUUID("8a-b3-06-0e2cba4f23b74c-b52db3bdfb-46"); //leading / trailing characters id = parseUUID("{8ab3060e-2cba-4f23-b74c-b52db3bdfb46}"); //unicode id = parseUUID("ü8ab3060e2cba4f23b74cb52db3bdfb46ü"); //multiple trailing/leading characters id = parseUUID("///8ab3060e2cba4f23b74cb52db3bdfb46||"); //Can also be used in CTFE, for example as UUID literals: enum ctfeID = parseUUID("8ab3060e-2cba-4f23-b74c-b52db3bdfb46"); //here parsing is done at compile time, no runtime overhead!
- UUID dnsNamespace;
- Default namespace from RFC 4122 Name string is a fully-qualified domain name
- UUID urlNamespace;
- Default namespace from RFC 4122 Name string is a URL
- UUID oidNamespace;
- Default namespace from RFC 4122 Name string is an ISO OID
- UUID x500Namespace;
- Default namespace from RFC 4122 Name string is an X.500 DN (in DER or a text output format)
- string uuidRegex;
- Regex string to extract UUIDs from text.
Examples:
import std.algorithm; import std.regex; string test = "Lorem ipsum dolor sit amet, consetetur " "6ba7b814-9dad-11d1-80b4-00c04fd430c8 sadipscing \n" "elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore \r\n" "magna aliquyam erat, sed diam voluptua. " "8ab3060e-2cba-4f23-b74c-b52db3bdfb46 At vero eos et accusam et " "justo duo dolores et ea rebum."; auto r = regex(uuidRegex, "g"); UUID[] found; foreach(c; match(test, r)) { found ~= UUID(c.hit); } writeln(found);
Examples:import std.algorithm; import std.regex; string test = "Lorem ipsum dolor sit amet, consetetur " "6ba7b814-9dad-11d1-80b4-00c04fd430c8 sadipscing \n" "elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore \r\n" "magna aliquyam erat, sed diam voluptua. " "8ab3060e-2cba-4f23-b74c-b52db3bdfb46 At vero eos et accusam et " "justo duo dolores et ea rebum."; auto r = regex(uuidRegex, "g"); UUID[] found; foreach(c; match(test, r)) { found ~= UUID(c.hit); } assert(found == [ UUID("6ba7b814-9dad-11d1-80b4-00c04fd430c8"), UUID("8ab3060e-2cba-4f23-b74c-b52db3bdfb46"), ]);
- class UUIDParsingException: object.Exception;
- This exception is thrown if an error occurs when parsing a UUID from a string.