Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Regex.cs
Go to the documentation of this file.
10
12
13public class Regex : ISerializable
14{
15 protected internal string? pattern;
16
17 protected internal RegexOptions roptions;
18
19 protected internal RegexRunnerFactory? factory;
20
21 protected internal Hashtable? caps;
22
23 protected internal Hashtable? capnames;
24
25 protected internal string[]? capslist;
26
27 protected internal int capsize;
28
30
31 private volatile RegexRunner _runner;
32
34
35 private bool _refsInitialized;
36
38
40
41 protected internal TimeSpan internalMatchTimeout;
42
43 [CLSCompliant(false)]
44 protected IDictionary? Caps
45 {
46 get
47 {
48 return caps;
49 }
51 set
52 {
53 if (value == null)
54 {
56 }
58 }
59 }
60
61 [CLSCompliant(false)]
63 {
64 get
65 {
66 return capnames;
67 }
69 set
70 {
71 if (value == null)
72 {
74 }
76 }
77 }
78
80
81 public bool RightToLeft => UseOptionR();
82
83 public static int CacheSize
84 {
85 get
86 {
88 }
89 set
90 {
91 if (value < 0)
92 {
94 }
96 }
97 }
98
100
101 protected Regex()
102 {
104 }
105
106 public Regex(string pattern)
107 : this(pattern, null)
108 {
109 }
110
115
120
125
135
152
153 internal static void ValidatePattern(string pattern)
154 {
155 if (pattern == null)
156 {
158 }
159 }
160
162 {
163 if ((uint)options >> 10 != 0 || ((options & RegexOptions.ECMAScript) != 0 && ((uint)options & 0xFFFFFCF4u) != 0))
164 {
166 }
167 }
168
169 protected internal static void ValidateMatchTimeout(TimeSpan matchTimeout)
170 {
171 long ticks = matchTimeout.Ticks;
172 if (ticks != -10000 && (ulong)(ticks - 1) >= 21474836460000uL)
173 {
175 }
176 }
177
179 {
181 }
182
187
188 [MethodImpl(MethodImplOptions.NoInlining)]
193
198
203
216
217 public static string Escape(string str)
218 {
219 if (str == null)
220 {
222 }
223 return RegexParser.Escape(str);
224 }
225
226 public static string Unescape(string str)
227 {
228 if (str == null)
229 {
231 }
232 return RegexParser.Unescape(str);
233 }
234
235 public override string ToString()
236 {
237 return pattern;
238 }
239
240 public string[] GetGroupNames()
241 {
242 string[] array;
243 if (capslist == null)
244 {
245 array = new string[capsize];
246 for (int i = 0; i < array.Length; i++)
247 {
248 string[] array2 = array;
249 int num = i;
250 uint num2 = (uint)i;
251 array2[num] = num2.ToString();
252 }
253 }
254 else
255 {
256 array = capslist.AsSpan().ToArray();
257 }
258 return array;
259 }
260
261 public int[] GetGroupNumbers()
262 {
263 int[] array;
264 if (caps == null)
265 {
266 array = new int[capsize];
267 for (int i = 0; i < array.Length; i++)
268 {
269 array[i] = i;
270 }
271 }
272 else
273 {
274 array = new int[caps.Count];
276 while (enumerator.MoveNext())
277 {
278 array[(int)enumerator.Value] = (int)enumerator.Key;
279 }
280 }
281 return array;
282 }
283
284 public string GroupNameFromNumber(int i)
285 {
286 if (capslist == null)
287 {
288 if ((uint)i >= (uint)capsize)
289 {
290 return string.Empty;
291 }
292 uint num = (uint)i;
293 return num.ToString();
294 }
295 if (caps == null || caps.TryGetValue<int>(i, out i))
296 {
297 if ((uint)i >= (uint)capslist.Length)
298 {
299 return string.Empty;
300 }
301 return capslist[i];
302 }
303 return string.Empty;
304 }
305
306 public int GroupNumberFromName(string name)
307 {
308 if (name == null)
309 {
311 }
312 if (capnames != null)
313 {
314 if (!capnames.TryGetValue<int>(name, out var value))
315 {
316 return -1;
317 }
318 return value;
319 }
320 if (!uint.TryParse(name, NumberStyles.None, CultureInfo.InvariantCulture, out var result) || result >= capsize)
321 {
322 return -1;
323 }
324 return (int)result;
325 }
326
327 protected void InitializeReferences()
328 {
330 {
332 }
334 _refsInitialized = true;
335 }
336
337 internal Match Run(bool quick, int prevlen, string input, int beginning, int length, int startat)
338 {
339 if ((uint)startat > (uint)input.Length)
340 {
342 }
343 if ((uint)length > (uint)input.Length)
344 {
346 }
348 try
349 {
351 }
352 finally
353 {
355 }
356 }
357
358 internal void Run<TState>(string input, int startat, ref TState state, MatchCallback<TState> callback, bool reuseMatchObject)
359 {
361 try
362 {
364 }
365 finally
366 {
368 }
369 }
370
371 [MethodImpl(MethodImplOptions.AggressiveInlining)]
373 {
375 if (regexRunner == null)
376 {
377 if (factory == null)
378 {
380 }
382 }
383 return regexRunner;
384 }
385
387 {
388 _runner = runner;
389 }
390
391 protected bool UseOptionC()
392 {
393 return (roptions & RegexOptions.Compiled) != 0;
394 }
395
396 protected internal bool UseOptionR()
397 {
398 return (roptions & RegexOptions.RightToLeft) != 0;
399 }
400
401 internal bool UseOptionInvariant()
402 {
403 return (roptions & RegexOptions.CultureInvariant) != 0;
404 }
405
406 public static bool IsMatch(string input, string pattern)
407 {
408 return RegexCache.GetOrAdd(pattern).IsMatch(input);
409 }
410
411 public static bool IsMatch(string input, string pattern, RegexOptions options)
412 {
414 }
415
416 public static bool IsMatch(string input, string pattern, RegexOptions options, TimeSpan matchTimeout)
417 {
419 }
420
421 public bool IsMatch(string input)
422 {
423 if (input == null)
424 {
426 }
427 return Run(quick: true, -1, input, 0, input.Length, UseOptionR() ? input.Length : 0) == null;
428 }
429
430 public bool IsMatch(string input, int startat)
431 {
432 if (input == null)
433 {
435 }
436 return Run(quick: true, -1, input, 0, input.Length, startat) == null;
437 }
438
439 public static Match Match(string input, string pattern)
440 {
441 return RegexCache.GetOrAdd(pattern).Match(input);
442 }
443
444 public static Match Match(string input, string pattern, RegexOptions options)
445 {
447 }
448
450 {
452 }
453
454 public Match Match(string input)
455 {
456 if (input == null)
457 {
459 }
460 return Run(quick: false, -1, input, 0, input.Length, UseOptionR() ? input.Length : 0);
461 }
462
463 public Match Match(string input, int startat)
464 {
465 if (input == null)
466 {
468 }
469 return Run(quick: false, -1, input, 0, input.Length, startat);
470 }
471
472 public Match Match(string input, int beginning, int length)
473 {
474 if (input == null)
475 {
477 }
478 return Run(quick: false, -1, input, beginning, length, UseOptionR() ? (beginning + length) : beginning);
479 }
480
481 public static MatchCollection Matches(string input, string pattern)
482 {
483 return RegexCache.GetOrAdd(pattern).Matches(input);
484 }
485
487 {
489 }
490
495
497 {
498 if (input == null)
499 {
501 }
502 return new MatchCollection(this, input, UseOptionR() ? input.Length : 0);
503 }
504
506 {
507 if (input == null)
508 {
510 }
511 return new MatchCollection(this, input, startat);
512 }
513
514 public static string Replace(string input, string pattern, string replacement)
515 {
516 return RegexCache.GetOrAdd(pattern).Replace(input, replacement);
517 }
518
519 public static string Replace(string input, string pattern, string replacement, RegexOptions options)
520 {
522 }
523
524 public static string Replace(string input, string pattern, string replacement, RegexOptions options, TimeSpan matchTimeout)
525 {
527 }
528
529 public string Replace(string input, string replacement)
530 {
531 if (input == null)
532 {
534 }
535 return Replace(input, replacement, -1, UseOptionR() ? input.Length : 0);
536 }
537
538 public string Replace(string input, string replacement, int count)
539 {
540 if (input == null)
541 {
543 }
544 return Replace(input, replacement, count, UseOptionR() ? input.Length : 0);
545 }
546
547 public string Replace(string input, string replacement, int count, int startat)
548 {
549 if (input == null)
550 {
552 }
553 if (replacement == null)
554 {
556 }
558 }
559
560 public static string Replace(string input, string pattern, MatchEvaluator evaluator)
561 {
562 return RegexCache.GetOrAdd(pattern).Replace(input, evaluator);
563 }
564
565 public static string Replace(string input, string pattern, MatchEvaluator evaluator, RegexOptions options)
566 {
568 }
569
574
575 public string Replace(string input, MatchEvaluator evaluator)
576 {
577 if (input == null)
578 {
580 }
581 return Replace(evaluator, this, input, -1, UseOptionR() ? input.Length : 0);
582 }
583
584 public string Replace(string input, MatchEvaluator evaluator, int count)
585 {
586 if (input == null)
587 {
589 }
590 return Replace(evaluator, this, input, count, UseOptionR() ? input.Length : 0);
591 }
592
593 public string Replace(string input, MatchEvaluator evaluator, int count, int startat)
594 {
595 if (input == null)
596 {
598 }
599 return Replace(evaluator, this, input, count, startat);
600 }
601
602 private static string Replace(MatchEvaluator evaluator, Regex regex, string input, int count, int startat)
603 {
604 if (evaluator == null)
605 {
607 }
608 if (count < -1)
609 {
611 }
612 if ((uint)startat > (uint)input.Length)
613 {
615 }
616 if (count == 0)
617 {
618 return input;
619 }
621 if (!regex.RightToLeft)
622 {
624 {
625 state.segments.Add(state.input.AsMemory(state.prevat, match.Index - state.prevat));
626 state.prevat = match.Index + match.Length;
627 state.segments.Add(state.evaluator(match).AsMemory());
628 return --state.count != 0;
629 }, reuseMatchObject: false);
630 if (state2.Item1.Count == 0)
631 {
632 return input;
633 }
634 state2.Item1.Add(input.AsMemory(state2.Item3, input.Length - state2.Item3));
635 }
636 else
637 {
638 state2.Item3 = input.Length;
640 {
641 state.segments.Add(state.input.AsMemory(match.Index + match.Length, state.prevat - match.Index - match.Length));
642 state.prevat = match.Index;
643 state.segments.Add(state.evaluator(match).AsMemory());
644 return --state.count != 0;
645 }, reuseMatchObject: false);
646 if (state2.Item1.Count == 0)
647 {
648 return input;
649 }
650 state2.Item1.Add(input.AsMemory(0, state2.Item3));
651 state2.Item1.AsSpan().Reverse();
652 }
653 return state2.Item1.ToString();
654 }
655
656 public static string[] Split(string input, string pattern)
657 {
658 return RegexCache.GetOrAdd(pattern).Split(input);
659 }
660
661 public static string[] Split(string input, string pattern, RegexOptions options)
662 {
664 }
665
666 public static string[] Split(string input, string pattern, RegexOptions options, TimeSpan matchTimeout)
667 {
669 }
670
671 public string[] Split(string input)
672 {
673 if (input == null)
674 {
676 }
677 return Split(this, input, 0, UseOptionR() ? input.Length : 0);
678 }
679
680 public string[] Split(string input, int count)
681 {
682 if (input == null)
683 {
685 }
686 return Split(this, input, count, UseOptionR() ? input.Length : 0);
687 }
688
689 public string[] Split(string input, int count, int startat)
690 {
691 if (input == null)
692 {
694 }
695 return Split(this, input, count, startat);
696 }
697
698 private static string[] Split(Regex regex, string input, int count, int startat)
699 {
700 if (count < 0)
701 {
703 }
704 if ((uint)startat > (uint)input.Length)
705 {
707 }
708 if (count == 1)
709 {
710 return new string[1] { input };
711 }
712 count--;
713 (List<string>, int, string, int) state2 = (new List<string>(), 0, input, count);
714 if (!regex.RightToLeft)
715 {
717 {
718 state.results.Add(state.input.Substring(state.prevat, match.Index - state.prevat));
719 state.prevat = match.Index + match.Length;
720 for (int j = 1; j < match.Groups.Count; j++)
721 {
722 if (match.IsMatched(j))
723 {
724 state.results.Add(match.Groups[j].ToString());
725 }
726 }
727 return --state.count != 0;
728 }, reuseMatchObject: true);
729 if (state2.Item1.Count == 0)
730 {
731 return new string[1] { input };
732 }
733 state2.Item1.Add(input.Substring(state2.Item2, input.Length - state2.Item2));
734 }
735 else
736 {
737 state2.Item2 = input.Length;
739 {
740 state.results.Add(state.input.Substring(match.Index + match.Length, state.prevat - match.Index - match.Length));
741 state.prevat = match.Index;
742 for (int i = 1; i < match.Groups.Count; i++)
743 {
744 if (match.IsMatched(i))
745 {
746 state.results.Add(match.Groups[i].ToString());
747 }
748 }
749 return --state.count != 0;
750 }, reuseMatchObject: true);
751 if (state2.Item1.Count == 0)
752 {
753 return new string[1] { input };
754 }
755 state2.Item1.Add(input.Substring(0, state2.Item2));
756 state2.Item1.Reverse(0, state2.Item1.Count);
757 }
758 return state2.Item1.ToArray();
759 }
760
762 {
764 object data = currentDomain.GetData("REGEX_DEFAULT_MATCH_TIMEOUT");
765 if (data == null)
766 {
768 }
769 if (data is TimeSpan timeSpan)
770 {
771 try
772 {
774 return timeSpan;
775 }
777 {
779 }
780 }
781 throw new InvalidCastException(System.SR.Format(System.SR.IllegalDefaultRegexMatchTimeoutInAppDomain, "REGEX_DEFAULT_MATCH_TIMEOUT", data));
782 }
783}
static AppDomain CurrentDomain
Definition AppDomain.cs:28
void Add(TKey key, TValue value)
IEnumerator IEnumerable. GetEnumerator()
Definition Hashtable.cs:899
static CultureInfo CurrentCulture
static CultureInfo InvariantCulture
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string IllegalDefaultRegexMatchTimeoutInAppDomain
Definition SR.cs:34
static string PlatformNotSupported_CompileToAssembly
Definition SR.cs:70
Definition SR.cs:7
static Regex GetOrAdd(string pattern)
static RegexRunnerFactory Compile(string pattern, RegexCode code, RegexOptions options, bool hasTimeout)
static RegexReplacement GetOrCreate(WeakReference< RegexReplacement > replRef, string replacement, Hashtable caps, int capsize, Hashtable capnames, RegexOptions roptions)
static void CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname, CustomAttributeBuilder[]? attributes, string? resourceFile)
Definition Regex.cs:204
Match Match(string input)
Definition Regex.cs:454
volatile RegexRunner _runner
Definition Regex.cs:31
Match Match(string input, int beginning, int length)
Definition Regex.cs:472
static Match Match(string input, string pattern, RegexOptions options)
Definition Regex.cs:444
static bool IsMatch(string input, string pattern)
Definition Regex.cs:406
static string Replace(string input, string pattern, string replacement)
Definition Regex.cs:514
static string Escape(string str)
Definition Regex.cs:217
static string Replace(MatchEvaluator evaluator, Regex regex, string input, int count, int startat)
Definition Regex.cs:602
static string Replace(string input, string pattern, MatchEvaluator evaluator, RegexOptions options)
Definition Regex.cs:565
string Replace(string input, string replacement)
Definition Regex.cs:529
Regex(string pattern, RegexOptions options, TimeSpan matchTimeout, CultureInfo culture)
Definition Regex.cs:126
RegexRunnerFactory? factory
Definition Regex.cs:19
string Replace(string input, string replacement, int count)
Definition Regex.cs:538
string[] Split(string input, int count, int startat)
Definition Regex.cs:689
static string[] Split(Regex regex, string input, int count, int startat)
Definition Regex.cs:698
static readonly TimeSpan InfiniteMatchTimeout
Definition Regex.cs:37
bool IsMatch(string input, int startat)
Definition Regex.cs:430
static MatchCollection Matches(string input, string pattern)
Definition Regex.cs:481
Match Match(string input, int startat)
Definition Regex.cs:463
Regex(SerializationInfo info, StreamingContext context)
Definition Regex.cs:178
static string[] Split(string input, string pattern)
Definition Regex.cs:656
static void CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname, CustomAttributeBuilder[]? attributes)
Definition Regex.cs:199
static void ValidateOptions(RegexOptions options)
Definition Regex.cs:161
static MatchCollection Matches(string input, string pattern, RegexOptions options, TimeSpan matchTimeout)
Definition Regex.cs:491
string Replace(string input, MatchEvaluator evaluator, int count, int startat)
Definition Regex.cs:593
static void ValidatePattern(string pattern)
Definition Regex.cs:153
static string Replace(string input, string pattern, MatchEvaluator evaluator)
Definition Regex.cs:560
string[] Split(string input)
Definition Regex.cs:671
static MatchCollection Matches(string input, string pattern, RegexOptions options)
Definition Regex.cs:486
Match Run(bool quick, int prevlen, string input, int beginning, int length, int startat)
Definition Regex.cs:337
static readonly TimeSpan s_defaultMatchTimeout
Definition Regex.cs:39
static string Replace(string input, string pattern, MatchEvaluator evaluator, RegexOptions options, TimeSpan matchTimeout)
Definition Regex.cs:570
static bool IsMatch(string input, string pattern, RegexOptions options, TimeSpan matchTimeout)
Definition Regex.cs:416
MatchCollection Matches(string input)
Definition Regex.cs:496
Regex(string pattern, RegexOptions options, TimeSpan matchTimeout)
Definition Regex.cs:116
void Run< TState >(string input, int startat, ref TState state, MatchCallback< TState > callback, bool reuseMatchObject)
Definition Regex.cs:358
void Init(string pattern, RegexOptions options, TimeSpan matchTimeout, CultureInfo culture)
Definition Regex.cs:136
WeakReference< RegexReplacement > _replref
Definition Regex.cs:29
static void CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname)
Definition Regex.cs:194
static Match Match(string input, string pattern)
Definition Regex.cs:439
static void ValidateMatchTimeout(TimeSpan matchTimeout)
Definition Regex.cs:169
static Match Match(string input, string pattern, RegexOptions options, TimeSpan matchTimeout)
Definition Regex.cs:449
string Replace(string input, MatchEvaluator evaluator)
Definition Regex.cs:575
static TimeSpan InitDefaultMatchTimeout()
Definition Regex.cs:761
static string Replace(string input, string pattern, string replacement, RegexOptions options)
Definition Regex.cs:519
int GroupNumberFromName(string name)
Definition Regex.cs:306
string[] Split(string input, int count)
Definition Regex.cs:680
static string Unescape(string str)
Definition Regex.cs:226
static RegexRunnerFactory Compile(string pattern, RegexCode code, RegexOptions options, bool hasTimeout)
Definition Regex.cs:189
static bool IsMatch(string input, string pattern, RegexOptions options)
Definition Regex.cs:411
static string[] Split(string input, string pattern, RegexOptions options)
Definition Regex.cs:661
Regex(string pattern, RegexOptions options)
Definition Regex.cs:111
static string Replace(string input, string pattern, string replacement, RegexOptions options, TimeSpan matchTimeout)
Definition Regex.cs:524
static string[] Split(string input, string pattern, RegexOptions options, TimeSpan matchTimeout)
Definition Regex.cs:666
Regex(string pattern, CultureInfo culture)
Definition Regex.cs:121
string Replace(string input, MatchEvaluator evaluator, int count)
Definition Regex.cs:584
void ReturnRunner(RegexRunner runner)
Definition Regex.cs:386
string Replace(string input, string replacement, int count, int startat)
Definition Regex.cs:547
MatchCollection Matches(string input, int startat)
Definition Regex.cs:505
static void ThrowNotSupportedException(ExceptionResource resource)
static void ThrowArgumentNullException(ExceptionArgument arg)
static void ThrowArgumentOutOfRangeException(ExceptionArgument arg)
static int Exchange(ref int location1, int value)
static readonly TimeSpan InfiniteTimeSpan
Definition Timeout.cs:5
void GetObjectData(SerializationInfo info, StreamingContext context)
delegate string MatchEvaluator(Match match)
static RegexTree Parse(string pattern, RegexOptions options, CultureInfo culture)
static string Escape(string input)
static string Unescape(string input)
static RegexCode Write(RegexTree tree)
static SegmentStringBuilder Create()