2 // System.IO.SearchPattern.cs: Filename glob support.
5 // Dan Lewis (dihlewis@yahoo.co.uk)
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.
37 // FIXME: there's a complication with this algorithm under windows.
38 // the pattern '*.*' matches all files (i think . matches the extension),
39 // whereas under UNIX it should only match files containing the '.' character.
42 public SearchPattern (string pattern
) : this (pattern
, false) { }
44 public SearchPattern (string pattern
, bool ignore
)
50 public bool IsMatch (string text
)
52 return Match (ops
, text
, 0);
57 private Op ops
; // the compiled pattern
58 private bool ignore
; // ignore case
60 private void Compile (string pattern
)
62 if (pattern
== null || pattern
.IndexOfAny (InvalidChars
) >= 0)
63 throw new ArgumentException ("Invalid search pattern.");
65 if (pattern
== "*") { // common case
66 ops
= new Op (OpCode
.True
);
74 while (ptr
< pattern
.Length
) {
77 switch (pattern
[ptr
]) {
79 op
= new Op (OpCode
.AnyChar
);
84 op
= new Op (OpCode
.AnyString
);
89 op
= new Op (OpCode
.ExactString
);
90 int end
= pattern
.IndexOfAny (WildcardChars
, ptr
);
94 op
.Argument
= pattern
.Substring (ptr
, end
- ptr
);
96 op
.Argument
= op
.Argument
.ToLowerInvariant ();
111 ops
= new Op (OpCode
.End
);
113 last_op
.Next
= new Op (OpCode
.End
);
116 private bool Match (Op op
, string text
, int ptr
)
124 if (ptr
== text
.Length
)
129 case OpCode
.ExactString
:
130 int length
= op
.Argument
.Length
;
131 if (ptr
+ length
> text
.Length
)
134 string str
= text
.Substring (ptr
, length
);
136 str
= str
.ToLowerInvariant ();
138 if (str
!= op
.Argument
)
145 if (++ ptr
> text
.Length
)
149 case OpCode
.AnyString
:
150 while (ptr
<= text
.Length
) {
151 if (Match (op
.Next
, text
, ptr
))
168 internal static readonly char [] WildcardChars
= { '*', '?' }
;
169 internal static readonly char [] InvalidChars
= { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }
;
172 public Op (OpCode code
)
175 this.Argument
= null;
180 public string Argument
;
184 private enum OpCode
{
185 ExactString
, // literal
188 End
, // end of pattern
189 True
// always succeeds