1 //------------------------------------------------------------------------------
2 // <copyright file="Regex.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
7 // The Regex class represents a single compiled instance of a regular
10 namespace System.Text.RegularExpressions {
13 using System.Threading;
14 using System.Collections;
15 using System.Reflection;
17 using System.Reflection.Emit;
19 using System.Globalization;
20 using System.Security.Permissions;
21 using System.Runtime.CompilerServices;
22 using System.Collections.Generic;
23 using System.Diagnostics.CodeAnalysis;
26 using System.Runtime.Serialization;
27 using System.Runtime.Versioning;
33 /// Represents an immutable, compiled regular expression. Also
34 /// contains static methods that allow use of regular expressions without instantiating
35 /// a Regex explicitly.
47 // Fields used by precompiled regexes
48 protected internal string pattern;
50 protected internal RegexRunnerFactory factory; // if compiled, this is the RegexRunner subclass
52 internal RegexRunnerFactory factory; // if compiled, this is the RegexRunner subclass
55 protected internal RegexOptions roptions; // the top-level options from the options string
58 // *********** Match timeout fields { ***********
60 // We need this because time is queried using Environment.TickCount for performance reasons
61 // (Environment.TickCount returns millisecs as an int and cycles):
65 private static readonly TimeSpan MaximumMatchTimeout = TimeSpan.FromMilliseconds(Int32.MaxValue - 1);
67 // InfiniteMatchTimeout specifies that match timeout is switched OFF. It allows for faster code paths
68 // compared to simply having a very large timeout.
69 // We do not want to ask users to use System.Threading.Timeout.InfiniteTimeSpan as a parameter because:
70 // (1) We do not want to imply any relation between having using a RegEx timeout and using multi-threading.
71 // (2) We do not want to require users to take ref to a contract assembly for threading just to use RegEx.
72 // There may in theory be a SKU that has RegEx, but no multithreading.
73 // We create a public Regex.InfiniteMatchTimeout constant, which for consistency uses the save underlying
74 // value as Timeout.InfiniteTimeSpan creating an implementation detail dependency only.
75 #if !SILVERLIGHT || FEATURE_NETCORE
79 public static readonly TimeSpan InfiniteMatchTimeout =
81 new TimeSpan (0, 0, 0, 0, Timeout.Infinite);
83 Timeout.InfiniteTimeSpan;
86 internal static readonly TimeSpan InfiniteMatchTimeout = new TimeSpan(0, 0, 0, 0, Timeout.Infinite);
89 // All these protected internal fields in this class really should not be protected. The historic reason
90 // for this is that classes extending Regex that are generated via CompileToAssembly rely on the fact that
91 // these are accessible as protected in order to initialise them in the generated constructor of the
92 // extending class. We should update this initialisation logic to using a protected constructor, but until
93 // that is done we stick to the existing pattern however ugly it may be.
95 [OptionalField(VersionAdded = 2)]
100 TimeSpan internalMatchTimeout; // timeout for the execution of this regex
103 // During static initialisation of Regex we check
104 private const String DefaultMatchTimeout_ConfigKeyName = "REGEX_DEFAULT_MATCH_TIMEOUT";
107 // FallbackDefaultMatchTimeout specifies the match timeout to use if no other timeout was specified
108 // by one means or another. For now it is set to InfiniteMatchTimeout, meaning timeouts are OFF by
109 // default (for Dev12 we plan to set a positive value).
110 // Having this field is helpful to read the code as it makes it clear when we mean
111 // "default that is currently no-timeouts" and when we mean "actually no-timeouts".
112 // In Silverlight, DefaultMatchTimeout is always set to FallbackDefaultMatchTimeout,
113 // on desktop, DefaultMatchTimeout can be configured via AppDomain and falls back to
114 // FallbackDefaultMatchTimeout, if no AppDomain setting is present (see InitDefaultMatchTimeout()).
118 internal static readonly TimeSpan FallbackDefaultMatchTimeout = InfiniteMatchTimeout;
121 // DefaultMatchTimeout specifies the match timeout to use if no other timeout was specified
122 // by one means or another. Typically, it is set to InfiniteMatchTimeout in Dev 11
123 // (we plan to set a positive timeout in Dev12).
124 // Hosts (e.g.) ASP may set an AppDomain property via SetData to change the default value.
127 internal static readonly TimeSpan DefaultMatchTimeout = InitDefaultMatchTimeout();
129 internal static readonly TimeSpan DefaultMatchTimeout = FallbackDefaultMatchTimeout;
132 // *********** } match timeout fields ***********
136 internal Dictionary<Int32, Int32> caps; // if captures are sparse, this is the hashtable capnum->index
137 internal Dictionary<String, Int32> capnames; // if named captures are used, this maps names->index
139 // desktop build still uses non-generic collections for AppCompat with .NET Framework 3.5 pre-compiled assemblies
140 protected internal Hashtable caps;
141 protected internal Hashtable capnames;
143 protected internal String[] capslist; // if captures are sparse or named captures are used, this is the sorted list of names
144 protected internal int capsize; // the size of the capture array
146 internal ExclusiveReference runnerref; // cached runner
147 internal SharedReference replref; // cached parsed replacement pattern
148 internal RegexCode code; // if interpreted, this is the code for RegexIntepreter
149 internal bool refsInitialized = false;
151 internal static LinkedList<CachedCodeEntry> livecode = new LinkedList<CachedCodeEntry>();// the cached of code and factories that are currently loaded
152 internal static int cacheSize = 15;
154 internal const int MaxOptionShift = 10;
158 // If a compiled-to-assembly RegEx was generated using an earlier version, then internalMatchTimeout will be uninitialised.
160 // In distant future, when RegEx generated using pre Dev11 are not supported any more, we can remove this to aid performance:
162 this.internalMatchTimeout = DefaultMatchTimeout;
166 * Compiles and returns a Regex object corresponding to the given pattern
170 /// Creates and compiles a regular expression object for the specified regular
174 public Regex(String pattern)
175 : this(pattern, RegexOptions.None, DefaultMatchTimeout, false) {
179 * Returns a Regex object corresponding to the given pattern, compiled with
180 * the specified options.
184 /// Creates and compiles a regular expression object for the
185 /// specified regular expression
186 /// with options that modify the pattern.
189 public Regex(String pattern, RegexOptions options)
190 : this(pattern, options, DefaultMatchTimeout, false) {
193 #if !SILVERLIGHT || FEATURE_NETCORE
198 Regex(String pattern, RegexOptions options, TimeSpan matchTimeout)
199 : this(pattern, options, matchTimeout, false) {
202 private Regex(String pattern, RegexOptions options, TimeSpan matchTimeout, bool useCache) {
204 CachedCodeEntry cached = null;
205 string cultureKey = null;
208 throw new ArgumentNullException("pattern");
209 if (options < RegexOptions.None || ( ((int) options) >> MaxOptionShift) != 0)
210 throw new ArgumentOutOfRangeException("options");
211 if ((options & RegexOptions.ECMAScript) != 0
212 && (options & ~(RegexOptions.ECMAScript |
213 RegexOptions.IgnoreCase |
214 RegexOptions.Multiline |
215 #if !(SILVERLIGHT) || FEATURE_LEGACYNETCF
216 RegexOptions.Compiled |
218 RegexOptions.CultureInvariant
223 throw new ArgumentOutOfRangeException("options");
225 ValidateMatchTimeout(matchTimeout);
227 // Try to look up this regex in the cache. We do this regardless of whether useCache is true since there's
228 // really no reason not to.
229 if ((options & RegexOptions.CultureInvariant) != 0)
230 cultureKey = CultureInfo.InvariantCulture.ToString(); // "English (United States)"
232 cultureKey = CultureInfo.CurrentCulture.ToString();
234 String key = ((int) options).ToString(NumberFormatInfo.InvariantInfo) + ":" + cultureKey + ":" + pattern;
235 cached = LookupCachedAndUpdate(key);
237 this.pattern = pattern;
238 this.roptions = options;
240 this.internalMatchTimeout = matchTimeout;
242 if (cached == null) {
244 tree = RegexParser.Parse(pattern, roptions);
246 // Extract the relevant information
247 capnames = tree._capnames;
248 capslist = tree._capslist;
249 code = RegexWriter.Write(tree);
251 capsize = code._capsize;
253 InitializeReferences();
257 cached = CacheCode(key);
261 capnames = cached._capnames;
262 capslist = cached._capslist;
263 capsize = cached._capsize;
265 factory = cached._factory;
266 runnerref = cached._runnerref;
267 replref = cached._replref;
268 refsInitialized = true;
271 #if !(SILVERLIGHT || FULL_AOT_RUNTIME)
272 // if the compile option is set, then compile the code if it's not already
273 if (UseOptionC() && factory == null) {
274 factory = Compile(code, roptions);
276 if (useCache && cached != null)
277 cached.AddCompiled(factory);
285 * ISerializable constructor
287 protected Regex(SerializationInfo info, StreamingContext context)
288 : this(info.GetString("pattern"), (RegexOptions) info.GetInt32("options")) {
291 Int64 timeoutTicks = info.GetInt64("matchTimeout");
292 TimeSpan timeout = new TimeSpan(timeoutTicks);
293 ValidateMatchTimeout(timeout);
294 this.internalMatchTimeout = timeout;
295 } catch (SerializationException) {
296 // If this occurs, then assume that this object was serialised using a version
297 // before timeout was added. In that case just do not set a timeout
298 // (keep default value)
304 * ISerializable method
307 void ISerializable.GetObjectData(SerializationInfo si, StreamingContext context) {
308 si.AddValue("pattern", this.ToString());
309 si.AddValue("options", this.Options);
310 si.AddValue("matchTimeout", this.MatchTimeout.Ticks);
312 #endif // !SILVERLIGHT
314 //* Note: "<" is the XML entity for smaller ("<").
316 /// Validates that the specified match timeout value is valid.
317 /// The valid range is <code>TimeSpan.Zero < matchTimeout <= Regex.MaximumMatchTimeout</code>.
319 /// <param name="matchTimeout">The timeout value to validate.</param>
320 /// <exception cref="System.ArgumentOutOfRangeException">If the specified timeout is not within a valid range.
327 static void ValidateMatchTimeout(TimeSpan matchTimeout) {
329 if (InfiniteMatchTimeout == matchTimeout)
332 // Change this to make sure timeout is not longer then Environment.Ticks cycle length:
333 if (TimeSpan.Zero < matchTimeout && matchTimeout <= MaximumMatchTimeout)
336 throw new ArgumentOutOfRangeException("matchTimeout");
341 /// Specifies the default RegEx matching timeout value (i.e. the timeout that will be used if no
342 /// explicit timeout is specified).
343 /// The default is queried from the current <code>AppDomain</code> through <code>GetData</code> using
344 /// the key specified in <code>Regex.DefaultMatchTimeout_ConfigKeyName</code>. For that key, the
345 /// current <code>AppDomain</code> is expected to either return <code>null</code> or a <code>TimeSpan</code>
346 /// value specifying the default timeout within a valid range.
347 /// If the AddDomain's data value for that key is not a <code>TimeSpan</code> value or if it is outside the
348 /// valid range, an exception is thrown which will result in a <code>TypeInitializationException</code> for RegEx.
349 /// If the AddDomain's data value for that key is <code>null</code>, a fallback value is returned
350 /// (see <code>FallbackDefaultMatchTimeout</code> in code).
352 /// <returns>The default RegEx matching timeout for this AppDomain</returns>
353 private static TimeSpan InitDefaultMatchTimeout() {
356 AppDomain ad = AppDomain.CurrentDomain;
357 Object defTmOut = ad.GetData(DefaultMatchTimeout_ConfigKeyName);
359 // If no default is specified, use fallback:
360 if (defTmOut == null)
361 return FallbackDefaultMatchTimeout;
363 // If default has invalid type, throw. It will result in a TypeInitializationException:
364 if (!(defTmOut is TimeSpan)) {
367 String errMsg = "AppDomain.CurrentDomain.GetData(\"" + DefaultMatchTimeout_ConfigKeyName + "\")"
368 + " is expected to return null or a value of type System.TimeSpan only; but it returned a value of type"
369 + " '" + defTmOut.GetType().FullName + "'.";
370 System.Diagnostics.Debug.WriteLine(errMsg);
373 throw new InvalidCastException(SR.GetString(SR.IllegalDefaultRegexMatchTimeoutInAppDomain, DefaultMatchTimeout_ConfigKeyName));
376 // Convert default value:
377 TimeSpan defaultTimeout = (TimeSpan) defTmOut;
379 // If default timeout is outside the valid range, throw. It will result in a TypeInitializationException:
381 ValidateMatchTimeout(defaultTimeout);
383 } catch (ArgumentOutOfRangeException) {
386 String errMsg = "AppDomain.CurrentDomain.GetData(\"" + DefaultMatchTimeout_ConfigKeyName + "\")"
387 + " returned a TimeSpan value outside the valid range"
388 + " ("+ defaultTimeout.ToString() + ").";
389 System.Diagnostics.Debug.WriteLine(errMsg);
392 throw new ArgumentOutOfRangeException(SR.GetString(SR.IllegalDefaultRegexMatchTimeoutInAppDomain, DefaultMatchTimeout_ConfigKeyName));
396 return defaultTimeout;
397 } // private static TimeSpan InitDefaultMatchTimeout
398 #endif // !SILVERLIGHT
400 #if !SILVERLIGHT && !FULL_AOT_RUNTIME
402 * This method is here for perf reasons: if the call to RegexCompiler is NOT in the
403 * Regex constructor, we don't load RegexCompiler and its reflection classes when
404 * instantiating a non-compiled regex
405 * This method is internal virtual so the jit does not inline it.
409 HostProtection(MayLeakOnAbort=true),
411 MethodImplAttribute(MethodImplOptions.NoInlining)
413 internal RegexRunnerFactory Compile(RegexCode code, RegexOptions roptions) {
414 return RegexCompiler.Compile(code, roptions);
416 #endif // !SILVERLIGHT
419 * Escape metacharacters within the string
424 /// a minimal set of metacharacters (\, *, +, ?, |, {, [, (, ), ^, $, ., #, and
425 /// whitespace) by replacing them with their \ codes. This converts a string so that
426 /// it can be used as a constant within a regular expression safely. (Note that the
427 /// reason # and whitespace must be escaped is so the string can be used safely
428 /// within an expression parsed with x mode. If future Regex features add
429 /// additional metacharacters, developers should depend on Escape to escape those
430 /// characters as well.)
433 public static String Escape(String str) {
435 throw new ArgumentNullException("str");
437 return RegexParser.Escape(str);
441 * Unescape character codes within the string
445 /// Unescapes any escaped characters in the input string.
448 [SuppressMessage("Microsoft.Naming","CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId="Unescape", Justification="[....]: already shipped since v1 - can't fix without causing a breaking change")]
449 public static String Unescape(String str) {
451 throw new ArgumentNullException("str");
453 return RegexParser.Unescape(str);
456 [SuppressMessage("Microsoft.Concurrency", "CA8001", Justification = "Reviewed for thread-safety")]
457 public static int CacheSize {
463 throw new ArgumentOutOfRangeException("value");
466 if (livecode.Count > cacheSize) {
468 while (livecode.Count > cacheSize)
469 livecode.RemoveLast();
477 /// Returns the options passed into the constructor
480 public RegexOptions Options {
481 get { return roptions;}
486 /// The match timeout used by this Regex instance.
488 #if !SILVERLIGHT || FEATURE_NETCORE
493 TimeSpan MatchTimeout {
494 get { return internalMatchTimeout; }
499 * True if the regex is leftward
503 /// Indicates whether the regular expression matches from right to
507 public bool RightToLeft {
515 /// Returns the regular expression pattern passed into the constructor
518 public override string ToString() {
523 * Returns an array of the group names that are used to capture groups
524 * in the regular expression. Only needed if the regex is not known until
525 * runtime, and one wants to extract captured groups. (Probably unusual,
526 * but supplied for completeness.)
530 /// the GroupNameCollection for the regular expression. This collection contains the
531 /// set of strings used to name capturing groups in the expression.
533 public String[] GetGroupNames() {
536 if (capslist == null) {
538 result = new String[max];
540 for (int i = 0; i < max; i++) {
541 result[i] = Convert.ToString(i, CultureInfo.InvariantCulture);
545 result = new String[capslist.Length];
547 System.Array.Copy(capslist, 0, result, 0, capslist.Length);
554 * Returns an array of the group numbers that are used to capture groups
555 * in the regular expression. Only needed if the regex is not known until
556 * runtime, and one wants to extract captured groups. (Probably unusual,
557 * but supplied for completeness.)
561 /// the integer group number corresponding to a group name.
563 public int[] GetGroupNumbers() {
568 result = new int[max];
570 for (int i = 0; i < max; i++) {
575 result = new int[caps.Count];
577 IDictionaryEnumerator de = caps.GetEnumerator();
578 while (de.MoveNext()) {
579 result[(int)de.Value] = (int)de.Key;
587 * Given a group number, maps it to a group name. Note that nubmered
588 * groups automatically get a group name that is the decimal string
589 * equivalent of its number.
591 * Returns null if the number is not a recognized group number.
595 /// Retrieves a group name that corresponds to a group number.
598 public String GroupNameFromNumber(int i) {
599 if (capslist == null) {
600 if (i >= 0 && i < capsize)
601 return i.ToString(CultureInfo.InvariantCulture);
608 if (!caps.ContainsKey(i))
610 Object obj = caps[i];
622 if (i >= 0 && i < capslist.Length)
630 * Given a group name, maps it to a group number. Note that nubmered
631 * groups automatically get a group name that is the decimal string
632 * equivalent of its number.
634 * Returns -1 if the name is not a recognized group name.
638 /// Returns a group number that corresponds to a group name.
641 public int GroupNumberFromName(String name) {
645 throw new ArgumentNullException("name");
647 // look up name if we have a hashtable of names
648 if (capnames != null) {
650 if (!capnames.ContainsKey(name))
652 Object ret = capnames[name];
658 return capnames[name];
664 // convert to an int if it looks like a number
666 for (int i = 0; i < name.Length; i++) {
669 if (ch > '9' || ch < '0')
673 result += (ch - '0');
676 // return int if it's in range
677 if (result >= 0 && result < capsize)
684 * Static version of simple IsMatch call
688 /// Searches the input
689 /// string for one or more occurrences of the text supplied in the pattern
693 public static bool IsMatch(String input, String pattern) {
694 return IsMatch(input, pattern, RegexOptions.None, DefaultMatchTimeout);
698 * Static version of simple IsMatch call
702 /// Searches the input string for one or more occurrences of the text
703 /// supplied in the pattern parameter with matching options supplied in the options
707 public static bool IsMatch(String input, String pattern, RegexOptions options) {
708 return IsMatch(input, pattern, options, DefaultMatchTimeout);
711 #if !SILVERLIGHT || FEATURE_NETCORE
716 static bool IsMatch(String input, String pattern, RegexOptions options, TimeSpan matchTimeout) {
717 return new Regex(pattern, options, matchTimeout, true).IsMatch(input);
721 * Returns true if the regex finds a match within the specified string
725 /// Searches the input string for one or
726 /// more matches using the previous pattern, options, and starting
730 public bool IsMatch(String input) {
733 throw new ArgumentNullException("input");
735 return IsMatch(input, UseOptionR() ? input.Length : 0);
739 * Returns true if the regex finds a match after the specified position
740 * (proceeding leftward if the regex is leftward and rightward otherwise)
744 /// Searches the input
745 /// string for one or more matches using the previous pattern and options, with
746 /// a new starting position.
749 public bool IsMatch(String input, int startat) {
752 throw new ArgumentNullException("input");
754 return (null == Run(true, -1, input, 0, input.Length, startat));
758 * Static version of simple Match call
762 /// Searches the input string for one or more occurrences of the text
763 /// supplied in the pattern parameter.
766 public static Match Match(String input, String pattern) {
767 return Match(input, pattern, RegexOptions.None, DefaultMatchTimeout);
771 * Static version of simple Match call
775 /// Searches the input string for one or more occurrences of the text
776 /// supplied in the pattern parameter. Matching is modified with an option
780 public static Match Match(String input, String pattern, RegexOptions options) {
781 return Match(input, pattern, options, DefaultMatchTimeout);
785 #if !SILVERLIGHT || FEATURE_NETCORE
790 static Match Match(String input, String pattern, RegexOptions options, TimeSpan matchTimeout) {
791 return new Regex(pattern, options, matchTimeout, true).Match(input);
795 * Finds the first match for the regular expression starting at the beginning
796 * of the string (or at the end of the string if the regex is leftward)
800 /// Matches a regular expression with a string and returns
801 /// the precise result as a RegexMatch object.
804 public Match Match(String input) {
807 throw new ArgumentNullException("input");
809 return Match(input, UseOptionR() ? input.Length : 0);
813 * Finds the first match, starting at the specified position
816 /// Matches a regular expression with a string and returns
817 /// the precise result as a RegexMatch object.
819 public Match Match(String input, int startat) {
822 throw new ArgumentNullException("input");
824 return Run(false, -1, input, 0, input.Length, startat);
828 * Finds the first match, restricting the search to the specified interval of
834 /// regular expression with a string and returns the precise result as a
835 /// RegexMatch object.
838 public Match Match(String input, int beginning, int length) {
840 throw new ArgumentNullException("input");
842 return Run(false, -1, input, beginning, length, UseOptionR() ? beginning + length : beginning);
846 * Static version of simple Matches call
850 /// Returns all the successful matches as if Match were
851 /// called iteratively numerous times.
854 public static MatchCollection Matches(String input, String pattern) {
855 return Matches(input, pattern, RegexOptions.None, DefaultMatchTimeout);
859 * Static version of simple Matches call
863 /// Returns all the successful matches as if Match were called iteratively
867 public static MatchCollection Matches(String input, String pattern, RegexOptions options) {
868 return Matches(input, pattern, options, DefaultMatchTimeout);
871 #if !SILVERLIGHT || FEATURE_NETCORE
876 static MatchCollection Matches(String input, String pattern, RegexOptions options, TimeSpan matchTimeout) {
877 return new Regex(pattern, options, matchTimeout, true).Matches(input);
881 * Finds the first match for the regular expression starting at the beginning
882 * of the string Enumerator(or at the end of the string if the regex is leftward)
887 /// all the successful matches as if Match was called iteratively numerous
891 public MatchCollection Matches(String input) {
894 throw new ArgumentNullException("input");
896 return Matches(input, UseOptionR() ? input.Length : 0);
900 * Finds the first match, starting at the specified position
905 /// all the successful matches as if Match was called iteratively numerous
909 public MatchCollection Matches(String input, int startat) {
912 throw new ArgumentNullException("input");
914 return new MatchCollection(this, input, 0, input.Length, startat);
918 * Static version of simple Replace call
923 /// all occurrences of the pattern with the <paramref name="replacement"/> pattern, starting at
924 /// the first character in the input string.
927 public static String Replace(String input, String pattern, String replacement) {
928 return Replace(input, pattern, replacement, RegexOptions.None, DefaultMatchTimeout);
932 * Static version of simple Replace call
936 /// Replaces all occurrences of
937 /// the <paramref name="pattern "/>with the <paramref name="replacement "/>
938 /// pattern, starting at the first character in the input string.
941 public static String Replace(String input, String pattern, String replacement, RegexOptions options) {
942 return Replace(input, pattern, replacement, options, DefaultMatchTimeout);
945 #if !SILVERLIGHT || FEATURE_NETCORE
950 static String Replace(String input, String pattern, String replacement, RegexOptions options, TimeSpan matchTimeout) {
951 return new Regex(pattern, options, matchTimeout, true).Replace(input, replacement);
955 * Does the replacement
959 /// Replaces all occurrences of
960 /// the <paramref name="pattern "/> with the <paramref name="replacement"/> pattern, starting at the
961 /// first character in the input string, using the previous patten.
964 public String Replace(String input, String replacement) {
967 throw new ArgumentNullException("input");
969 return Replace(input, replacement, -1, UseOptionR() ? input.Length : 0);
973 * Does the replacement
977 /// Replaces all occurrences of the (previously defined) <paramref name="pattern "/>with the
978 /// <paramref name="replacement"/> pattern, starting at the first character in the input string.
981 public String Replace(String input, String replacement, int count) {
984 throw new ArgumentNullException("input");
986 return Replace(input, replacement, count, UseOptionR() ? input.Length : 0);
990 * Does the replacement
994 /// Replaces all occurrences of the <paramref name="pattern "/>with the recent
995 /// <paramref name="replacement"/> pattern, starting at the character position
996 /// <paramref name="startat."/>
999 public String Replace(String input, String replacement, int count, int startat) {
1002 throw new ArgumentNullException("input");
1004 if (replacement == null)
1005 throw new ArgumentNullException("replacement");
1007 // a little code to grab a cached parsed replacement object
1008 RegexReplacement repl = (RegexReplacement) replref.Get();
1010 if (repl == null || !repl.Pattern.Equals(replacement)) {
1011 repl = RegexParser.ParseReplacement(replacement, caps, capsize, capnames, this.roptions);
1012 replref.Cache(repl);
1015 return repl.Replace(this, input, count, startat);
1019 * Static version of simple Replace call
1023 /// Replaces all occurrences of the <paramref name="pattern "/>with the
1024 /// <paramref name="replacement"/> pattern
1025 /// <paramref name="."/>
1028 public static String Replace(String input, String pattern, MatchEvaluator evaluator) {
1029 return Replace(input, pattern, evaluator, RegexOptions.None, DefaultMatchTimeout);
1033 * Static version of simple Replace call
1037 /// Replaces all occurrences of the <paramref name="pattern "/>with the recent
1038 /// <paramref name="replacement"/> pattern, starting at the first character<paramref name="."/>
1041 public static String Replace(String input, String pattern, MatchEvaluator evaluator, RegexOptions options) {
1042 return Replace(input, pattern, evaluator, options, DefaultMatchTimeout);
1045 #if !SILVERLIGHT || FEATURE_NETCORE
1050 static String Replace(String input, String pattern, MatchEvaluator evaluator, RegexOptions options, TimeSpan matchTimeout) {
1051 return new Regex(pattern, options, matchTimeout, true).Replace(input, evaluator);
1055 * Does the replacement
1059 /// Replaces all occurrences of the <paramref name="pattern "/>with the recent
1060 /// <paramref name="replacement"/> pattern, starting at the first character
1061 /// position<paramref name="."/>
1064 public String Replace(String input, MatchEvaluator evaluator) {
1067 throw new ArgumentNullException("input");
1069 return Replace(input, evaluator, -1, UseOptionR() ? input.Length : 0);
1073 * Does the replacement
1077 /// Replaces all occurrences of the <paramref name="pattern "/>with the recent
1078 /// <paramref name="replacement"/> pattern, starting at the first character
1079 /// position<paramref name="."/>
1082 public String Replace(String input, MatchEvaluator evaluator, int count) {
1085 throw new ArgumentNullException("input");
1087 return Replace(input, evaluator, count, UseOptionR() ? input.Length : 0);
1091 * Does the replacement
1095 /// Replaces all occurrences of the (previouly defined) <paramref name="pattern "/>with
1096 /// the recent <paramref name="replacement"/> pattern, starting at the character
1097 /// position<paramref name=" startat."/>
1100 public String Replace(String input, MatchEvaluator evaluator, int count, int startat) {
1103 throw new ArgumentNullException("input");
1105 return RegexReplacement.Replace(evaluator, this, input, count, startat);
1109 * Static version of simple Split call
1113 /// Splits the <paramref name="input "/>string at the position defined
1114 /// by <paramref name="pattern"/>.
1117 public static String[] Split(String input, String pattern) {
1118 return Split(input, pattern, RegexOptions.None, DefaultMatchTimeout);
1122 * Static version of simple Split call
1126 /// Splits the <paramref name="input "/>string at the position defined by <paramref name="pattern"/>.
1129 public static String[] Split(String input, String pattern, RegexOptions options) {
1130 return Split(input, pattern, options, DefaultMatchTimeout);
1133 #if !SILVERLIGHT || FEATURE_NETCORE
1138 static String[] Split(String input, String pattern, RegexOptions options, TimeSpan matchTimeout) {
1139 return new Regex(pattern, options, matchTimeout, true).Split(input);
1147 /// Splits the <paramref name="input "/>string at the position defined by
1148 /// a previous <paramref name="pattern"/>
1152 public String[] Split(String input) {
1155 throw new ArgumentNullException("input");
1157 return Split(input, 0, UseOptionR() ? input.Length : 0);
1165 /// Splits the <paramref name="input "/>string at the position defined by a previous
1166 /// <paramref name="pattern"/> .
1169 public String[] Split(String input, int count) {
1172 throw new ArgumentNullException("input");
1174 return RegexReplacement.Split(this, input, count, UseOptionR() ? input.Length : 0);
1182 /// Splits the <paramref name="input "/>string at the position defined by a previous
1183 /// <paramref name="pattern"/> .
1186 public String[] Split(String input, int count, int startat) {
1188 throw new ArgumentNullException("input");
1190 return RegexReplacement.Split(this, input, count, startat);
1195 #if !(SILVERLIGHT || FULL_AOT_RUNTIME)
1198 #if !DISABLE_CAS_USE
1199 [HostProtection(MayLeakOnAbort=true)]
1201 [ResourceExposure(ResourceScope.Machine)] // The AssemblyName is interesting.
1202 [ResourceConsumption(ResourceScope.Machine)]
1203 [SuppressMessage("Microsoft.Naming","CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId="assemblyname", Justification="[....]: already shipped since v1 - can't fix without causing a breaking change")]
1204 public static void CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname) {
1206 CompileToAssemblyInternal(regexinfos, assemblyname, null, null);
1211 #if !DISABLE_CAS_USE
1212 [HostProtection(MayLeakOnAbort=true)]
1214 [ResourceExposure(ResourceScope.Machine)] // The AssemblyName is interesting.
1215 [ResourceConsumption(ResourceScope.Machine)]
1216 [SuppressMessage("Microsoft.Naming","CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId="assemblyname", Justification="[....]: already shipped since v1 - can't fix without causing a breaking change")]
1217 public static void CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname, CustomAttributeBuilder[] attributes) {
1218 CompileToAssemblyInternal(regexinfos, assemblyname, attributes, null);
1221 #if !DISABLE_CAS_USE
1222 [HostProtection(MayLeakOnAbort=true)]
1224 [ResourceExposure(ResourceScope.Machine)]
1225 [ResourceConsumption(ResourceScope.Machine)]
1226 [SuppressMessage("Microsoft.Naming","CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId="assemblyname", Justification="[....]: already shipped since v1 - can't fix without causing a breaking change")]
1227 public static void CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname, CustomAttributeBuilder[] attributes, String resourceFile) {
1228 CompileToAssemblyInternal(regexinfos, assemblyname, attributes, resourceFile);
1231 [ResourceExposure(ResourceScope.Machine)] // AssemblyName & resourceFile
1232 [ResourceConsumption(ResourceScope.Machine)]
1233 private static void CompileToAssemblyInternal (RegexCompilationInfo[] regexinfos, AssemblyName assemblyname, CustomAttributeBuilder[] attributes, String resourceFile) {
1234 if (assemblyname == null)
1235 throw new ArgumentNullException("assemblyname");
1237 if (regexinfos == null)
1238 throw new ArgumentNullException("regexinfos");
1240 RegexCompiler.CompileToAssembly(regexinfos, assemblyname, attributes, resourceFile);
1247 protected void InitializeReferences() {
1248 if (refsInitialized)
1249 throw new NotSupportedException(SR.GetString(SR.OnlyAllowedOnce));
1251 refsInitialized = true;
1252 runnerref = new ExclusiveReference();
1253 replref = new SharedReference();
1258 * Internal worker called by all the public APIs
1260 internal Match Run(bool quick, int prevlen, String input, int beginning, int length, int startat) {
1262 RegexRunner runner = null;
1264 if (startat < 0 || startat > input.Length)
1265 throw new ArgumentOutOfRangeException("start", SR.GetString(SR.BeginIndexNotNegative));
1267 if (length < 0 || length > input.Length)
1268 throw new ArgumentOutOfRangeException("length", SR.GetString(SR.LengthNotNegative));
1270 // There may be a cached runner; grab ownership of it if we can.
1272 runner = (RegexRunner)runnerref.Get();
1274 // Create a RegexRunner instance if we need to
1276 if (runner == null) {
1277 // Use the compiled RegexRunner factory if the code was compiled to MSIL
1279 if (factory != null)
1280 runner = factory.CreateInstance();
1282 runner = new RegexInterpreter(code, UseOptionInvariant() ? CultureInfo.InvariantCulture : CultureInfo.CurrentCulture);
1286 // Do the scan starting at the requested position
1287 match = runner.Scan(this, input, beginning, beginning + length, startat, prevlen, quick, internalMatchTimeout);
1289 // Release or fill the cache slot
1290 runnerref.Release(runner);
1294 if (Debug && match != null)
1301 * Find code cache based on options+pattern
1303 private static CachedCodeEntry LookupCachedAndUpdate(String key) {
1305 for (LinkedListNode<CachedCodeEntry> current = livecode.First; current != null; current = current.Next) {
1306 if (current.Value._key == key) {
1307 // If we find an entry in the cache, move it to the head at the same time.
1308 livecode.Remove(current);
1309 livecode.AddFirst(current);
1310 return current.Value;
1319 * Add current code to the cache
1321 private CachedCodeEntry CacheCode(String key) {
1322 CachedCodeEntry newcached = null;
1325 // first look for it in the cache and move it to the head
1326 for (LinkedListNode<CachedCodeEntry> current = livecode.First; current != null; current = current.Next) {
1327 if (current.Value._key == key) {
1328 livecode.Remove(current);
1329 livecode.AddFirst(current);
1330 return current.Value;
1334 // it wasn't in the cache, so we'll add a new one. Shortcut out for the case where cacheSize is zero.
1335 if (cacheSize != 0) {
1336 newcached = new CachedCodeEntry(key, capnames, capslist, code, caps, capsize, runnerref, replref);
1337 livecode.AddFirst(newcached);
1338 if (livecode.Count > cacheSize)
1339 livecode.RemoveLast();
1346 #if !(SILVERLIGHT||FULL_AOT_RUNTIME)
1348 * True if the O option was set
1353 protected bool UseOptionC() {
1354 /* Mono: Set to false until we investigate https://bugzilla.xamarin.com/show_bug.cgi?id=25671 */
1356 return(roptions & RegexOptions.Compiled) != 0;
1361 * True if the L option was set
1366 protected bool UseOptionR() {
1367 return(roptions & RegexOptions.RightToLeft) != 0;
1370 internal bool UseOptionInvariant() {
1371 return(roptions & RegexOptions.CultureInvariant) != 0;
1377 * True if the regex has debugging enabled
1382 internal bool Debug {
1384 return(roptions & RegexOptions.Debug) != 0;
1400 public delegate String MatchEvaluator(Match match);
1404 * Used to cache byte codes or compiled factories
1406 internal sealed class CachedCodeEntry {
1407 internal string _key;
1408 internal RegexCode _code;
1410 internal Dictionary<Int32, Int32> _caps;
1411 internal Dictionary<String, Int32> _capnames;
1413 internal Hashtable _caps;
1414 internal Hashtable _capnames;
1416 internal String[] _capslist;
1417 internal int _capsize;
1418 internal RegexRunnerFactory _factory;
1419 internal ExclusiveReference _runnerref;
1420 internal SharedReference _replref;
1423 internal CachedCodeEntry(string key, Dictionary<String, Int32> capnames, String[] capslist, RegexCode code, Dictionary<Int32, Int32> caps, int capsize, ExclusiveReference runner, SharedReference repl)
1425 internal CachedCodeEntry(string key, Hashtable capnames, String[] capslist, RegexCode code, Hashtable caps, int capsize, ExclusiveReference runner, SharedReference repl)
1430 _capnames = capnames;
1431 _capslist = capslist;
1437 _runnerref = runner;
1442 internal void AddCompiled(RegexRunnerFactory factory) {
1450 * Used to cache one exclusive runner reference
1452 internal sealed class ExclusiveReference {
1458 * Return an object and grab an exclusive lock.
1460 * If the exclusive lock can't be obtained, null is returned;
1461 * if the object can't be returned, the lock is released.
1464 internal Object Get() {
1465 // try to obtain the lock
1467 if (0 == Interlocked.Exchange(ref _locked, 1)) {
1473 // release the lock and return null if no reference
1480 // remember the reference and keep the lock
1490 * Release an object back to the cache
1492 * If the object is the one that's under lock, the lock
1495 * If there is no cached object, then the lock is obtained
1496 * and the object is placed in the cache.
1499 internal void Release(Object obj) {
1501 throw new ArgumentNullException("obj");
1503 // if this reference owns the lock, release it
1511 // if no reference owns the lock, try to cache this reference
1514 // try to obtain the lock
1516 if (0 == Interlocked.Exchange(ref _locked, 1)) {
1517 // if there's really no reference, cache this reference
1520 _ref = (RegexRunner) obj;
1532 * Used to cache a weak reference in a threadsafe way
1534 internal sealed class SharedReference {
1535 WeakReference _ref = new WeakReference(null);
1539 * Return an object from a weakref, protected by a lock.
1541 * If the exclusive lock can't be obtained, null is returned;
1543 * Note that _ref.Target is referenced only under the protection
1544 * of the lock. (Is this necessary?)
1546 internal Object Get() {
1547 if (0 == Interlocked.Exchange(ref _locked, 1)) {
1548 Object obj = _ref.Target;
1557 * Suggest an object into a weakref, protected by a lock.
1559 * Note that _ref.Target is referenced only under the protection
1560 * of the lock. (Is this necessary?)
1562 internal void Cache(Object obj) {
1563 if (0 == Interlocked.Exchange(ref _locked, 1)) {