Guid.parse constructor
Guid.parse(
- String guid
Creates a new GUID from a string.
The string must be of the form {dddddddd-dddd-dddd-dddd-dddddddddddd}
.
where d is a hex digit.
Implementation
factory Guid.parse(String guid) {
// This is a debug assert, becuase it's probably computationally expensive,
// and int.parse will throw a FormatException anyway if it can't parse the
// values.
assert(RegExp(r'\{[0-9A-Fa-f]{8}(?:-[0-9A-Fa-f]{4}){3}-[0-9A-Fa-f]{12}}')
.hasMatch(guid));
if (guid.length != 38) {
throw FormatException('GUID is not the correct length', guid);
}
// Note that the order of bytes in the returned byte array is different from
// the string representation of a GUID value. The order of the beginning
// four-byte group and the next two two-byte groups are reversed; the order
// of the final two-byte group and the closing six-byte group are the same.
//
// The following zero-indexed list provides the offset for each 8-bit hex
// value in the string representation.
const offsets = [
7, 5, 3, 1, 12, 10, 17, 15, //
20, 22, 25, 27, 29, 31, 33, 35
];
final guidAsBytes = offsets
.map((idx) => int.parse(guid.substring(idx, idx + 2), radix: 16))
.toList(growable: false);
return Guid(UnmodifiableUint8ListView(Uint8List.fromList(guidAsBytes)));
}