示例#1
0
            public static PatternByte NewWildcardByte()
            {
                var pb = new PatternByte
                {
                    nibble1 = { IsWildcard = true },
                    nibble2 = { IsWildcard = true }
                };

                return(pb);
            }
示例#2
0
            public static PatternByte AsByte(byte b)
            {
                var pb = new PatternByte
                {
                    nibble1 = { Value = (b >> 4) & 0xF },
                    nibble2 = { Value = b & 0xF }
                };

                return(pb);
            }
示例#3
0
        public static BytePattern From(IEnumerable <Tuple <byte, bool> > data)
        {
            var pattern = new BytePattern();

            foreach (var i in data)
            {
                var pb = i.Item2 ? PatternByte.AsWildcard() : PatternByte.AsByte(i.Item1);

                pattern.pattern.Add(pb);
            }

            return(pattern);
        }
示例#4
0
        /// <summary>
        /// Parses the provided string for a byte pattern. Wildcards are supported by nibble.
        /// </summary>
        /// <example>
        /// Valid patterns:
        /// AA BB CC DD
        /// AABBCCDD
        /// aabb CCdd
        /// A? ?B ?? DD
        /// </example>
        /// <exception cref="ArgumentException">Thrown if the provided string doesn't contain a valid byte pattern.</exception>
        /// <param name="value">The byte pattern in hex format.</param>
        /// <returns>The corresponding <see cref="BytePattern"/>.</returns>
        public static BytePattern Parse(string value)
        {
            Contract.Requires(!string.IsNullOrEmpty(value));
            Contract.Ensures(Contract.Result <BytePattern>() != null);

            var pattern = new BytePattern();

            using (var sr = new StringReader(value))
            {
                var pb = new PatternByte();
                while (pb.TryRead(sr))
                {
                    pattern.pattern.Add(pb);
                }

                // Check if we are not at the end of the stream
                if (sr.Peek() != -1)
                {
                    throw new ArgumentException($"'{value}' is not a valid byte pattern.");
                }
            }

            return(pattern);
        }