SearchPattern.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // System.IO.SearchPattern2.cs: Filename glob support.
  3. //
  4. // Author:
  5. // Dan Lewis ([email protected])
  6. //
  7. // (C) 2002
  8. //
  9. // Copied from corlib/System.IO/SearchPatter.cs
  10. using System;
  11. namespace System.IO {
  12. // FIXME: there's a complication with this algorithm under windows.
  13. // the pattern '*.*' matches all files (i think . matches the extension),
  14. // whereas under UNIX it should only match files containing the '.' character.
  15. class SearchPattern2 {
  16. public SearchPattern2 (string pattern) : this (pattern, false) { }
  17. public SearchPattern2 (string pattern, bool ignore)
  18. {
  19. this.ignore = ignore;
  20. Compile (pattern);
  21. }
  22. public bool IsMatch (string text)
  23. {
  24. return Match (ops, text, 0);
  25. }
  26. // private
  27. private Op ops; // the compiled pattern
  28. private bool ignore; // ignore case
  29. private void Compile (string pattern)
  30. {
  31. if (pattern == null || pattern.IndexOfAny (InvalidChars) >= 0)
  32. throw new ArgumentException ("Invalid search pattern.");
  33. if (pattern == "*") { // common case
  34. ops = new Op (OpCode.True);
  35. return;
  36. }
  37. ops = null;
  38. int ptr = 0;
  39. Op last_op = null;
  40. while (ptr < pattern.Length) {
  41. Op op;
  42. switch (pattern [ptr]) {
  43. case '?':
  44. op = new Op (OpCode.AnyChar);
  45. ++ ptr;
  46. break;
  47. case '*':
  48. op = new Op (OpCode.AnyString);
  49. ++ ptr;
  50. break;
  51. default:
  52. op = new Op (OpCode.ExactString);
  53. int end = pattern.IndexOfAny (WildcardChars, ptr);
  54. if (end < 0)
  55. end = pattern.Length;
  56. op.Argument = pattern.Substring (ptr, end - ptr);
  57. if (ignore)
  58. op.Argument = op.Argument.ToLower ();
  59. ptr = end;
  60. break;
  61. }
  62. if (last_op == null)
  63. ops = op;
  64. else
  65. last_op.Next = op;
  66. last_op = op;
  67. }
  68. if (last_op == null)
  69. ops = new Op (OpCode.End);
  70. else
  71. last_op.Next = new Op (OpCode.End);
  72. }
  73. private bool Match (Op op, string text, int ptr)
  74. {
  75. while (op != null) {
  76. switch (op.Code) {
  77. case OpCode.True:
  78. return true;
  79. case OpCode.End:
  80. if (ptr == text.Length)
  81. return true;
  82. return false;
  83. case OpCode.ExactString:
  84. int length = op.Argument.Length;
  85. if (ptr + length > text.Length)
  86. return false;
  87. string str = text.Substring (ptr, length);
  88. if (ignore)
  89. str = str.ToLower ();
  90. if (str != op.Argument)
  91. return false;
  92. ptr += length;
  93. break;
  94. case OpCode.AnyChar:
  95. if (++ ptr > text.Length)
  96. return false;
  97. break;
  98. case OpCode.AnyString:
  99. while (ptr <= text.Length) {
  100. if (Match (op.Next, text, ptr))
  101. return true;
  102. ++ ptr;
  103. }
  104. return false;
  105. }
  106. op = op.Next;
  107. }
  108. return true;
  109. }
  110. // private static
  111. internal static readonly char [] WildcardChars = { '*', '?' };
  112. internal static readonly char [] InvalidChars = { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };
  113. private class Op {
  114. public Op (OpCode code)
  115. {
  116. this.Code = code;
  117. this.Argument = null;
  118. this.Next = null;
  119. }
  120. public OpCode Code;
  121. public string Argument;
  122. public Op Next;
  123. }
  124. private enum OpCode {
  125. ExactString, // literal
  126. AnyChar, // ?
  127. AnyString, // *
  128. End, // end of pattern
  129. True // always succeeds
  130. };
  131. }
  132. }