SearchPattern.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. public bool IsMatch (string text)
  44. {
  45. if (!hasWildcard)
  46. return (String.Compare (pattern, text, ignore) == 0);
  47. return Match (ops, text, 0);
  48. }
  49. public bool HasWildcard {
  50. get { return hasWildcard; }
  51. }
  52. // private
  53. Op ops; // the compiled pattern
  54. bool ignore; // ignore case
  55. bool hasWildcard;
  56. string pattern;
  57. private void Compile (string pattern)
  58. {
  59. if (pattern == null || pattern.IndexOfAny (InvalidChars) >= 0)
  60. throw new ArgumentException ("Invalid search pattern: '" + pattern + "'");
  61. if (pattern == "*") { // common case
  62. ops = new Op (OpCode.True);
  63. hasWildcard = true;
  64. return;
  65. }
  66. ops = null;
  67. int ptr = 0;
  68. Op last_op = null;
  69. while (ptr < pattern.Length) {
  70. Op op;
  71. switch (pattern [ptr]) {
  72. case '?':
  73. op = new Op (OpCode.AnyChar);
  74. ++ ptr;
  75. hasWildcard = true;
  76. break;
  77. case '*':
  78. op = new Op (OpCode.AnyString);
  79. ++ ptr;
  80. hasWildcard = true;
  81. break;
  82. default:
  83. op = new Op (OpCode.ExactString);
  84. int end = pattern.IndexOfAny (WildcardChars, ptr);
  85. if (end < 0)
  86. end = pattern.Length;
  87. op.Argument = pattern.Substring (ptr, end - ptr);
  88. if (ignore)
  89. op.Argument = op.Argument.ToLower ();
  90. ptr = end;
  91. break;
  92. }
  93. if (last_op == null)
  94. ops = op;
  95. else
  96. last_op.Next = op;
  97. last_op = op;
  98. }
  99. if (last_op == null)
  100. ops = new Op (OpCode.End);
  101. else
  102. last_op.Next = new Op (OpCode.End);
  103. }
  104. private bool Match (Op op, string text, int ptr)
  105. {
  106. while (op != null) {
  107. switch (op.Code) {
  108. case OpCode.True:
  109. return true;
  110. case OpCode.End:
  111. if (ptr == text.Length)
  112. return true;
  113. return false;
  114. case OpCode.ExactString:
  115. int length = op.Argument.Length;
  116. if (ptr + length > text.Length)
  117. return false;
  118. string str = text.Substring (ptr, length);
  119. if (ignore)
  120. str = str.ToLower ();
  121. if (str != op.Argument)
  122. return false;
  123. ptr += length;
  124. break;
  125. case OpCode.AnyChar:
  126. if (++ ptr > text.Length)
  127. return false;
  128. break;
  129. case OpCode.AnyString:
  130. while (ptr <= text.Length) {
  131. if (Match (op.Next, text, ptr))
  132. return true;
  133. ++ ptr;
  134. }
  135. return false;
  136. }
  137. op = op.Next;
  138. }
  139. return true;
  140. }
  141. // private static
  142. internal static readonly char [] WildcardChars = { '*', '?' };
  143. internal static readonly char [] InvalidChars = { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };
  144. private class Op {
  145. public Op (OpCode code)
  146. {
  147. this.Code = code;
  148. this.Argument = null;
  149. this.Next = null;
  150. }
  151. public OpCode Code;
  152. public string Argument;
  153. public Op Next;
  154. }
  155. private enum OpCode {
  156. ExactString, // literal
  157. AnyChar, // ?
  158. AnyString, // *
  159. End, // end of pattern
  160. True // always succeeds
  161. };
  162. }
  163. }