private bool Match(SearchPattern2.Op op, string text, int ptr) { while (op != null) { switch (op.Code) { case SearchPattern2.OpCode.ExactString: { int length = op.Argument.Length; if (ptr + length > text.Length) { return(false); } string text2 = text.Substring(ptr, length); if (this.ignore) { text2 = text2.ToLower(); } if (text2 != op.Argument) { return(false); } ptr += length; break; } case SearchPattern2.OpCode.AnyChar: if (++ptr > text.Length) { return(false); } break; case SearchPattern2.OpCode.AnyString: while (ptr <= text.Length) { if (this.Match(op.Next, text, ptr)) { return(true); } ptr++; } return(false); case SearchPattern2.OpCode.End: return(ptr == text.Length); case SearchPattern2.OpCode.True: return(true); } op = op.Next; } return(true); }
private void Compile(string pattern) { if (pattern == null || pattern.IndexOfAny(SearchPattern2.InvalidChars) >= 0) { throw new ArgumentException("Invalid search pattern: '" + pattern + "'"); } if (pattern == "*") { this.ops = new SearchPattern2.Op(SearchPattern2.OpCode.True); this.hasWildcard = true; return; } this.ops = null; int i = 0; SearchPattern2.Op op = null; while (i < pattern.Length) { char c = pattern[i]; SearchPattern2.Op op2; if (c != '*') { if (c != '?') { op2 = new SearchPattern2.Op(SearchPattern2.OpCode.ExactString); int num = pattern.IndexOfAny(SearchPattern2.WildcardChars, i); if (num < 0) { num = pattern.Length; } op2.Argument = pattern.Substring(i, num - i); if (this.ignore) { op2.Argument = op2.Argument.ToLower(); } i = num; } else { op2 = new SearchPattern2.Op(SearchPattern2.OpCode.AnyChar); i++; this.hasWildcard = true; } } else { op2 = new SearchPattern2.Op(SearchPattern2.OpCode.AnyString); i++; this.hasWildcard = true; } if (op == null) { this.ops = op2; } else { op.Next = op2; } op = op2; } if (op == null) { this.ops = new SearchPattern2.Op(SearchPattern2.OpCode.End); } else { op.Next = new SearchPattern2.Op(SearchPattern2.OpCode.End); } }
public Op(SearchPattern2.OpCode code) { this.Code = code; this.Argument = null; this.Next = null; }