2 // System.IO.SearchPattern.cs: Filename glob support.
\r
5 // Dan Lewis (dihlewis@yahoo.co.uk)
\r
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 namespace System.IO {
\r
37 // FIXME: there's a complication with this algorithm under windows.
\r
38 // the pattern '*.*' matches all files (i think . matches the extension),
\r
39 // whereas under UNIX it should only match files containing the '.' character.
\r
41 class SearchPattern {
\r
42 public SearchPattern (string pattern) : this (pattern, false) { }
\r
44 public SearchPattern (string pattern, bool ignore)
\r
46 this.ignore = ignore;
\r
50 public bool IsMatch (string text)
\r
52 return Match (ops, text, 0);
\r
57 private Op ops; // the compiled pattern
\r
58 private bool ignore; // ignore case
\r
60 private void Compile (string pattern)
\r
62 if (pattern == null || pattern.IndexOfAny (InvalidChars) >= 0)
\r
63 throw new ArgumentException ("Invalid search pattern.");
\r
65 if (pattern == "*") { // common case
\r
66 ops = new Op (OpCode.True);
\r
74 while (ptr < pattern.Length) {
\r
77 switch (pattern [ptr]) {
\r
79 op = new Op (OpCode.AnyChar);
\r
84 op = new Op (OpCode.AnyString);
\r
89 op = new Op (OpCode.ExactString);
\r
90 int end = pattern.IndexOfAny (WildcardChars, ptr);
\r
92 end = pattern.Length;
\r
94 op.Argument = pattern.Substring (ptr, end - ptr);
\r
96 op.Argument = op.Argument.ToLowerInvariant ();
\r
102 if (last_op == null)
\r
110 if (last_op == null)
\r
111 ops = new Op (OpCode.End);
\r
113 last_op.Next = new Op (OpCode.End);
\r
116 private bool Match (Op op, string text, int ptr)
\r
118 while (op != null) {
\r
124 if (ptr == text.Length)
\r
129 case OpCode.ExactString:
\r
130 int length = op.Argument.Length;
\r
131 if (ptr + length > text.Length)
\r
134 string str = text.Substring (ptr, length);
\r
136 str = str.ToLowerInvariant ();
\r
138 if (str != op.Argument)
\r
144 case OpCode.AnyChar:
\r
145 if (++ ptr > text.Length)
\r
149 case OpCode.AnyString:
\r
150 while (ptr <= text.Length) {
\r
151 if (Match (op.Next, text, ptr))
\r
168 internal static readonly char [] WildcardChars = { '*', '?' };
\r
169 internal static readonly char [] InvalidChars = { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };
\r
172 public Op (OpCode code)
\r
175 this.Argument = null;
\r
179 public OpCode Code;
\r
180 public string Argument;
\r
184 private enum OpCode {
\r
185 ExactString, // literal
\r
188 End, // end of pattern
\r
189 True // always succeeds
\r