SearchPattern.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // System.IO.SearchPattern2.cs: Filename glob support.
  3. //
  4. // Author:
  5. // Dan Lewis ([email protected])
  6. //
  7. // (C) 2002
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. // Copied from corlib/System.IO/SearchPatter.cs
  30. using System;
  31. namespace System.IO {
  32. // FIXME: there's a complication with this algorithm under windows.
  33. // the pattern '*.*' matches all files (i think . matches the extension),
  34. // whereas under UNIX it should only match files containing the '.' character.
  35. class SearchPattern2 {
  36. public SearchPattern2 (string pattern) : this (pattern, false) { }
  37. public SearchPattern2 (string pattern, bool ignore)
  38. {
  39. this.ignore = ignore;
  40. this.pattern = pattern;
  41. Compile (pattern);
  42. }
  43. // OSX has a retarded case-insensitive yet case-aware filesystem
  44. // so we need a overload in here for the Kqueue watcher
  45. public bool IsMatch (string text, bool ignorecase)
  46. {
  47. if (!hasWildcard)
  48. return (String.Compare (pattern, text, ignorecase) == 0);
  49. return Match (ops, text, 0);
  50. }
  51. public bool IsMatch (string text)
  52. {
  53. return IsMatch (text, ignore);
  54. }
  55. public bool HasWildcard {
  56. get { return hasWildcard; }
  57. }
  58. // private
  59. Op ops; // the compiled pattern
  60. bool ignore; // ignore case
  61. bool hasWildcard;
  62. string pattern;
  63. private void Compile (string pattern)
  64. {
  65. if (pattern == null || pattern.IndexOfAny (InvalidChars) >= 0)
  66. throw new ArgumentException ("Invalid search pattern: '" + pattern + "'");
  67. if (pattern == "*") { // common case
  68. ops = new Op (OpCode.True);
  69. hasWildcard = true;
  70. return;
  71. }
  72. ops = null;
  73. int ptr = 0;
  74. Op last_op = null;
  75. while (ptr < pattern.Length) {
  76. Op op;
  77. switch (pattern [ptr]) {
  78. case '?':
  79. op = new Op (OpCode.AnyChar);
  80. ++ ptr;
  81. hasWildcard = true;
  82. break;
  83. case '*':
  84. op = new Op (OpCode.AnyString);
  85. ++ ptr;
  86. hasWildcard = true;
  87. break;
  88. default:
  89. op = new Op (OpCode.ExactString);
  90. int end = pattern.IndexOfAny (WildcardChars, ptr);
  91. if (end < 0)
  92. end = pattern.Length;
  93. op.Argument = pattern.Substring (ptr, end - ptr);
  94. if (ignore)
  95. op.Argument = op.Argument.ToLower ();
  96. ptr = end;
  97. break;
  98. }
  99. if (last_op == null)
  100. ops = op;
  101. else
  102. last_op.Next = op;
  103. last_op = op;
  104. }
  105. if (last_op == null)
  106. ops = new Op (OpCode.End);
  107. else
  108. last_op.Next = new Op (OpCode.End);
  109. }
  110. private bool Match (Op op, string text, int ptr)
  111. {
  112. while (op != null) {
  113. switch (op.Code) {
  114. case OpCode.True:
  115. return true;
  116. case OpCode.End:
  117. if (ptr == text.Length)
  118. return true;
  119. return false;
  120. case OpCode.ExactString:
  121. int length = op.Argument.Length;
  122. if (ptr + length > text.Length)
  123. return false;
  124. string str = text.Substring (ptr, length);
  125. if (ignore)
  126. str = str.ToLower ();
  127. if (str != op.Argument)
  128. return false;
  129. ptr += length;
  130. break;
  131. case OpCode.AnyChar:
  132. if (++ ptr > text.Length)
  133. return false;
  134. break;
  135. case OpCode.AnyString:
  136. while (ptr <= text.Length) {
  137. if (Match (op.Next, text, ptr))
  138. return true;
  139. ++ ptr;
  140. }
  141. return false;
  142. }
  143. op = op.Next;
  144. }
  145. return true;
  146. }
  147. // private static
  148. internal static readonly char [] WildcardChars = { '*', '?' };
  149. internal static readonly char [] InvalidChars = { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };
  150. private class Op {
  151. public Op (OpCode code)
  152. {
  153. this.Code = code;
  154. this.Argument = null;
  155. this.Next = null;
  156. }
  157. public OpCode Code;
  158. public string Argument;
  159. public Op Next;
  160. }
  161. private enum OpCode {
  162. ExactString, // literal
  163. AnyChar, // ?
  164. AnyString, // *
  165. End, // end of pattern
  166. True // always succeeds
  167. };
  168. }
  169. }