Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
RegexRunner.cs
Go to the documentation of this file.
2
3public abstract class RegexRunner
4{
5 protected internal int runtextbeg;
6
7 protected internal int runtextend;
8
9 protected internal int runtextstart;
10
11 protected internal string? runtext;
12
13 protected internal int runtextpos;
14
15 protected internal int[]? runtrack;
16
17 protected internal int runtrackpos;
18
19 protected internal int[]? runstack;
20
21 protected internal int runstackpos;
22
23 protected internal int[]? runcrawl;
24
25 protected internal int runcrawlpos;
26
27 protected internal int runtrackcount;
28
29 protected internal Match? runmatch;
30
31 protected internal Regex? runregex;
32
33 private int _timeout;
34
35 private bool _ignoreTimeout;
36
37 private int _timeoutOccursAt;
38
39 private const int TimeoutCheckFrequency = 1000;
40
42
43 protected internal RegexRunner()
44 {
45 }
46
47 protected internal Match? Scan(Regex regex, string text, int textbeg, int textend, int textstart, int prevlen, bool quick)
48 {
49 return Scan(regex, text, textbeg, textend, textstart, prevlen, quick, regex.MatchTimeout);
50 }
51
52 protected internal Match? Scan(Regex regex, string text, int textbeg, int textend, int textstart, int prevlen, bool quick, TimeSpan timeout)
53 {
54 _timeout = -1;
55 bool flag = (_ignoreTimeout = Regex.InfiniteMatchTimeout == timeout);
56 if (!flag)
57 {
59 _timeout = (int)(timeout.TotalMilliseconds + 0.5);
60 _timeoutOccursAt = Environment.TickCount + _timeout;
62 }
63 int num = 1;
64 int num2 = textend;
65 if (regex.RightToLeft)
66 {
67 num = -1;
68 num2 = textbeg;
69 }
70 runtextpos = textstart;
71 if (prevlen == 0)
72 {
73 if (textstart == num2)
74 {
75 return Match.Empty;
76 }
77 runtextpos += num;
78 }
79 runregex = regex;
80 runtext = text;
81 runtextstart = textstart;
82 runtextbeg = textbeg;
83 runtextend = textend;
84 bool flag2 = false;
85 while (true)
86 {
87 if (FindFirstChar())
88 {
89 if (!flag)
90 {
92 }
93 if (!flag2)
94 {
96 flag2 = true;
97 }
98 Go();
100 if (match._matchcount[0] > 0)
101 {
102 runtext = null;
103 if (quick)
104 {
105 runmatch.Text = null;
106 return null;
107 }
108 runmatch = null;
110 return match;
111 }
112 runtrackpos = runtrack.Length;
113 runstackpos = runstack.Length;
114 runcrawlpos = runcrawl.Length;
115 }
116 if (runtextpos == num2)
117 {
118 break;
119 }
120 runtextpos += num;
121 }
122 runtext = null;
123 if (runmatch != null)
124 {
125 runmatch.Text = null;
126 }
127 return Match.Empty;
128 }
129
130 internal void Scan<TState>(Regex regex, string text, int textstart, ref TState state, MatchCallback<TState> callback, bool reuseMatchObject, TimeSpan timeout)
131 {
132 _timeout = -1;
133 bool flag = (_ignoreTimeout = Regex.InfiniteMatchTimeout == timeout);
134 if (!flag)
135 {
136 _timeout = (int)(timeout.TotalMilliseconds + 0.5);
137 _timeoutOccursAt = Environment.TickCount + _timeout;
139 }
140 int num = 1;
141 int num2 = text.Length;
142 if (regex.RightToLeft)
143 {
144 num = -1;
145 num2 = 0;
146 }
147 runregex = regex;
148 runtextstart = (runtextpos = textstart);
149 runtext = text;
150 runtextend = text.Length;
151 runtextbeg = 0;
152 bool flag2 = false;
153 while (true)
154 {
155 if (FindFirstChar())
156 {
157 if (!flag)
158 {
160 }
161 if (!flag2)
162 {
164 flag2 = true;
165 }
166 Go();
168 if (match._matchcount[0] > 0)
169 {
170 if (!reuseMatchObject)
171 {
172 runmatch = null;
173 }
175 flag2 = false;
176 if (!callback(ref state, match))
177 {
178 runtext = null;
179 if (reuseMatchObject)
180 {
181 match.Text = null;
182 }
183 return;
184 }
186 runtrackpos = runtrack.Length;
187 runstackpos = runstack.Length;
188 runcrawlpos = runcrawl.Length;
189 if (match.Length != 0)
190 {
191 continue;
192 }
193 if (runtextpos == num2)
194 {
195 runtext = null;
196 if (reuseMatchObject)
197 {
198 match.Text = null;
199 }
200 return;
201 }
202 runtextpos += num;
203 continue;
204 }
205 runtrackpos = runtrack.Length;
206 runstackpos = runstack.Length;
207 runcrawlpos = runcrawl.Length;
208 }
209 if (runtextpos == num2)
210 {
211 break;
212 }
213 runtextpos += num;
214 }
215 runtext = null;
216 if (runmatch != null)
217 {
218 runmatch.Text = null;
219 }
220 }
221
222 protected void CheckTimeout()
223 {
224 if (!_ignoreTimeout)
225 {
227 }
228 }
229
230 private void DoCheckTimeout()
231 {
232 if (--_timeoutChecksToSkip == 0)
233 {
235 int tickCount = Environment.TickCount;
236 if (tickCount >= _timeoutOccursAt && (0 <= _timeoutOccursAt || 0 >= tickCount))
237 {
239 }
240 }
241 }
242
243 protected abstract void Go();
244
245 protected abstract bool FindFirstChar();
246
247 protected abstract void InitTrackCount();
248
249 private void InitializeForGo()
250 {
251 if (runmatch == null)
252 {
254 }
255 else
256 {
258 }
259 if (runcrawl != null)
260 {
261 runtrackpos = runtrack.Length;
262 runstackpos = runstack.Length;
263 runcrawlpos = runcrawl.Length;
264 return;
265 }
267 int num;
268 int num2 = (num = runtrackcount * 8);
269 if (num2 < 32)
270 {
271 num2 = 32;
272 }
273 if (num < 16)
274 {
275 num = 16;
276 }
277 runtrack = new int[num2];
278 runtrackpos = num2;
279 runstack = new int[num];
280 runstackpos = num;
281 runcrawl = new int[32];
282 runcrawlpos = 32;
283 }
284
285 protected void EnsureStorage()
286 {
287 int num = runtrackcount * 4;
288 if (runstackpos < num)
289 {
290 DoubleStack();
291 }
292 if (runtrackpos < num)
293 {
294 DoubleTrack();
295 }
296 }
297
298 protected bool IsBoundary(int index, int startpos, int endpos)
299 {
300 return (index > startpos && RegexCharClass.IsWordChar(runtext[index - 1])) != (index < endpos && RegexCharClass.IsWordChar(runtext[index]));
301 }
302
303 protected bool IsECMABoundary(int index, int startpos, int endpos)
304 {
305 return (index > startpos && RegexCharClass.IsECMAWordChar(runtext[index - 1])) != (index < endpos && RegexCharClass.IsECMAWordChar(runtext[index]));
306 }
307
308 protected static bool CharInSet(char ch, string set, string category)
309 {
310 string set2 = RegexCharClass.ConvertOldStringsToClass(set, category);
311 return RegexCharClass.CharInClass(ch, set2);
312 }
313
314 protected static bool CharInClass(char ch, string charClass)
315 {
316 return RegexCharClass.CharInClass(ch, charClass);
317 }
318
319 protected void DoubleTrack()
320 {
321 int[] destinationArray = new int[runtrack.Length * 2];
323 runtrackpos += runtrack.Length;
325 }
326
327 protected void DoubleStack()
328 {
329 int[] destinationArray = new int[runstack.Length * 2];
331 runstackpos += runstack.Length;
333 }
334
335 protected void DoubleCrawl()
336 {
337 int[] destinationArray = new int[runcrawl.Length * 2];
339 runcrawlpos += runcrawl.Length;
341 }
342
343 protected void Crawl(int i)
344 {
345 if (runcrawlpos == 0)
346 {
347 DoubleCrawl();
348 }
350 }
351
352 protected int Popcrawl()
353 {
354 return runcrawl[runcrawlpos++];
355 }
356
357 protected int Crawlpos()
358 {
359 return runcrawl.Length - runcrawlpos;
360 }
361
362 protected void Capture(int capnum, int start, int end)
363 {
364 if (end < start)
365 {
366 int num = end;
367 end = start;
368 start = num;
369 }
370 Crawl(capnum);
371 runmatch.AddMatch(capnum, start, end - start);
372 }
373
374 protected void TransferCapture(int capnum, int uncapnum, int start, int end)
375 {
376 if (end < start)
377 {
378 int num = end;
379 end = start;
380 start = num;
381 }
382 int num2 = MatchIndex(uncapnum);
383 int num3 = num2 + MatchLength(uncapnum);
384 if (start >= num3)
385 {
386 end = start;
387 start = num3;
388 }
389 else if (end <= num2)
390 {
391 start = num2;
392 }
393 else
394 {
395 if (end > num3)
396 {
397 end = num3;
398 }
399 if (num2 > start)
400 {
401 start = num2;
402 }
403 }
404 Crawl(uncapnum);
405 runmatch.BalanceMatch(uncapnum);
406 if (capnum != -1)
407 {
408 Crawl(capnum);
409 runmatch.AddMatch(capnum, start, end - start);
410 }
411 }
412
413 protected void Uncapture()
414 {
415 int cap = Popcrawl();
417 }
418
419 protected bool IsMatched(int cap)
420 {
421 return runmatch.IsMatched(cap);
422 }
423
424 protected int MatchIndex(int cap)
425 {
426 return runmatch.MatchIndex(cap);
427 }
428
429 protected int MatchLength(int cap)
430 {
431 return runmatch.MatchLength(cap);
432 }
433}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
static int TickCount
void Reset(Regex regex, string text, int textbeg, int textend, int textstart)
Definition Match.cs:41
void AddMatch(int cap, int start, int len)
Definition Match.cs:114
static string ConvertOldStringsToClass(string set, string category)
static bool CharInClass(char ch, string set, ref int[] asciiResultCache)
static bool CharInClass(char ch, string charClass)
void Scan< TState >(Regex regex, string text, int textstart, ref TState state, MatchCallback< TState > callback, bool reuseMatchObject, TimeSpan timeout)
void TransferCapture(int capnum, int uncapnum, int start, int end)
bool IsECMABoundary(int index, int startpos, int endpos)
static bool CharInSet(char ch, string set, string category)
bool IsBoundary(int index, int startpos, int endpos)
Match? Scan(Regex regex, string text, int textbeg, int textend, int textstart, int prevlen, bool quick)
void Capture(int capnum, int start, int end)
Match? Scan(Regex regex, string text, int textbeg, int textend, int textstart, int prevlen, bool quick, TimeSpan timeout)
static void ValidateMatchTimeout(TimeSpan matchTimeout)
Definition Regex.cs:169
delegate bool MatchCallback< TState >(ref TState state, Match match)
static TimeSpan FromMilliseconds(double value)
Definition TimeSpan.cs:228