mscorlib(4.0.0.0) API with additions
RegexRunner.cs
2 
4 {
6  [EditorBrowsable(EditorBrowsableState.Never)]
7  public abstract class RegexRunner
8  {
10  protected internal int runtextbeg;
11 
13  protected internal int runtextend;
14 
16  protected internal int runtextstart;
17 
19  protected internal string runtext;
20 
22  protected internal int runtextpos;
23 
25  protected internal int[] runtrack;
26 
28  protected internal int runtrackpos;
29 
31  protected internal int[] runstack;
32 
34  protected internal int runstackpos;
35 
37  protected internal int[] runcrawl;
38 
40  protected internal int runcrawlpos;
41 
43  protected internal int runtrackcount;
44 
46  protected internal Match runmatch;
47 
49  protected internal Regex runregex;
50 
51  private int timeout;
52 
53  private bool ignoreTimeout;
54 
55  private int timeoutOccursAt;
56 
57  private const int TimeoutCheckFrequency = 1000;
58 
59  private int timeoutChecksToSkip;
60 
62  protected internal RegexRunner()
63  {
64  }
65 
76  protected internal Match Scan(Regex regex, string text, int textbeg, int textend, int textstart, int prevlen, bool quick)
77  {
78  return Scan(regex, text, textbeg, textend, textstart, prevlen, quick, regex.MatchTimeout);
79  }
80 
92  protected internal Match Scan(Regex regex, string text, int textbeg, int textend, int textstart, int prevlen, bool quick, TimeSpan timeout)
93  {
94  bool flag = false;
95  Regex.ValidateMatchTimeout(timeout);
96  ignoreTimeout = (Regex.InfiniteMatchTimeout == timeout);
97  this.timeout = (ignoreTimeout ? ((int)Regex.InfiniteMatchTimeout.TotalMilliseconds) : ((int)(timeout.TotalMilliseconds + 0.5)));
98  runregex = regex;
99  runtext = text;
100  runtextbeg = textbeg;
101  runtextend = textend;
102  runtextstart = textstart;
103  int num = (!runregex.RightToLeft) ? 1 : (-1);
104  int num2 = runregex.RightToLeft ? runtextbeg : runtextend;
105  runtextpos = textstart;
106  if (prevlen == 0)
107  {
108  if (runtextpos == num2)
109  {
110  return Match.Empty;
111  }
112  runtextpos += num;
113  }
114  StartTimeoutWatch();
115  while (true)
116  {
117  if (FindFirstChar())
118  {
119  CheckTimeout();
120  if (!flag)
121  {
122  InitMatch();
123  flag = true;
124  }
125  Go();
126  if (runmatch._matchcount[0] > 0)
127  {
128  return TidyMatch(quick);
129  }
130  runtrackpos = runtrack.Length;
131  runstackpos = runstack.Length;
132  runcrawlpos = runcrawl.Length;
133  }
134  if (runtextpos == num2)
135  {
136  break;
137  }
138  runtextpos += num;
139  }
140  TidyMatch(quick: true);
141  return Match.Empty;
142  }
143 
144  private void StartTimeoutWatch()
145  {
146  if (!ignoreTimeout)
147  {
148  timeoutChecksToSkip = 1000;
149  timeoutOccursAt = Environment.TickCount + timeout;
150  }
151  }
152 
154  protected void CheckTimeout()
155  {
156  if (!ignoreTimeout && --timeoutChecksToSkip == 0)
157  {
158  timeoutChecksToSkip = 1000;
159  DoCheckTimeout();
160  }
161  }
162 
163  private void DoCheckTimeout()
164  {
165  int tickCount = Environment.TickCount;
166  if (tickCount < timeoutOccursAt || (0 > timeoutOccursAt && 0 < tickCount))
167  {
168  return;
169  }
170  throw new RegexMatchTimeoutException(runtext, runregex.pattern, TimeSpan.FromMilliseconds(timeout));
171  }
172 
174  protected abstract void Go();
175 
178  protected abstract bool FindFirstChar();
179 
181  protected abstract void InitTrackCount();
182 
183  private void InitMatch()
184  {
185  if (runmatch == null)
186  {
187  if (runregex.caps != null)
188  {
190  }
191  else
192  {
194  }
195  }
196  else
197  {
199  }
200  if (runcrawl != null)
201  {
202  runtrackpos = runtrack.Length;
203  runstackpos = runstack.Length;
204  runcrawlpos = runcrawl.Length;
205  return;
206  }
207  InitTrackCount();
208  int num = runtrackcount * 8;
209  int num2 = runtrackcount * 8;
210  if (num < 32)
211  {
212  num = 32;
213  }
214  if (num2 < 16)
215  {
216  num2 = 16;
217  }
218  runtrack = new int[num];
219  runtrackpos = num;
220  runstack = new int[num2];
221  runstackpos = num2;
222  runcrawl = new int[32];
223  runcrawlpos = 32;
224  }
225 
226  private Match TidyMatch(bool quick)
227  {
228  if (!quick)
229  {
230  Match match = runmatch;
231  runmatch = null;
232  match.Tidy(runtextpos);
233  return match;
234  }
235  return null;
236  }
237 
239  protected void EnsureStorage()
240  {
241  if (runstackpos < runtrackcount * 4)
242  {
243  DoubleStack();
244  }
245  if (runtrackpos < runtrackcount * 4)
246  {
247  DoubleTrack();
248  }
249  }
250 
256  protected bool IsBoundary(int index, int startpos, int endpos)
257  {
258  return (index > startpos && RegexCharClass.IsWordChar(runtext[index - 1])) != (index < endpos && RegexCharClass.IsWordChar(runtext[index]));
259  }
260 
266  protected bool IsECMABoundary(int index, int startpos, int endpos)
267  {
268  return (index > startpos && RegexCharClass.IsECMAWordChar(runtext[index - 1])) != (index < endpos && RegexCharClass.IsECMAWordChar(runtext[index]));
269  }
270 
276  protected static bool CharInSet(char ch, string set, string category)
277  {
278  string set2 = RegexCharClass.ConvertOldStringsToClass(set, category);
279  return RegexCharClass.CharInClass(ch, set2);
280  }
281 
287  protected static bool CharInClass(char ch, string charClass)
288  {
289  return RegexCharClass.CharInClass(ch, charClass);
290  }
291 
293  protected void DoubleTrack()
294  {
295  int[] destinationArray = new int[runtrack.Length * 2];
296  Array.Copy(runtrack, 0, destinationArray, runtrack.Length, runtrack.Length);
297  runtrackpos += runtrack.Length;
298  runtrack = destinationArray;
299  }
300 
302  protected void DoubleStack()
303  {
304  int[] destinationArray = new int[runstack.Length * 2];
305  Array.Copy(runstack, 0, destinationArray, runstack.Length, runstack.Length);
306  runstackpos += runstack.Length;
307  runstack = destinationArray;
308  }
309 
311  protected void DoubleCrawl()
312  {
313  int[] destinationArray = new int[runcrawl.Length * 2];
314  Array.Copy(runcrawl, 0, destinationArray, runcrawl.Length, runcrawl.Length);
315  runcrawlpos += runcrawl.Length;
316  runcrawl = destinationArray;
317  }
318 
321  protected void Crawl(int i)
322  {
323  if (runcrawlpos == 0)
324  {
325  DoubleCrawl();
326  }
327  runcrawl[--runcrawlpos] = i;
328  }
329 
332  protected int Popcrawl()
333  {
334  return runcrawl[runcrawlpos++];
335  }
336 
339  protected int Crawlpos()
340  {
341  return runcrawl.Length - runcrawlpos;
342  }
343 
348  protected void Capture(int capnum, int start, int end)
349  {
350  if (end < start)
351  {
352  int num = end;
353  end = start;
354  start = num;
355  }
356  Crawl(capnum);
357  runmatch.AddMatch(capnum, start, end - start);
358  }
359 
365  protected void TransferCapture(int capnum, int uncapnum, int start, int end)
366  {
367  if (end < start)
368  {
369  int num = end;
370  end = start;
371  start = num;
372  }
373  int num2 = MatchIndex(uncapnum);
374  int num3 = num2 + MatchLength(uncapnum);
375  if (start >= num3)
376  {
377  end = start;
378  start = num3;
379  }
380  else if (end <= num2)
381  {
382  start = num2;
383  }
384  else
385  {
386  if (end > num3)
387  {
388  end = num3;
389  }
390  if (num2 > start)
391  {
392  start = num2;
393  }
394  }
395  Crawl(uncapnum);
396  runmatch.BalanceMatch(uncapnum);
397  if (capnum != -1)
398  {
399  Crawl(capnum);
400  runmatch.AddMatch(capnum, start, end - start);
401  }
402  }
403 
405  protected void Uncapture()
406  {
407  int cap = Popcrawl();
408  runmatch.RemoveMatch(cap);
409  }
410 
414  protected bool IsMatched(int cap)
415  {
416  return runmatch.IsMatched(cap);
417  }
418 
422  protected int MatchIndex(int cap)
423  {
424  return runmatch.MatchIndex(cap);
425  }
426 
430  protected int MatchLength(int cap)
431  {
432  return runmatch.MatchLength(cap);
433  }
434  }
435 }
int MatchLength(int cap)
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:430
bool IsECMABoundary(int index, int startpos, int endpos)
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:266
void Uncapture()
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:405
void EnsureStorage()
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:239
internal Regex runregex
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:49
void DoubleStack()
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:302
EditorBrowsableState
Specifies the browsable state of a property or method from within an editor.
internal Match runmatch
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:46
int MatchIndex(int cap)
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:422
Definition: __Canon.cs:3
internal Match Scan(Regex regex, string text, int textbeg, int textend, int textstart, int prevlen, bool quick)
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:76
double TotalMilliseconds
Gets the value of the current T:System.TimeSpan structure expressed in whole and fractional milliseco...
Definition: TimeSpan.cs:180
void CheckTimeout()
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:154
static internal void ValidateMatchTimeout(TimeSpan matchTimeout)
Checks whether a time-out interval is within an acceptable range.
Definition: Regex.cs:340
bool IsMatched(int cap)
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:414
static Match Empty
Gets the empty group. All failed matches return this empty match.
Definition: Match.cs:34
void TransferCapture(int capnum, int uncapnum, int start, int end)
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:365
static TimeSpan FromMilliseconds(double value)
Returns a T:System.TimeSpan that represents a specified number of milliseconds.
Definition: TimeSpan.cs:477
internal int [] runstack
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:31
internal int capsize
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: Regex.cs:57
internal int runtextpos
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:22
void Crawl(int i)
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:321
void Capture(int capnum, int start, int end)
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:348
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
bool IsBoundary(int index, int startpos, int endpos)
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:256
abstract void Go()
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
int Crawlpos()
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:339
internal int runtrackpos
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:28
internal int runtrackcount
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:43
internal Hashtable caps
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: Regex.cs:48
internal int runstackpos
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:34
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
static int TickCount
Gets the number of milliseconds elapsed since the system started.
Definition: Environment.cs:306
internal int [] runcrawl
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:37
static bool CharInSet(char ch, string set, string category)
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:276
Represents the results from a single regular expression match.
Definition: Match.cs:8
bool RightToLeft
Gets a value that indicates whether the regular expression searches from right to left.
Definition: Regex.cs:182
internal int runcrawlpos
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:40
void DoubleTrack()
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:293
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
abstract void InitTrackCount()
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
internal int runtextend
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:13
internal string runtext
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:19
internal int runtextstart
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:16
static bool CharInClass(char ch, string charClass)
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:287
Represents a time interval.To browse the .NET Framework source code for this type,...
Definition: TimeSpan.cs:12
abstract bool FindFirstChar()
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
TimeSpan MatchTimeout
Gets the time-out interval of the current instance.
Definition: Regex.cs:169
internal Match Scan(Regex regex, string text, int textbeg, int textend, int textstart, int prevlen, bool quick, TimeSpan timeout)
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:92
internal int [] runtrack
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:25
internal int runtextbeg
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:10
The T:System.Text.RegularExpressions.RegexRunner class is the base class for compiled regular express...
Definition: RegexRunner.cs:7
Represents an immutable regular expression.To browse the .NET Framework source code for this type,...
Definition: Regex.cs:16
internal RegexRunner()
Initializes a new instance of the T:System.Text.RegularExpressions.RegexRunner class.
Definition: RegexRunner.cs:62
internal string pattern
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: Regex.cs:19
int Popcrawl()
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:332
static readonly TimeSpan InfiniteMatchTimeout
Specifies that a pattern-matching operation should not time out.
Definition: Regex.cs:33
void DoubleCrawl()
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: RegexRunner.cs:311