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)
\r
13 // Permission is hereby granted, free of charge, to any person obtaining
\r
14 // a copy of this software and associated documentation files (the
\r
15 // "Software"), to deal in the Software without restriction, including
\r
16 // without limitation the rights to use, copy, modify, merge, publish,
\r
17 // distribute, sublicense, and/or sell copies of the Software, and to
\r
18 // permit persons to whom the Software is furnished to do so, subject to
\r
19 // the following conditions:
\r
21 // The above copyright notice and this permission notice shall be
\r
22 // included in all copies or substantial portions of the Software.
\r
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
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
44 public SearchPattern (string pattern) : this (pattern, false) { }
\r
46 public SearchPattern (string pattern, bool ignore)
\r
48 this.ignore = ignore;
\r
52 public bool IsMatch (string text)
\r
54 return Match (ops, text, 0);
\r
59 private Op ops; // the compiled pattern
\r
60 private bool ignore; // ignore case
\r
62 private void Compile (string pattern)
\r
64 if (pattern == null || pattern.IndexOfAny (InvalidChars) >= 0)
\r
65 throw new ArgumentException ("Invalid search pattern.");
\r
67 if (pattern == "*") { // common case
\r
68 ops = new Op (OpCode.True);
\r
76 while (ptr < pattern.Length) {
\r
79 switch (pattern [ptr]) {
\r
81 op = new Op (OpCode.AnyChar);
\r
86 op = new Op (OpCode.AnyString);
\r
91 op = new Op (OpCode.ExactString);
\r
92 int end = pattern.IndexOfAny (WildcardChars, ptr);
\r
94 end = pattern.Length;
\r
96 op.Argument = pattern.Substring (ptr, end - ptr);
\r
98 op.Argument = op.Argument.ToLowerInvariant ();
\r
104 if (last_op == null)
\r
112 if (last_op == null)
\r
113 ops = new Op (OpCode.End);
\r
115 last_op.Next = new Op (OpCode.End);
\r
118 private bool Match (Op op, string text, int ptr)
\r
120 while (op != null) {
\r
126 if (ptr == text.Length)
\r
131 case OpCode.ExactString:
\r
132 int length = op.Argument.Length;
\r
133 if (ptr + length > text.Length)
\r
136 string str = text.Substring (ptr, length);
\r
138 str = str.ToLowerInvariant ();
\r
140 if (str != op.Argument)
\r
146 case OpCode.AnyChar:
\r
147 if (++ ptr > text.Length)
\r
151 case OpCode.AnyString:
\r
152 while (ptr <= text.Length) {
\r
153 if (Match (op.Next, text, ptr))
\r
170 internal static readonly char [] WildcardChars = { '*', '?' };
\r
172 internal static readonly char [] InvalidChars = { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };
\r
175 public Op (OpCode code)
\r
178 this.Argument = null;
\r
182 public OpCode Code;
\r
183 public string Argument;
\r
187 private enum OpCode {
\r
188 ExactString, // literal
\r
191 End, // end of pattern
\r
192 True // always succeeds
\r