private static ulong Extract(IReadOnlyString source, int offset, int count, ulong accumulator, UUIDParseFormat format) { for (int i = offset; i < offset + count; i += 1) { int d = HexDigit(source[i], format); if (d == -1) { throw new ArgumentException("UUID is improperly formatted."); } accumulator = (accumulator << 4) | (uint)d; } return(accumulator); }
private static bool CanExtract(IReadOnlyString source, int offset, int count, ulong accumulator, UUIDParseFormat format) { if (offset + count - 1 >= source.Length) { return(false); } for (int i = offset; i < offset + count; i += 1) { int d = HexDigit(source[i], format); if (d == -1) { return(false); } } return(true); }
/// <summary> /// Assign this UUID by string with offset using specified <see cref="UUIDParseFormat"/>. /// </summary> /// <param name="str">string</param> /// <param name="offset">offset</param> /// <param name="format">Input string format.</param> public UUID(IReadOnlyString str, int offset = 0, UUIDParseFormat format = UUIDParseFormat.Any) { if (str == null) { throw new ArgumentNullException(nameof(str)); } if (str.Length < offset + 32) { throw new ArgumentException("UUID is too short.", nameof(str)); } if (format == UUIDParseFormat.Any) { format = str[offset + 8] == '-' ? UUIDParseFormat.AnyCase : UUIDParseFormat.AnyCaseWithoutDashes; } switch (format) { case UUIDParseFormat.LowerCase: case UUIDParseFormat.UpperCase: case UUIDParseFormat.AnyCase: { ulong m = Extract(str, offset, 8, 0UL, format); if (str[offset + 8] != '-') { throw new ArgumentException("UUID is improperly formatted", str.ToString()); } m = Extract(str, offset + 9, 4, m, format); if (str[offset + 13] != '-') { throw new ArgumentException("UUID is improperly formatted", str.ToString()); } m = Extract(str, offset + 14, 4, m, format); if (str[offset + 18] != '-') { throw new ArgumentException("UUID is improperly formatted", str.ToString()); } ulong l = Extract(str, offset + 19, 4, 0L, format); if (str[offset + 23] != '-') { throw new ArgumentException("UUID is improperly formatted", str.ToString()); } l = Extract(str, offset + 24, 12, l, format); MSB = m; LSB = l; break; } case UUIDParseFormat.LowerCaseWithoutDashes: case UUIDParseFormat.UpperCaseWithoutDashes: case UUIDParseFormat.AnyCaseWithoutDashes: MSB = Extract(str, offset, 16, 0L, format); LSB = Extract(str, offset + 16, 16, 0L, format); break; default: throw new ArgumentException("Unsupported format", format.ToString()); } }
/// <summary> /// Assign this UUID by string using specified <see cref="UUIDParseFormat"/>. /// </summary> /// <param name="str">string</param> /// <param name="format">Input string format.</param> public UUID(IReadOnlyString str, UUIDParseFormat format) : this(str, 0, format) { }
/// <summary> /// Return true if you can parse string as valid uuid. /// </summary> /// <param name="str">String</param> /// <param name="offset">Offset of string</param> /// <param name="format">Format of string</param> /// <param name="output">UUID.Empty if str is not valid uuid or parsed uuid if valid</param> /// <returns>True if you can parse string as valid uuid.</returns> public static bool TryParse(IReadOnlyString str, int offset, UUIDParseFormat format, out UUID output) { output = Empty; if (str == null) { return(false); } if (str.Length < offset + 32) { return(false); } if (format == UUIDParseFormat.Any) { format = str[offset + 8] == '-' ? UUIDParseFormat.AnyCase : UUIDParseFormat.AnyCaseWithoutDashes; } switch (format) { case UUIDParseFormat.LowerCase: case UUIDParseFormat.UpperCase: case UUIDParseFormat.AnyCase: { if (!CanExtract(str, offset, 8, 0L, format)) { return(false); } ulong m = Extract(str, offset, 8, 0L, format); if (str[offset + 8] != '-') { return(false); } if (!CanExtract(str, offset + 9, 4, m, format)) { return(false); } m = Extract(str, offset + 9, 4, m, format); if (str[offset + 13] != '-') { return(false); } if (!CanExtract(str, offset + 14, 4, m, format)) { return(false); } m = Extract(str, offset + 14, 4, m, format); if (str[offset + 18] != '-') { return(false); } if (!CanExtract(str, offset + 19, 4, 0L, format)) { return(false); } ulong l = Extract(str, offset + 19, 4, 0L, format); if (str[offset + 23] != '-') { return(false); } if (!CanExtract(str, offset + 24, 12, l, format)) { return(false); } l = Extract(str, offset + 24, 12, l, format); output.MSB = m; output.LSB = l; break; } case UUIDParseFormat.LowerCaseWithoutDashes: case UUIDParseFormat.UpperCaseWithoutDashes: case UUIDParseFormat.AnyCaseWithoutDashes: if ((!CanExtract(str, offset, 16, 0L, format)) || (!CanExtract(str, offset + 16, 16, 0L, format))) { return(false); } output.MSB = Extract(str, offset, 16, 0L, format); output.LSB = Extract(str, offset + 16, 16, 0L, format); break; } return(true); }
/// <summary> /// Return true if you can parse string as valid uuid. /// </summary> /// <param name="str">String</param> /// <param name="format">Format of string</param> /// <param name="output">UUID.Empty if str is not valid uuid or parsed uuid if valid</param> /// <returns>True if you can parse string as valid uuid.</returns> public static bool TryParse(IReadOnlyString str, UUIDParseFormat format, out UUID output) { return(TryParse(str, 0, format, out output)); }
/// <summary> /// Return true if you can parse string as valid uuid. /// </summary> /// <param name="str">String</param> /// <param name="offset">Offset of string</param> /// <param name="output">UUID.Empty if str is not valid uuid or parsed uuid if valid</param> /// <returns>True if you can parse string as valid uuid.</returns> public static bool TryParse(IReadOnlyString str, int offset, out UUID output) { return(TryParse(str, offset, UUIDParseFormat.Any, out output)); }
/// <summary> /// Return true if you can parse string as valid uuid. /// </summary> /// <param name="str">String</param> /// <param name="output">UUID.Empty if str is not valid uuid or parsed uuid if valid</param> /// <returns>True if you can parse string as valid uuid.</returns> public static bool TryParse(IReadOnlyString str, out UUID output) { return(TryParse(str, 0, out output)); }
/// <summary> /// Return true if string is valid uuid. /// </summary> /// <param name="str">string</param> /// <param name="format">format of uuid.</param> /// <returns>True if string is valid uuid.</returns> public static bool IsValid(IReadOnlyString str, UUIDParseFormat format) { return(IsValid(str, 0, format)); }
/// <summary> /// Return true if string is valid uuid. /// </summary> /// <param name="str">string</param> /// <param name="offset">offset of string</param> /// <returns>true if string is valid uuid.</returns> public static bool IsValid(IReadOnlyString str, int offset) { return(IsValid(str, offset, UUIDParseFormat.Any)); }
/// <summary> /// Return true if string is valid uuid. /// </summary> /// <param name="str">string</param> /// <returns>true if string is valid uuid.</returns> public static bool IsValid(IReadOnlyString str) { return(IsValid(str, 0)); }