Пример #1
0
 private void TestFromStringFormat(String str, UUIDParseFormat format, bool isCorrect)
 {
     if (isCorrect)
     {
         UUID uuid = new UUID(str, format);
         Assert.AreEqual((UUID)Guid.Parse(str), uuid);
     }
     else
     {
         try
         {
             UUID uuid = new UUID(str, format);
             Assert.Fail("UUID: " + str + ". format: " + format);
         }
         catch (Exception)
         {
         }
     }
 }
Пример #2
0
        private static int HexDigit(char c, UUIDParseFormat format)
        {
            if (c >= '0' && c <= '9')
            {
                return(c - '0');
            }
            switch (format)
            {
            case UUIDParseFormat.LowerCase:
            case UUIDParseFormat.LowerCaseWithoutDashes:
                if (c >= 'a' && c <= 'f')
                {
                    return(10 + c - 'a');
                }
                return(-1);

            case UUIDParseFormat.UpperCase:
            case UUIDParseFormat.UpperCaseWithoutDashes:
                if (c >= 'A' && c <= 'F')
                {
                    return(10 + c - 'A');
                }
                return(-1);

            case UUIDParseFormat.AnyCase:
            case UUIDParseFormat.AnyCaseWithoutDashes:
            case UUIDParseFormat.Any:
                if (c >= 'a' && c <= 'f')
                {
                    return(10 + c - 'a');
                }
                if (c >= 'A' && c <= 'F')
                {
                    return(10 + c - 'A');
                }
                return(-1);
            }
            return(-1);
        }
Пример #3
0
        /// <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());
            }
        }
Пример #4
0
 /// <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)
 {
 }
Пример #5
0
 /// <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(string str, UUIDParseFormat format)
     : this(str, 0, format)
 {
 }
Пример #6
0
        /// <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(string 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);
        }
Пример #7
0
 /// <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(string str, UUIDParseFormat format, out UUID output)
 {
     return(TryParse(str, 0, format, out output));
 }
Пример #8
0
 private static bool CanExtract(string 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);
 }
Пример #9
0
 /// <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(string str, UUIDParseFormat format)
 {
     return(IsValid(str, 0, format));
 }
Пример #10
0
        /// <summary>
        /// Return true if string is valid uuid.
        /// </summary>
        /// <param name="str">string</param>
        /// <param name="offset">offset of uuid.</param>
        /// <param name="format">format of uuid</param>
        /// <returns>True if string is valid uuid.</returns>
        public static bool IsValid(IReadOnlyString str, int offset, UUIDParseFormat format)
        {
            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, 0, format))
                {
                    return(false);
                }
                ulong m = Extract(str, offset, 8, 0, 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);
                }
                ulong l = 0;
                if (!CanExtract(str, offset + 19, 4, 0L, format))
                {
                    return(false);
                }
                l = Extract(str, offset + 19, 4, 0L, format);
                if (str[offset + 23] != '-')
                {
                    return(false);
                }
                if (!CanExtract(str, offset + 24, 12, l, format))
                {
                    return(false);
                }

                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);
                }
                break;
            }

            return(true);
        }
Пример #11
0
 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);
 }