SearchPattern.cs 4.5 KB

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