mscorlib(4.0.0.0) API with additions
Regex.cs
1 using System.Collections;
4 using System.Reflection;
9 using System.Threading;
10 
12 {
14  [Serializable]
15  [global::__DynamicallyInvokable]
16  public class Regex : ISerializable
17  {
19  protected internal string pattern;
20 
22  protected internal RegexRunnerFactory factory;
23 
25  protected internal RegexOptions roptions;
26 
27  [NonSerialized]
28  private static readonly TimeSpan MaximumMatchTimeout = TimeSpan.FromMilliseconds(2147483646.0);
29 
31  [NonSerialized]
32  [global::__DynamicallyInvokable]
34 
36  [OptionalField(VersionAdded = 2)]
37  protected internal TimeSpan internalMatchTimeout;
38 
39  private const string DefaultMatchTimeout_ConfigKeyName = "REGEX_DEFAULT_MATCH_TIMEOUT";
40 
41  [NonSerialized]
42  internal static readonly TimeSpan FallbackDefaultMatchTimeout = InfiniteMatchTimeout;
43 
44  [NonSerialized]
45  internal static readonly TimeSpan DefaultMatchTimeout = InitDefaultMatchTimeout();
46 
48  protected internal Hashtable caps;
49 
51  protected internal Hashtable capnames;
52 
54  protected internal string[] capslist;
55 
57  protected internal int capsize;
58 
59  internal ExclusiveReference runnerref;
60 
61  internal SharedReference replref;
62 
63  internal RegexCode code;
64 
65  internal bool refsInitialized;
66 
67  internal static LinkedList<CachedCodeEntry> livecode = new LinkedList<CachedCodeEntry>();
68 
69  internal static int cacheSize = 15;
70 
71  internal const int MaxOptionShift = 10;
72 
76  [CLSCompliant(false)]
77  protected IDictionary Caps
78  {
79  get
80  {
81  return caps;
82  }
83  set
84  {
85  if (value == null)
86  {
87  throw new ArgumentNullException("value");
88  }
89  caps = (value as Hashtable);
90  if (caps == null)
91  {
92  caps = new Hashtable(value);
93  }
94  }
95  }
96 
100  [CLSCompliant(false)]
101  protected IDictionary CapNames
102  {
103  get
104  {
105  return capnames;
106  }
107  set
108  {
109  if (value == null)
110  {
111  throw new ArgumentNullException("value");
112  }
113  capnames = (value as Hashtable);
114  if (capnames == null)
115  {
116  capnames = new Hashtable(value);
117  }
118  }
119  }
120 
124  [global::__DynamicallyInvokable]
125  public static int CacheSize
126  {
127  [global::__DynamicallyInvokable]
128  get
129  {
130  return cacheSize;
131  }
132  [global::__DynamicallyInvokable]
133  set
134  {
135  if (value < 0)
136  {
137  throw new ArgumentOutOfRangeException("value");
138  }
139  cacheSize = value;
140  if (livecode.Count > cacheSize)
141  {
142  lock (livecode)
143  {
144  while (livecode.Count > cacheSize)
145  {
146  livecode.RemoveLast();
147  }
148  }
149  }
150  }
151  }
152 
155  [global::__DynamicallyInvokable]
156  public RegexOptions Options
157  {
158  [global::__DynamicallyInvokable]
159  get
160  {
161  return roptions;
162  }
163  }
164 
167  [global::__DynamicallyInvokable]
168  public TimeSpan MatchTimeout
169  {
170  [global::__DynamicallyInvokable]
171  get
172  {
173  return internalMatchTimeout;
174  }
175  }
176 
180  [global::__DynamicallyInvokable]
181  public bool RightToLeft
182  {
183  [global::__DynamicallyInvokable]
184  get
185  {
186  return UseOptionR();
187  }
188  }
189 
191  [global::__DynamicallyInvokable]
192  protected Regex()
193  {
194  internalMatchTimeout = DefaultMatchTimeout;
195  }
196 
202  [global::__DynamicallyInvokable]
203  public Regex(string pattern)
204  : this(pattern, RegexOptions.None, DefaultMatchTimeout, useCache: false)
205  {
206  }
207 
216  [global::__DynamicallyInvokable]
217  public Regex(string pattern, RegexOptions options)
218  : this(pattern, options, DefaultMatchTimeout, useCache: false)
219  {
220  }
221 
232  [global::__DynamicallyInvokable]
233  public Regex(string pattern, RegexOptions options, TimeSpan matchTimeout)
234  : this(pattern, options, matchTimeout, useCache: false)
235  {
236  }
237 
238  private Regex(string pattern, RegexOptions options, TimeSpan matchTimeout, bool useCache)
239  {
240  CachedCodeEntry cachedCodeEntry = null;
241  string text = null;
242  if (pattern == null)
243  {
244  throw new ArgumentNullException("pattern");
245  }
246  if (options < RegexOptions.None || (int)options >> 10 != 0)
247  {
248  throw new ArgumentOutOfRangeException("options");
249  }
250  if ((options & RegexOptions.ECMAScript) != 0 && (options & ~(RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled | RegexOptions.ECMAScript | RegexOptions.CultureInvariant)) != 0)
251  {
252  throw new ArgumentOutOfRangeException("options");
253  }
254  ValidateMatchTimeout(matchTimeout);
255  text = (((options & RegexOptions.CultureInvariant) == RegexOptions.None) ? CultureInfo.CurrentCulture.ToString() : CultureInfo.InvariantCulture.ToString());
256  string[] obj = new string[5];
257  int num = (int)options;
258  obj[0] = num.ToString(NumberFormatInfo.InvariantInfo);
259  obj[1] = ":";
260  obj[2] = text;
261  obj[3] = ":";
262  obj[4] = pattern;
263  string key = string.Concat(obj);
264  cachedCodeEntry = LookupCachedAndUpdate(key);
265  this.pattern = pattern;
266  roptions = options;
267  internalMatchTimeout = matchTimeout;
268  if (cachedCodeEntry == null)
269  {
270  RegexTree regexTree = RegexParser.Parse(pattern, roptions);
271  capnames = regexTree._capnames;
272  capslist = regexTree._capslist;
273  code = RegexWriter.Write(regexTree);
274  caps = code._caps;
275  capsize = code._capsize;
277  regexTree = null;
278  if (useCache)
279  {
280  cachedCodeEntry = CacheCode(key);
281  }
282  }
283  else
284  {
285  caps = cachedCodeEntry._caps;
286  capnames = cachedCodeEntry._capnames;
287  capslist = cachedCodeEntry._capslist;
288  capsize = cachedCodeEntry._capsize;
289  code = cachedCodeEntry._code;
290  factory = cachedCodeEntry._factory;
291  runnerref = cachedCodeEntry._runnerref;
292  replref = cachedCodeEntry._replref;
293  refsInitialized = true;
294  }
295  if (UseOptionC() && factory == null)
296  {
297  factory = Compile(code, roptions);
298  if (useCache)
299  {
300  cachedCodeEntry?.AddCompiled(factory);
301  }
302  code = null;
303  }
304  }
305 
313  protected Regex(SerializationInfo info, StreamingContext context)
314  : this(info.GetString("pattern"), (RegexOptions)info.GetInt32("options"))
315  {
316  try
317  {
318  long @int = info.GetInt64("matchTimeout");
319  TimeSpan matchTimeout = new TimeSpan(@int);
320  ValidateMatchTimeout(matchTimeout);
321  internalMatchTimeout = matchTimeout;
322  }
323  catch (SerializationException)
324  {
325  }
326  }
327 
332  {
333  si.AddValue("pattern", ToString());
334  si.AddValue("options", Options);
335  si.AddValue("matchTimeout", MatchTimeout.Ticks);
336  }
337 
340  protected internal static void ValidateMatchTimeout(TimeSpan matchTimeout)
341  {
342  if (InfiniteMatchTimeout == matchTimeout || (TimeSpan.Zero < matchTimeout && matchTimeout <= MaximumMatchTimeout))
343  {
344  return;
345  }
346  throw new ArgumentOutOfRangeException("matchTimeout");
347  }
348 
349  private static TimeSpan InitDefaultMatchTimeout()
350  {
351  AppDomain currentDomain = AppDomain.CurrentDomain;
352  object data = currentDomain.GetData("REGEX_DEFAULT_MATCH_TIMEOUT");
353  if (data == null)
354  {
355  return FallbackDefaultMatchTimeout;
356  }
357  if (!(data is TimeSpan))
358  {
359  throw new InvalidCastException(SR.GetString("IllegalDefaultRegexMatchTimeoutInAppDomain", "REGEX_DEFAULT_MATCH_TIMEOUT"));
360  }
361  TimeSpan timeSpan = (TimeSpan)data;
362  try
363  {
364  ValidateMatchTimeout(timeSpan);
365  return timeSpan;
366  }
367  catch (ArgumentOutOfRangeException)
368  {
369  throw new ArgumentOutOfRangeException(SR.GetString("IllegalDefaultRegexMatchTimeoutInAppDomain", "REGEX_DEFAULT_MATCH_TIMEOUT"));
370  }
371  }
372 
373  [MethodImpl(MethodImplOptions.NoInlining)]
374  [HostProtection(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
375  internal RegexRunnerFactory Compile(RegexCode code, RegexOptions roptions)
376  {
377  return RegexCompiler.Compile(code, roptions);
378  }
379 
385  [global::__DynamicallyInvokable]
386  public static string Escape(string str)
387  {
388  if (str == null)
389  {
390  throw new ArgumentNullException("str");
391  }
392  return RegexParser.Escape(str);
393  }
394 
402  [global::__DynamicallyInvokable]
403  public static string Unescape(string str)
404  {
405  if (str == null)
406  {
407  throw new ArgumentNullException("str");
408  }
409  return RegexParser.Unescape(str);
410  }
411 
414  [global::__DynamicallyInvokable]
415  public override string ToString()
416  {
417  return pattern;
418  }
419 
422  [global::__DynamicallyInvokable]
423  public string[] GetGroupNames()
424  {
425  string[] array;
426  if (capslist == null)
427  {
428  int num = capsize;
429  array = new string[num];
430  for (int i = 0; i < num; i++)
431  {
433  }
434  }
435  else
436  {
437  array = new string[capslist.Length];
438  Array.Copy(capslist, 0, array, 0, capslist.Length);
439  }
440  return array;
441  }
442 
445  [global::__DynamicallyInvokable]
446  public int[] GetGroupNumbers()
447  {
448  int[] array;
449  if (caps == null)
450  {
451  int num = capsize;
452  array = new int[num];
453  for (int i = 0; i < num; i++)
454  {
455  array[i] = i;
456  }
457  }
458  else
459  {
460  array = new int[caps.Count];
461  IDictionaryEnumerator enumerator = caps.GetEnumerator();
462  while (enumerator.MoveNext())
463  {
464  array[(int)enumerator.Value] = (int)enumerator.Key;
465  }
466  }
467  return array;
468  }
469 
473  [global::__DynamicallyInvokable]
474  public string GroupNameFromNumber(int i)
475  {
476  if (capslist == null)
477  {
478  if (i >= 0 && i < capsize)
479  {
480  return i.ToString(CultureInfo.InvariantCulture);
481  }
482  return string.Empty;
483  }
484  if (caps != null)
485  {
486  object obj = caps[i];
487  if (obj == null)
488  {
489  return string.Empty;
490  }
491  i = (int)obj;
492  }
493  if (i >= 0 && i < capslist.Length)
494  {
495  return capslist[i];
496  }
497  return string.Empty;
498  }
499 
505  [global::__DynamicallyInvokable]
506  public int GroupNumberFromName(string name)
507  {
508  int num = -1;
509  if (name == null)
510  {
511  throw new ArgumentNullException("name");
512  }
513  if (capnames != null)
514  {
515  object obj = capnames[name];
516  if (obj == null)
517  {
518  return -1;
519  }
520  return (int)obj;
521  }
522  num = 0;
523  foreach (char c in name)
524  {
525  if (c > '9' || c < '0')
526  {
527  return -1;
528  }
529  num *= 10;
530  num += c - 48;
531  }
532  if (num >= 0 && num < capsize)
533  {
534  return num;
535  }
536  return -1;
537  }
538 
548  [global::__DynamicallyInvokable]
549  public static bool IsMatch(string input, string pattern)
550  {
551  return IsMatch(input, pattern, RegexOptions.None, DefaultMatchTimeout);
552  }
553 
566  [global::__DynamicallyInvokable]
567  public static bool IsMatch(string input, string pattern, RegexOptions options)
568  {
569  return IsMatch(input, pattern, options, DefaultMatchTimeout);
570  }
571 
586  [global::__DynamicallyInvokable]
587  public static bool IsMatch(string input, string pattern, RegexOptions options, TimeSpan matchTimeout)
588  {
589  return new Regex(pattern, options, matchTimeout, useCache: true).IsMatch(input);
590  }
591 
599  [global::__DynamicallyInvokable]
600  public bool IsMatch(string input)
601  {
602  if (input == null)
603  {
604  throw new ArgumentNullException("input");
605  }
606  return IsMatch(input, UseOptionR() ? input.Length : 0);
607  }
608 
619  [global::__DynamicallyInvokable]
620  public bool IsMatch(string input, int startat)
621  {
622  if (input == null)
623  {
624  throw new ArgumentNullException("input");
625  }
626  return Run(quick: true, -1, input, 0, input.Length, startat) == null;
627  }
628 
637  [global::__DynamicallyInvokable]
638  public static Match Match(string input, string pattern)
639  {
640  return Match(input, pattern, RegexOptions.None, DefaultMatchTimeout);
641  }
642 
654  [global::__DynamicallyInvokable]
655  public static Match Match(string input, string pattern, RegexOptions options)
656  {
657  return Match(input, pattern, options, DefaultMatchTimeout);
658  }
659 
673  [global::__DynamicallyInvokable]
674  public static Match Match(string input, string pattern, RegexOptions options, TimeSpan matchTimeout)
675  {
676  return new Regex(pattern, options, matchTimeout, useCache: true).Match(input);
677  }
678 
685  [global::__DynamicallyInvokable]
686  public Match Match(string input)
687  {
688  if (input == null)
689  {
690  throw new ArgumentNullException("input");
691  }
692  return Match(input, UseOptionR() ? input.Length : 0);
693  }
694 
704  [global::__DynamicallyInvokable]
705  public Match Match(string input, int startat)
706  {
707  if (input == null)
708  {
709  throw new ArgumentNullException("input");
710  }
711  return Run(quick: false, -1, input, 0, input.Length, startat);
712  }
713 
726  [global::__DynamicallyInvokable]
727  public Match Match(string input, int beginning, int length)
728  {
729  if (input == null)
730  {
731  throw new ArgumentNullException("input");
732  }
733  return Run(quick: false, -1, input, beginning, length, UseOptionR() ? (beginning + length) : beginning);
734  }
735 
743  [global::__DynamicallyInvokable]
744  public static MatchCollection Matches(string input, string pattern)
745  {
746  return Matches(input, pattern, RegexOptions.None, DefaultMatchTimeout);
747  }
748 
759  [global::__DynamicallyInvokable]
760  public static MatchCollection Matches(string input, string pattern, RegexOptions options)
761  {
762  return Matches(input, pattern, options, DefaultMatchTimeout);
763  }
764 
777  [global::__DynamicallyInvokable]
778  public static MatchCollection Matches(string input, string pattern, RegexOptions options, TimeSpan matchTimeout)
779  {
780  return new Regex(pattern, options, matchTimeout, useCache: true).Matches(input);
781  }
782 
788  [global::__DynamicallyInvokable]
789  public MatchCollection Matches(string input)
790  {
791  if (input == null)
792  {
793  throw new ArgumentNullException("input");
794  }
795  return Matches(input, UseOptionR() ? input.Length : 0);
796  }
797 
806  [global::__DynamicallyInvokable]
807  public MatchCollection Matches(string input, int startat)
808  {
809  if (input == null)
810  {
811  throw new ArgumentNullException("input");
812  }
813  return new MatchCollection(this, input, 0, input.Length, startat);
814  }
815 
825  [global::__DynamicallyInvokable]
826  public static string Replace(string input, string pattern, string replacement)
827  {
828  return Replace(input, pattern, replacement, RegexOptions.None, DefaultMatchTimeout);
829  }
830 
843  [global::__DynamicallyInvokable]
844  public static string Replace(string input, string pattern, string replacement, RegexOptions options)
845  {
846  return Replace(input, pattern, replacement, options, DefaultMatchTimeout);
847  }
848 
863  [global::__DynamicallyInvokable]
864  public static string Replace(string input, string pattern, string replacement, RegexOptions options, TimeSpan matchTimeout)
865  {
866  return new Regex(pattern, options, matchTimeout, useCache: true).Replace(input, replacement);
867  }
868 
876  [global::__DynamicallyInvokable]
877  public string Replace(string input, string replacement)
878  {
879  if (input == null)
880  {
881  throw new ArgumentNullException("input");
882  }
883  return Replace(input, replacement, -1, UseOptionR() ? input.Length : 0);
884  }
885 
894  [global::__DynamicallyInvokable]
895  public string Replace(string input, string replacement, int count)
896  {
897  if (input == null)
898  {
899  throw new ArgumentNullException("input");
900  }
901  return Replace(input, replacement, count, UseOptionR() ? input.Length : 0);
902  }
903 
915  [global::__DynamicallyInvokable]
916  public string Replace(string input, string replacement, int count, int startat)
917  {
918  if (input == null)
919  {
920  throw new ArgumentNullException("input");
921  }
922  if (replacement == null)
923  {
924  throw new ArgumentNullException("replacement");
925  }
926  RegexReplacement regexReplacement = (RegexReplacement)replref.Get();
927  if (regexReplacement == null || !regexReplacement.Pattern.Equals(replacement))
928  {
929  regexReplacement = RegexParser.ParseReplacement(replacement, caps, capsize, capnames, roptions);
930  replref.Cache(regexReplacement);
931  }
932  return regexReplacement.Replace(this, input, count, startat);
933  }
934 
944  [global::__DynamicallyInvokable]
945  public static string Replace(string input, string pattern, MatchEvaluator evaluator)
946  {
947  return Replace(input, pattern, evaluator, RegexOptions.None, DefaultMatchTimeout);
948  }
949 
962  [global::__DynamicallyInvokable]
963  public static string Replace(string input, string pattern, MatchEvaluator evaluator, RegexOptions options)
964  {
965  return Replace(input, pattern, evaluator, options, DefaultMatchTimeout);
966  }
967 
982  [global::__DynamicallyInvokable]
983  public static string Replace(string input, string pattern, MatchEvaluator evaluator, RegexOptions options, TimeSpan matchTimeout)
984  {
985  return new Regex(pattern, options, matchTimeout, useCache: true).Replace(input, evaluator);
986  }
987 
995  [global::__DynamicallyInvokable]
996  public string Replace(string input, MatchEvaluator evaluator)
997  {
998  if (input == null)
999  {
1000  throw new ArgumentNullException("input");
1001  }
1002  return Replace(input, evaluator, -1, UseOptionR() ? input.Length : 0);
1003  }
1004 
1013  [global::__DynamicallyInvokable]
1014  public string Replace(string input, MatchEvaluator evaluator, int count)
1015  {
1016  if (input == null)
1017  {
1018  throw new ArgumentNullException("input");
1019  }
1020  return Replace(input, evaluator, count, UseOptionR() ? input.Length : 0);
1021  }
1022 
1034  [global::__DynamicallyInvokable]
1035  public string Replace(string input, MatchEvaluator evaluator, int count, int startat)
1036  {
1037  if (input == null)
1038  {
1039  throw new ArgumentNullException("input");
1040  }
1041  return RegexReplacement.Replace(evaluator, this, input, count, startat);
1042  }
1043 
1052  [global::__DynamicallyInvokable]
1053  public static string[] Split(string input, string pattern)
1054  {
1055  return Split(input, pattern, RegexOptions.None, DefaultMatchTimeout);
1056  }
1057 
1069  [global::__DynamicallyInvokable]
1070  public static string[] Split(string input, string pattern, RegexOptions options)
1071  {
1072  return Split(input, pattern, options, DefaultMatchTimeout);
1073  }
1074 
1088  [global::__DynamicallyInvokable]
1089  public static string[] Split(string input, string pattern, RegexOptions options, TimeSpan matchTimeout)
1090  {
1091  return new Regex(pattern, options, matchTimeout, useCache: true).Split(input);
1092  }
1093 
1100  [global::__DynamicallyInvokable]
1101  public string[] Split(string input)
1102  {
1103  if (input == null)
1104  {
1105  throw new ArgumentNullException("input");
1106  }
1107  return Split(input, 0, UseOptionR() ? input.Length : 0);
1108  }
1109 
1117  [global::__DynamicallyInvokable]
1118  public string[] Split(string input, int count)
1119  {
1120  if (input == null)
1121  {
1122  throw new ArgumentNullException("input");
1123  }
1124  return RegexReplacement.Split(this, input, count, UseOptionR() ? input.Length : 0);
1125  }
1126 
1137  [global::__DynamicallyInvokable]
1138  public string[] Split(string input, int count, int startat)
1139  {
1140  if (input == null)
1141  {
1142  throw new ArgumentNullException("input");
1143  }
1144  return RegexReplacement.Split(this, input, count, startat);
1145  }
1146 
1153  [HostProtection(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
1154  public static void CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname)
1155  {
1156  CompileToAssemblyInternal(regexinfos, assemblyname, null, null);
1157  }
1158 
1166  [HostProtection(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
1167  public static void CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname, CustomAttributeBuilder[] attributes)
1168  {
1169  CompileToAssemblyInternal(regexinfos, assemblyname, attributes, null);
1170  }
1171 
1182  [HostProtection(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
1183  public static void CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname, CustomAttributeBuilder[] attributes, string resourceFile)
1184  {
1185  CompileToAssemblyInternal(regexinfos, assemblyname, attributes, resourceFile);
1186  }
1187 
1188  private static void CompileToAssemblyInternal(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname, CustomAttributeBuilder[] attributes, string resourceFile)
1189  {
1190  if (assemblyname == null)
1191  {
1192  throw new ArgumentNullException("assemblyname");
1193  }
1194  if (regexinfos == null)
1195  {
1196  throw new ArgumentNullException("regexinfos");
1197  }
1198  RegexCompiler.CompileToAssembly(regexinfos, assemblyname, attributes, resourceFile);
1199  }
1200 
1203  protected void InitializeReferences()
1204  {
1205  if (refsInitialized)
1206  {
1207  throw new NotSupportedException(SR.GetString("OnlyAllowedOnce"));
1208  }
1209  refsInitialized = true;
1210  runnerref = new ExclusiveReference();
1211  replref = new SharedReference();
1212  }
1213 
1214  internal Match Run(bool quick, int prevlen, string input, int beginning, int length, int startat)
1215  {
1216  RegexRunner regexRunner = null;
1217  if (startat < 0 || startat > input.Length)
1218  {
1219  throw new ArgumentOutOfRangeException("start", SR.GetString("BeginIndexNotNegative"));
1220  }
1221  if (length < 0 || length > input.Length)
1222  {
1223  throw new ArgumentOutOfRangeException("length", SR.GetString("LengthNotNegative"));
1224  }
1225  regexRunner = (RegexRunner)runnerref.Get();
1226  if (regexRunner == null)
1227  {
1228  regexRunner = ((factory == null) ? new RegexInterpreter(code, UseOptionInvariant() ? CultureInfo.InvariantCulture : CultureInfo.CurrentCulture) : factory.CreateInstance());
1229  }
1230  try
1231  {
1232  return regexRunner.Scan(this, input, beginning, beginning + length, startat, prevlen, quick, internalMatchTimeout);
1233  }
1234  finally
1235  {
1236  runnerref.Release(regexRunner);
1237  }
1238  }
1239 
1240  private static CachedCodeEntry LookupCachedAndUpdate(string key)
1241  {
1242  lock (livecode)
1243  {
1244  for (LinkedListNode<CachedCodeEntry> linkedListNode = livecode.First; linkedListNode != null; linkedListNode = linkedListNode.Next)
1245  {
1246  if (linkedListNode.Value._key == key)
1247  {
1248  livecode.Remove(linkedListNode);
1249  livecode.AddFirst(linkedListNode);
1250  return linkedListNode.Value;
1251  }
1252  }
1253  }
1254  return null;
1255  }
1256 
1257  private CachedCodeEntry CacheCode(string key)
1258  {
1259  CachedCodeEntry result = null;
1260  lock (livecode)
1261  {
1262  for (LinkedListNode<CachedCodeEntry> linkedListNode = livecode.First; linkedListNode != null; linkedListNode = linkedListNode.Next)
1263  {
1264  if (linkedListNode.Value._key == key)
1265  {
1266  livecode.Remove(linkedListNode);
1267  livecode.AddFirst(linkedListNode);
1268  return linkedListNode.Value;
1269  }
1270  }
1271  if (cacheSize == 0)
1272  {
1273  return result;
1274  }
1275  result = new CachedCodeEntry(key, capnames, capslist, code, caps, capsize, runnerref, replref);
1276  livecode.AddFirst(result);
1277  if (livecode.Count <= cacheSize)
1278  {
1279  return result;
1280  }
1281  livecode.RemoveLast();
1282  return result;
1283  }
1284  }
1285 
1289  protected bool UseOptionC()
1290  {
1291  return (roptions & RegexOptions.Compiled) != RegexOptions.None;
1292  }
1293 
1297  protected bool UseOptionR()
1298  {
1299  return (roptions & RegexOptions.RightToLeft) != RegexOptions.None;
1300  }
1301 
1302  internal bool UseOptionInvariant()
1303  {
1304  return (roptions & RegexOptions.CultureInvariant) != RegexOptions.None;
1305  }
1306  }
1307 }
Converts a base data type to another base data type.
Definition: Convert.cs:10
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
IDictionary CapNames
Gets or sets a dictionary that maps named capturing groups to their index values.
Definition: Regex.cs:102
string [] Split(string input)
Splits an input string into an array of substrings at the positions defined by a regular expression p...
Definition: Regex.cs:1101
RegexOptions Options
Gets the options that were passed into the T:System.Text.RegularExpressions.Regex constructor.
Definition: Regex.cs:157
static string Replace(string input, string pattern, string replacement, RegexOptions options)
In a specified input string, replaces all strings that match a specified regular expression with a sp...
Definition: Regex.cs:844
static void CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname, CustomAttributeBuilder[] attributes)
Compiles one or more specified T:System.Text.RegularExpressions.Regex objects to a named assembly wit...
Definition: Regex.cs:1167
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Regex()
Initializes a new instance of the T:System.Text.RegularExpressions.Regex class.
Definition: Regex.cs:192
Creates a T:System.Text.RegularExpressions.RegexRunner class for a compiled regular expression.
bool MoveNext()
Advances the enumerator to the next element of the collection.
bool IsMatch(string input, int startat)
Indicates whether the regular expression specified in the T:System.Text.RegularExpressions....
Definition: Regex.cs:620
MatchCollection Matches(string input)
Searches the specified input string for all occurrences of a regular expression.
Definition: Regex.cs:789
bool Remove(T value)
Removes the first occurrence of the specified value from the T:System.Collections....
Definition: LinkedList.cs:657
static string [] Split(string input, string pattern)
Splits an input string into an array of substrings at the positions defined by a regular expression p...
Definition: Regex.cs:1053
static Match Match(string input, string pattern)
Searches the specified input string for the first occurrence of the specified regular expression.
Definition: Regex.cs:638
override string ToString()
Returns the regular expression pattern that was passed into the Regex constructor.
Definition: Regex.cs:415
internal TimeSpan internalMatchTimeout
The maximum amount of time that can elapse in a pattern-matching operation before the operation times...
Definition: Regex.cs:37
string [] GetGroupNames()
Returns an array of capturing group names for the regular expression.
Definition: Regex.cs:423
string [] Split(string input, int count, int startat)
Splits an input string a specified maximum number of times into an array of substrings,...
Definition: Regex.cs:1138
LinkedListNode< T > AddFirst(T value)
Adds a new node containing the specified value at the start of the T:System.Collections....
Definition: LinkedList.cs:418
object Key
Gets the key of the current dictionary entry.
static string Unescape(string str)
Converts any escaped characters in the input string.
Definition: Regex.cs:403
IDictionary Caps
Gets or sets a dictionary that maps numbered capturing groups to their index values.
Definition: Regex.cs:78
LinkedListNode< T > First
Gets the first node of the T:System.Collections.Generic.LinkedList`1.
Definition: LinkedList.cs:231
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
bool IsMatch(string input)
Indicates whether the regular expression specified in the T:System.Text.RegularExpressions....
Definition: Regex.cs:600
The exception that is thrown when the value of an argument is outside the allowable range of values a...
The exception that is thrown for invalid casting or explicit conversion.
static bool IsMatch(string input, string pattern)
Indicates whether the specified regular expression finds a match in the specified input string.
Definition: Regex.cs:549
MatchCollection Matches(string input, int startat)
Searches the specified input string for all occurrences of a regular expression, beginning at the spe...
Definition: Regex.cs:807
static string Replace(string input, string pattern, string replacement)
In a specified input string, replaces all strings that match a specified regular expression with a sp...
Definition: Regex.cs:826
static internal void ValidateMatchTimeout(TimeSpan matchTimeout)
Checks whether a time-out interval is within an acceptable range.
Definition: Regex.cs:340
string Replace(string input, MatchEvaluator evaluator, int count, int startat)
In a specified input substring, replaces a specified maximum number of strings that match a regular e...
Definition: Regex.cs:1035
internal Hashtable capnames
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: Regex.cs:51
static TimeSpan FromMilliseconds(double value)
Returns a T:System.TimeSpan that represents a specified number of milliseconds.
Definition: TimeSpan.cs:477
static Match Match(string input, string pattern, RegexOptions options, TimeSpan matchTimeout)
Searches the input string for the first occurrence of the specified regular expression,...
Definition: Regex.cs:674
Describes the source and destination of a given serialized stream, and provides an additional caller-...
internal int capsize
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: Regex.cs:57
static AppDomain CurrentDomain
Gets the current application domain for the current T:System.Threading.Thread.
Definition: AppDomain.cs:274
Provides information about a regular expression that is used to compile a regular expression to a sta...
Represents an application domain, which is an isolated environment where applications execute....
Definition: AppDomain.cs:33
static int CacheSize
Gets or sets the maximum number of entries in the current static cache of compiled regular expression...
Definition: Regex.cs:126
override string ToString()
Returns a string containing the name of the current T:System.Globalization.CultureInfo in the format ...
static Match Match(string input, string pattern, RegexOptions options)
Searches the input string for the first occurrence of the specified regular expression,...
Definition: Regex.cs:655
int Count
Gets the number of nodes actually contained in the T:System.Collections.Generic.LinkedList`1.
Definition: LinkedList.cs:219
static readonly TimeSpan InfiniteTimeSpan
A constant used to specify an infinite waiting period, for methods that accept a T:System....
Definition: Timeout.cs:13
static void CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname)
Compiles one or more specified T:System.Text.RegularExpressions.Regex objects to a named assembly.
Definition: Regex.cs:1154
Match Match(string input, int beginning, int length)
Searches the input string for the first occurrence of a regular expression, beginning at the specifie...
Definition: Regex.cs:727
long Ticks
Gets the number of ticks that represent the value of the current T:System.TimeSpan structure.
Definition: TimeSpan.cs:84
Regex(SerializationInfo info, StreamingContext context)
Initializes a new instance of the T:System.Text.RegularExpressions.Regex class by using serialized da...
Definition: Regex.cs:313
Represents a node in a T:System.Collections.Generic.LinkedList`1. This class cannot be inherited.
SecurityAction
Specifies the security actions that can be performed using declarative security.
internal RegexRunnerFactory factory
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: Regex.cs:22
Regex(string pattern)
Initializes a new instance of the T:System.Text.RegularExpressions.Regex class for the specified regu...
Definition: Regex.cs:203
Match Match(string input)
Searches the specified input string for the first occurrence of the regular expression specified in t...
Definition: Regex.cs:686
string Replace(string input, string replacement)
In a specified input string, replaces all strings that match a regular expression pattern with a spec...
Definition: Regex.cs:877
void AddValue(string name, object value, Type type)
Adds a value into the T:System.Runtime.Serialization.SerializationInfo store, where value is associa...
static string [] Split(string input, string pattern, RegexOptions options, TimeSpan matchTimeout)
Splits an input string into an array of substrings at the positions defined by a specified regular ex...
Definition: Regex.cs:1089
int [] GetGroupNumbers()
Returns an array of capturing group numbers that correspond to group names in an array.
Definition: Regex.cs:446
Represents a collection of key/value pairs that are organized based on the hash code of the key....
Definition: Hashtable.cs:17
bool UseOptionR()
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: Regex.cs:1297
internal Hashtable caps
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: Regex.cs:48
void InitializeReferences()
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: Regex.cs:1203
static string Replace(string input, string pattern, MatchEvaluator evaluator, RegexOptions options, TimeSpan matchTimeout)
In a specified input string, replaces all substrings that match a specified regular expression with a...
Definition: Regex.cs:983
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
string Replace(string input, string replacement, int count)
In a specified input string, replaces a specified maximum number of strings that match a regular expr...
Definition: Regex.cs:895
static void CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname, CustomAttributeBuilder[] attributes, string resourceFile)
Compiles one or more specified T:System.Text.RegularExpressions.Regex objects and a specified resourc...
Definition: Regex.cs:1183
string Replace(string input, string replacement, int count, int startat)
In a specified input substring, replaces a specified maximum number of strings that match a regular e...
Definition: Regex.cs:916
The exception thrown when an error occurs during serialization or deserialization.
static bool IsMatch(string input, string pattern, RegexOptions options, TimeSpan matchTimeout)
Indicates whether the specified regular expression finds a match in the specified input string,...
Definition: Regex.cs:587
static readonly TimeSpan Zero
Represents the zero T:System.TimeSpan value. This field is read-only.
Definition: TimeSpan.cs:64
MethodImplOptions
Defines the details of how a method is implemented.
bool UseOptionC()
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: Regex.cs:1289
Contains constants that specify infinite time-out intervals. This class cannot be inherited.
Definition: Timeout.cs:8
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
RegexOptions
Provides enumerated values to use to set regular expression options.
Definition: RegexOptions.cs:6
int GroupNumberFromName(string name)
Returns the group number that corresponds to the specified group name.
Definition: Regex.cs:506
string [] Split(string input, int count)
Splits an input string a specified maximum number of times into an array of substrings,...
Definition: Regex.cs:1118
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
string Replace(string input, MatchEvaluator evaluator, int count)
In a specified input string, replaces a specified maximum number of strings that match a regular expr...
Definition: Regex.cs:1014
Describes an assembly's unique identity in full.
Definition: AssemblyName.cs:19
static CultureInfo CurrentCulture
Gets or sets the T:System.Globalization.CultureInfo object that represents the culture used by the cu...
Definition: CultureInfo.cs:120
static string [] Split(string input, string pattern, RegexOptions options)
Splits an input string into an array of substrings at the positions defined by a specified regular ex...
Definition: Regex.cs:1070
static string Escape(string str)
Escapes a minimal set of characters (\, *, +, ?, |, {, [, (,), ^, $,., #, and white space) by replaci...
Definition: Regex.cs:386
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
string GroupNameFromNumber(int i)
Gets the group name that corresponds to the specified group number.
Definition: Regex.cs:474
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
static TimeSpan Parse(string s)
Converts the string representation of a time interval to its T:System.TimeSpan equivalent.
Definition: TimeSpan.cs:570
static MatchCollection Matches(string input, string pattern, RegexOptions options)
Searches the specified input string for all occurrences of a specified regular expression,...
Definition: Regex.cs:760
internal string [] capslist
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: Regex.cs:54
Regex(string pattern, RegexOptions options)
Initializes a new instance of the T:System.Text.RegularExpressions.Regex class for the specified regu...
Definition: Regex.cs:217
Match Match(string input, int startat)
Searches the input string for the first occurrence of a regular expression, beginning at the specifie...
Definition: Regex.cs:705
object Value
Gets the value of the current dictionary entry.
Represents a time interval.To browse the .NET Framework source code for this type,...
Definition: TimeSpan.cs:12
Regex(string pattern, RegexOptions options, TimeSpan matchTimeout)
Initializes a new instance of the T:System.Text.RegularExpressions.Regex class for the specified regu...
Definition: Regex.cs:233
static string Replace(string input, string pattern, string replacement, RegexOptions options, TimeSpan matchTimeout)
In a specified input string, replaces all strings that match a specified regular expression with a sp...
Definition: Regex.cs:864
abstract internal RegexRunner CreateInstance()
When overridden in a derived class, creates a T:System.Text.RegularExpressions.RegexRunner object for...
Specifies that the class can be serialized.
delegate string MatchEvaluator(Match match)
Represents the method that is called each time a regular expression match is found during a Overload:...
static string ToString(object value)
Converts the value of the specified object to its equivalent string representation.
Definition: Convert.cs:3793
Enumerates the elements of a nongeneric dictionary.
TimeSpan MatchTimeout
Gets the time-out interval of the current instance.
Definition: Regex.cs:169
internal RegexOptions roptions
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: Regex.cs:25
string Replace(string input, MatchEvaluator evaluator)
In a specified input string, replaces all strings that match a specified regular expression with a st...
Definition: Regex.cs:996
static MatchCollection Matches(string input, string pattern)
Searches the specified input string for all occurrences of a specified regular expression.
Definition: Regex.cs:744
object GetData(string name)
Gets the value stored in the current application domain for the specified name.
Definition: AppDomain.cs:2542
static MatchCollection Matches(string input, string pattern, RegexOptions options, TimeSpan matchTimeout)
Searches the specified input string for all occurrences of a specified regular expression,...
Definition: Regex.cs:778
static string Replace(string input, string pattern, MatchEvaluator evaluator)
In a specified input string, replaces all strings that match a specified regular expression with a st...
Definition: Regex.cs:945
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
The exception that is thrown when an invoked method is not supported, or when there is an attempt to ...
The T:System.Text.RegularExpressions.RegexRunner class is the base class for compiled regular express...
Definition: RegexRunner.cs:7
Represents a nongeneric collection of key/value pairs.
Definition: IDictionary.cs:8
static bool IsMatch(string input, string pattern, RegexOptions options)
Indicates whether the specified regular expression finds a match in the specified input string,...
Definition: Regex.cs:567
void GetObjectData(SerializationInfo info, StreamingContext context)
Populates a T:System.Runtime.Serialization.SerializationInfo with the data needed to serialize the ta...
Represents an immutable regular expression.To browse the .NET Framework source code for this type,...
Definition: Regex.cs:16
void RemoveLast()
Removes the node at the end of the T:System.Collections.Generic.LinkedList`1.
Definition: LinkedList.cs:696
internal string pattern
Used by a T:System.Text.RegularExpressions.Regex object generated by the Overload:System....
Definition: Regex.cs:19
static NumberFormatInfo InvariantInfo
Gets a read-only T:System.Globalization.NumberFormatInfo object that is culture-independent (invarian...
virtual int Count
Gets the number of key/value pairs contained in the T:System.Collections.Hashtable.
Definition: Hashtable.cs:658
Specifies that no options are set. For more information about the default behavior of the regular exp...
static string Replace(string input, string pattern, MatchEvaluator evaluator, RegexOptions options)
In a specified input string, replaces all strings that match a specified regular expression with a st...
Definition: Regex.cs:963
static readonly TimeSpan InfiniteMatchTimeout
Specifies that a pattern-matching operation should not time out.
Definition: Regex.cs:33
Represents the set of successful matches found by iteratively applying a regular expression pattern t...
Provides culture-specific information for formatting and parsing numeric values.