2
0

quicksearch.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // assembly: System
  3. // namespace: System.Text.RegularExpressions
  4. // file: quicksearch.cs
  5. //
  6. // Authors: Dan Lewis ([email protected])
  7. // Juraj Skripsky ([email protected])
  8. //
  9. // (c) 2002 Dan Lewis
  10. // (c) 2003 Juraj Skripsky
  11. //
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Collections;
  34. namespace System.Text.RegularExpressions {
  35. internal class QuickSearch {
  36. // simplified boyer-moore for fast substring matching
  37. // (for short strings, we use simple scans)
  38. public QuickSearch (string str, bool ignore)
  39. : this(str, ignore, false)
  40. {
  41. }
  42. public QuickSearch (string str, bool ignore, bool reverse) {
  43. this.str = str;
  44. this.len = str.Length;
  45. this.ignore = ignore;
  46. this.reverse = reverse;
  47. if (ignore)
  48. str = str.ToLower ();
  49. // create the shift table only for "long" search strings
  50. if(len > THRESHOLD)
  51. SetupShiftTable ();
  52. }
  53. public string String {
  54. get { return str; }
  55. }
  56. public int Length {
  57. get { return len; }
  58. }
  59. public bool IgnoreCase {
  60. get { return ignore; }
  61. }
  62. public int Search (string text, int start, int end) {
  63. int ptr = start;
  64. if ( reverse )
  65. {
  66. if (start < end)
  67. return -1;
  68. if ( ptr > text.Length)
  69. {
  70. ptr = text.Length;
  71. }
  72. // use simple scan for a single-character search string
  73. if (len == 1)
  74. {
  75. while (--ptr >= end)
  76. {
  77. if(str[0] == GetChar(text[ptr]))
  78. return ptr ;
  79. }
  80. return -1;
  81. }
  82. if ( end < len)
  83. end = len - 1 ;
  84. ptr--;
  85. while (ptr >= end)
  86. {
  87. int i = len -1 ;
  88. while (str[i] == GetChar(text[ptr - len +1 + i]))
  89. {
  90. if (-- i < 0)
  91. return ptr - len + 1;
  92. }
  93. if (ptr > end)
  94. {
  95. ptr -= GetShiftDistance (text[ptr - len ]);
  96. }
  97. else
  98. break;
  99. }
  100. }
  101. else
  102. {
  103. // use simple scan for a single-character search string
  104. if (len == 1)
  105. {
  106. while (ptr <= end)
  107. {
  108. if(str[0] == GetChar(text[ptr]))
  109. return ptr;
  110. else
  111. ptr++;
  112. }
  113. return -1;
  114. }
  115. if (end > text.Length - len)
  116. end = text.Length - len;
  117. while (ptr <= end)
  118. {
  119. int i = len - 1;
  120. while (str[i] == GetChar(text[ptr + i]))
  121. {
  122. if (-- i < 0)
  123. return ptr;
  124. }
  125. if (ptr < end)
  126. ptr += GetShiftDistance (text[ptr + len]);
  127. else
  128. break;
  129. }
  130. }
  131. return -1;
  132. }
  133. // private
  134. private void SetupShiftTable () {
  135. shift = new Hashtable ();
  136. if (reverse)
  137. {
  138. for (int i = len ; i > 0; -- i)
  139. {
  140. char c = str[i -1];
  141. shift[GetChar(c)] = i;
  142. }
  143. }
  144. else
  145. {
  146. for (int i = 0; i < len; ++ i)
  147. {
  148. char c = str[i];
  149. shift[GetChar(c)] = len - i;
  150. }
  151. }
  152. }
  153. private int GetShiftDistance (char c) {
  154. if(shift == null)
  155. return 1;
  156. object s = shift[c];
  157. return (s != null ? (int)s : len + 1);
  158. }
  159. private char GetChar(char c) {
  160. return (!ignore ? c : Char.ToLower(c));
  161. }
  162. private string str;
  163. private int len;
  164. private bool ignore;
  165. private bool reverse;
  166. private Hashtable shift;
  167. private readonly static int THRESHOLD = 5;
  168. }
  169. }