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