3 // namespace: System.Text.RegularExpressions
4 // file: quicksearch.cs
6 // Authors: Dan Lewis (dlewis@gmx.co.uk)
7 // Juraj Skripsky (juraj@hotfeet.ch)
10 // (c) 2003 Juraj Skripsky
14 using System.Collections;
16 namespace System.Text.RegularExpressions {
17 internal class QuickSearch {
18 // simplified boyer-moore for fast substring matching
19 // (for short strings, we use simple scans)
20 public QuickSearch (string str, bool ignore)
21 : this(str, ignore, false)
25 public QuickSearch (string str, bool ignore, bool reverse) {
27 this.len = str.Length;
29 this.reverse = reverse;
34 // create the shift table only for "long" search strings
39 public string String {
47 public bool IgnoreCase {
48 get { return ignore; }
51 public int Search (string text, int start, int end) {
60 if ( ptr > text.Length)
65 // use simple scan for a single-character search string
70 if(str[0] == GetChar(text[ptr]))
85 while (str[i] == GetChar(text[ptr - len +1 + i]))
93 ptr -= GetShiftDistance (text[ptr - len ]);
103 // use simple scan for a single-character search string
108 if(str[0] == GetChar(text[ptr]))
116 if (end > text.Length - len)
117 end = text.Length - len;
122 while (str[i] == GetChar(text[ptr + i]))
129 ptr += GetShiftDistance (text[ptr + len]);
141 private void SetupShiftTable () {
142 shift = new Hashtable ();
145 for (int i = len ; i > 0; -- i)
148 shift[GetChar(c)] = i;
153 for (int i = 0; i < len; ++ i)
156 shift[GetChar(c)] = len - i;
162 private int GetShiftDistance (char c) {
167 return (s != null ? (int)s : len + 1);
170 private char GetChar(char c) {
171 return (!ignore ? c : Char.ToLower(c));
177 private bool reverse;
179 private Hashtable shift;
180 private readonly static int THRESHOLD = 5;