View source code
Display the source code in std/uuid.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
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.

Function std.uuid.parseUUID

This is a less strict parser compared to the parser used in the UUID constructor. It enforces the following rules:

UUID parseUUID(T) (
  T uuidString
)
if (isSomeString!T);

UUID parseUUID(Range) (
  ref Range uuidRange
)
if (isInputRange!Range && isSomeChar!(ElementType!Range));

  • 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]

Note

Like most parsers, it consumes its argument. This means:

string s = "8AB3060E-2CBA-4F23-b74c-B52Db3BDFB46";
parseUUID(s);
assert(s == "");

Throws

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.

Example

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!

Authors

Johannes Pfau

License

Boost License 1.0.