2
0

SearchPattern.cs 4.2 KB

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