public static PatternByte NewWildcardByte() { var pb = new PatternByte { nibble1 = { IsWildcard = true }, nibble2 = { IsWildcard = true } }; return(pb); }
public static PatternByte AsByte(byte b) { var pb = new PatternByte { nibble1 = { Value = (b >> 4) & 0xF }, nibble2 = { Value = b & 0xF } }; return(pb); }
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); }
/// <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); }