parseUUID

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]

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

string s = "8AB3060E-2CBA-4F23-b74c-B52Db3BDFB46";
parseUUID(s);
assert(s == "");
  1. UUID parseUUID(T uuidString)
  2. UUID parseUUID(Range uuidRange)
    parseUUID
    (
    Range
    )
    (
    ref Range uuidRange
    )

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.

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!

Meta