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();
475 [CLSCompliant (false)]
476 protected IDictionary Caps
480 var dict = new Dictionary<int, int>();
482 foreach (int key in caps.Keys)
484 dict.Add (key, (int)caps[key]);
492 throw new ArgumentNullException("value");
495 caps = new Hashtable (value.Count);
496 foreach (DictionaryEntry entry in value)
498 caps[(int)entry.Key] = (int)entry.Value;
503 [CLSCompliant (false)]
504 protected IDictionary CapNames
508 var dict = new Dictionary<string, int>();
510 foreach (string key in capnames.Keys)
512 dict.Add (key, (int)capnames[key]);
520 throw new ArgumentNullException("value");
522 capnames = new Hashtable (value.Count);
523 foreach (DictionaryEntry entry in value)
525 capnames[(string)entry.Key] = (int)entry.Value;
532 /// Returns the options passed into the constructor
535 public RegexOptions Options {
536 get { return roptions;}
541 /// The match timeout used by this Regex instance.
543 #if !SILVERLIGHT || FEATURE_NETCORE
548 TimeSpan MatchTimeout {
549 get { return internalMatchTimeout; }
554 * True if the regex is leftward
558 /// Indicates whether the regular expression matches from right to
562 public bool RightToLeft {
570 /// Returns the regular expression pattern passed into the constructor
573 public override string ToString() {
578 * Returns an array of the group names that are used to capture groups
579 * in the regular expression. Only needed if the regex is not known until
580 * runtime, and one wants to extract captured groups. (Probably unusual,
581 * but supplied for completeness.)
585 /// the GroupNameCollection for the regular expression. This collection contains the
586 /// set of strings used to name capturing groups in the expression.
588 public String[] GetGroupNames() {
591 if (capslist == null) {
593 result = new String[max];
595 for (int i = 0; i < max; i++) {
596 result[i] = Convert.ToString(i, CultureInfo.InvariantCulture);
600 result = new String[capslist.Length];
602 System.Array.Copy(capslist, 0, result, 0, capslist.Length);
609 * Returns an array of the group numbers that are used to capture groups
610 * in the regular expression. Only needed if the regex is not known until
611 * runtime, and one wants to extract captured groups. (Probably unusual,
612 * but supplied for completeness.)
616 /// the integer group number corresponding to a group name.
618 public int[] GetGroupNumbers() {
623 result = new int[max];
625 for (int i = 0; i < max; i++) {
630 result = new int[caps.Count];
632 IDictionaryEnumerator de = caps.GetEnumerator();
633 while (de.MoveNext()) {
634 result[(int)de.Value] = (int)de.Key;
642 * Given a group number, maps it to a group name. Note that nubmered
643 * groups automatically get a group name that is the decimal string
644 * equivalent of its number.
646 * Returns null if the number is not a recognized group number.
650 /// Retrieves a group name that corresponds to a group number.
653 public String GroupNameFromNumber(int i) {
654 if (capslist == null) {
655 if (i >= 0 && i < capsize)
656 return i.ToString(CultureInfo.InvariantCulture);
663 if (!caps.ContainsKey(i))
665 Object obj = caps[i];
677 if (i >= 0 && i < capslist.Length)
685 * Given a group name, maps it to a group number. Note that nubmered
686 * groups automatically get a group name that is the decimal string
687 * equivalent of its number.
689 * Returns -1 if the name is not a recognized group name.
693 /// Returns a group number that corresponds to a group name.
696 public int GroupNumberFromName(String name) {
700 throw new ArgumentNullException("name");
702 // look up name if we have a hashtable of names
703 if (capnames != null) {
705 if (!capnames.ContainsKey(name))
707 Object ret = capnames[name];
713 return capnames[name];
719 // convert to an int if it looks like a number
721 for (int i = 0; i < name.Length; i++) {
724 if (ch > '9' || ch < '0')
728 result += (ch - '0');
731 // return int if it's in range
732 if (result >= 0 && result < capsize)
739 * Static version of simple IsMatch call
743 /// Searches the input
744 /// string for one or more occurrences of the text supplied in the pattern
748 public static bool IsMatch(String input, String pattern) {
749 return IsMatch(input, pattern, RegexOptions.None, DefaultMatchTimeout);
753 * Static version of simple IsMatch call
757 /// Searches the input string for one or more occurrences of the text
758 /// supplied in the pattern parameter with matching options supplied in the options
762 public static bool IsMatch(String input, String pattern, RegexOptions options) {
763 return IsMatch(input, pattern, options, DefaultMatchTimeout);
766 #if !SILVERLIGHT || FEATURE_NETCORE
771 static bool IsMatch(String input, String pattern, RegexOptions options, TimeSpan matchTimeout) {
772 return new Regex(pattern, options, matchTimeout, true).IsMatch(input);
776 * Returns true if the regex finds a match within the specified string
780 /// Searches the input string for one or
781 /// more matches using the previous pattern, options, and starting
785 public bool IsMatch(String input) {
788 throw new ArgumentNullException("input");
790 return IsMatch(input, UseOptionR() ? input.Length : 0);
794 * Returns true if the regex finds a match after the specified position
795 * (proceeding leftward if the regex is leftward and rightward otherwise)
799 /// Searches the input
800 /// string for one or more matches using the previous pattern and options, with
801 /// a new starting position.
804 public bool IsMatch(String input, int startat) {
807 throw new ArgumentNullException("input");
809 return (null == Run(true, -1, input, 0, input.Length, startat));
813 * Static version of simple Match call
817 /// Searches the input string for one or more occurrences of the text
818 /// supplied in the pattern parameter.
821 public static Match Match(String input, String pattern) {
822 return Match(input, pattern, RegexOptions.None, DefaultMatchTimeout);
826 * Static version of simple Match call
830 /// Searches the input string for one or more occurrences of the text
831 /// supplied in the pattern parameter. Matching is modified with an option
835 public static Match Match(String input, String pattern, RegexOptions options) {
836 return Match(input, pattern, options, DefaultMatchTimeout);
840 #if !SILVERLIGHT || FEATURE_NETCORE
845 static Match Match(String input, String pattern, RegexOptions options, TimeSpan matchTimeout) {
846 return new Regex(pattern, options, matchTimeout, true).Match(input);
850 * Finds the first match for the regular expression starting at the beginning
851 * of the string (or at the end of the string if the regex is leftward)
855 /// Matches a regular expression with a string and returns
856 /// the precise result as a RegexMatch object.
859 public Match Match(String input) {
862 throw new ArgumentNullException("input");
864 return Match(input, UseOptionR() ? input.Length : 0);
868 * Finds the first match, starting at the specified position
871 /// Matches a regular expression with a string and returns
872 /// the precise result as a RegexMatch object.
874 public Match Match(String input, int startat) {
877 throw new ArgumentNullException("input");
879 return Run(false, -1, input, 0, input.Length, startat);
883 * Finds the first match, restricting the search to the specified interval of
889 /// regular expression with a string and returns the precise result as a
890 /// RegexMatch object.
893 public Match Match(String input, int beginning, int length) {
895 throw new ArgumentNullException("input");
897 return Run(false, -1, input, beginning, length, UseOptionR() ? beginning + length : beginning);
901 * Static version of simple Matches call
905 /// Returns all the successful matches as if Match were
906 /// called iteratively numerous times.
909 public static MatchCollection Matches(String input, String pattern) {
910 return Matches(input, pattern, RegexOptions.None, DefaultMatchTimeout);
914 * Static version of simple Matches call
918 /// Returns all the successful matches as if Match were called iteratively
922 public static MatchCollection Matches(String input, String pattern, RegexOptions options) {
923 return Matches(input, pattern, options, DefaultMatchTimeout);
926 #if !SILVERLIGHT || FEATURE_NETCORE
931 static MatchCollection Matches(String input, String pattern, RegexOptions options, TimeSpan matchTimeout) {
932 return new Regex(pattern, options, matchTimeout, true).Matches(input);
936 * Finds the first match for the regular expression starting at the beginning
937 * of the string Enumerator(or at the end of the string if the regex is leftward)
942 /// all the successful matches as if Match was called iteratively numerous
946 public MatchCollection Matches(String input) {
949 throw new ArgumentNullException("input");
951 return Matches(input, UseOptionR() ? input.Length : 0);
955 * Finds the first match, starting at the specified position
960 /// all the successful matches as if Match was called iteratively numerous
964 public MatchCollection Matches(String input, int startat) {
967 throw new ArgumentNullException("input");
969 return new MatchCollection(this, input, 0, input.Length, startat);
973 * Static version of simple Replace call
978 /// all occurrences of the pattern with the <paramref name="replacement"/> pattern, starting at
979 /// the first character in the input string.
982 public static String Replace(String input, String pattern, String replacement) {
983 return Replace(input, pattern, replacement, RegexOptions.None, DefaultMatchTimeout);
987 * Static version of simple Replace call
991 /// Replaces all occurrences of
992 /// the <paramref name="pattern "/>with the <paramref name="replacement "/>
993 /// pattern, starting at the first character in the input string.
996 public static String Replace(String input, String pattern, String replacement, RegexOptions options) {
997 return Replace(input, pattern, replacement, options, DefaultMatchTimeout);
1000 #if !SILVERLIGHT || FEATURE_NETCORE
1005 static String Replace(String input, String pattern, String replacement, RegexOptions options, TimeSpan matchTimeout) {
1006 return new Regex(pattern, options, matchTimeout, true).Replace(input, replacement);
1010 * Does the replacement
1014 /// Replaces all occurrences of
1015 /// the <paramref name="pattern "/> with the <paramref name="replacement"/> pattern, starting at the
1016 /// first character in the input string, using the previous patten.
1019 public String Replace(String input, String replacement) {
1022 throw new ArgumentNullException("input");
1024 return Replace(input, replacement, -1, UseOptionR() ? input.Length : 0);
1028 * Does the replacement
1032 /// Replaces all occurrences of the (previously defined) <paramref name="pattern "/>with the
1033 /// <paramref name="replacement"/> pattern, starting at the first character in the input string.
1036 public String Replace(String input, String replacement, int count) {
1039 throw new ArgumentNullException("input");
1041 return Replace(input, replacement, count, UseOptionR() ? input.Length : 0);
1045 * Does the replacement
1049 /// Replaces all occurrences of the <paramref name="pattern "/>with the recent
1050 /// <paramref name="replacement"/> pattern, starting at the character position
1051 /// <paramref name="startat."/>
1054 public String Replace(String input, String replacement, int count, int startat) {
1057 throw new ArgumentNullException("input");
1059 if (replacement == null)
1060 throw new ArgumentNullException("replacement");
1062 // a little code to grab a cached parsed replacement object
1063 RegexReplacement repl = (RegexReplacement) replref.Get();
1065 if (repl == null || !repl.Pattern.Equals(replacement)) {
1066 repl = RegexParser.ParseReplacement(replacement, caps, capsize, capnames, this.roptions);
1067 replref.Cache(repl);
1070 return repl.Replace(this, input, count, startat);
1074 * Static version of simple Replace call
1078 /// Replaces all occurrences of the <paramref name="pattern "/>with the
1079 /// <paramref name="replacement"/> pattern
1080 /// <paramref name="."/>
1083 public static String Replace(String input, String pattern, MatchEvaluator evaluator) {
1084 return Replace(input, pattern, evaluator, RegexOptions.None, DefaultMatchTimeout);
1088 * Static version of simple Replace call
1092 /// Replaces all occurrences of the <paramref name="pattern "/>with the recent
1093 /// <paramref name="replacement"/> pattern, starting at the first character<paramref name="."/>
1096 public static String Replace(String input, String pattern, MatchEvaluator evaluator, RegexOptions options) {
1097 return Replace(input, pattern, evaluator, options, DefaultMatchTimeout);
1100 #if !SILVERLIGHT || FEATURE_NETCORE
1105 static String Replace(String input, String pattern, MatchEvaluator evaluator, RegexOptions options, TimeSpan matchTimeout) {
1106 return new Regex(pattern, options, matchTimeout, true).Replace(input, evaluator);
1110 * Does the replacement
1114 /// Replaces all occurrences of the <paramref name="pattern "/>with the recent
1115 /// <paramref name="replacement"/> pattern, starting at the first character
1116 /// position<paramref name="."/>
1119 public String Replace(String input, MatchEvaluator evaluator) {
1122 throw new ArgumentNullException("input");
1124 return Replace(input, evaluator, -1, UseOptionR() ? input.Length : 0);
1128 * Does the replacement
1132 /// Replaces all occurrences of the <paramref name="pattern "/>with the recent
1133 /// <paramref name="replacement"/> pattern, starting at the first character
1134 /// position<paramref name="."/>
1137 public String Replace(String input, MatchEvaluator evaluator, int count) {
1140 throw new ArgumentNullException("input");
1142 return Replace(input, evaluator, count, UseOptionR() ? input.Length : 0);
1146 * Does the replacement
1150 /// Replaces all occurrences of the (previouly defined) <paramref name="pattern "/>with
1151 /// the recent <paramref name="replacement"/> pattern, starting at the character
1152 /// position<paramref name=" startat."/>
1155 public String Replace(String input, MatchEvaluator evaluator, int count, int startat) {
1158 throw new ArgumentNullException("input");
1160 return RegexReplacement.Replace(evaluator, this, input, count, startat);
1164 * Static version of simple Split call
1168 /// Splits the <paramref name="input "/>string at the position defined
1169 /// by <paramref name="pattern"/>.
1172 public static String[] Split(String input, String pattern) {
1173 return Split(input, pattern, RegexOptions.None, DefaultMatchTimeout);
1177 * Static version of simple Split call
1181 /// Splits the <paramref name="input "/>string at the position defined by <paramref name="pattern"/>.
1184 public static String[] Split(String input, String pattern, RegexOptions options) {
1185 return Split(input, pattern, options, DefaultMatchTimeout);
1188 #if !SILVERLIGHT || FEATURE_NETCORE
1193 static String[] Split(String input, String pattern, RegexOptions options, TimeSpan matchTimeout) {
1194 return new Regex(pattern, options, matchTimeout, true).Split(input);
1202 /// Splits the <paramref name="input "/>string at the position defined by
1203 /// a previous <paramref name="pattern"/>
1207 public String[] Split(String input) {
1210 throw new ArgumentNullException("input");
1212 return Split(input, 0, UseOptionR() ? input.Length : 0);
1220 /// Splits the <paramref name="input "/>string at the position defined by a previous
1221 /// <paramref name="pattern"/> .
1224 public String[] Split(String input, int count) {
1227 throw new ArgumentNullException("input");
1229 return RegexReplacement.Split(this, input, count, UseOptionR() ? input.Length : 0);
1237 /// Splits the <paramref name="input "/>string at the position defined by a previous
1238 /// <paramref name="pattern"/> .
1241 public String[] Split(String input, int count, int startat) {
1243 throw new ArgumentNullException("input");
1245 return RegexReplacement.Split(this, input, count, startat);
1250 #if !(SILVERLIGHT || FULL_AOT_RUNTIME)
1253 #if MONO_FEATURE_CAS
1254 [HostProtection(MayLeakOnAbort=true)]
1256 [ResourceExposure(ResourceScope.Machine)] // The AssemblyName is interesting.
1257 [ResourceConsumption(ResourceScope.Machine)]
1258 [SuppressMessage("Microsoft.Naming","CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId="assemblyname", Justification="[....]: already shipped since v1 - can't fix without causing a breaking change")]
1259 public static void CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname) {
1261 CompileToAssemblyInternal(regexinfos, assemblyname, null, null);
1266 #if MONO_FEATURE_CAS
1267 [HostProtection(MayLeakOnAbort=true)]
1269 [ResourceExposure(ResourceScope.Machine)] // The AssemblyName is interesting.
1270 [ResourceConsumption(ResourceScope.Machine)]
1271 [SuppressMessage("Microsoft.Naming","CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId="assemblyname", Justification="[....]: already shipped since v1 - can't fix without causing a breaking change")]
1272 public static void CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname, CustomAttributeBuilder[] attributes) {
1273 CompileToAssemblyInternal(regexinfos, assemblyname, attributes, null);
1276 #if MONO_FEATURE_CAS
1277 [HostProtection(MayLeakOnAbort=true)]
1279 [ResourceExposure(ResourceScope.Machine)]
1280 [ResourceConsumption(ResourceScope.Machine)]
1281 [SuppressMessage("Microsoft.Naming","CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId="assemblyname", Justification="[....]: already shipped since v1 - can't fix without causing a breaking change")]
1282 public static void CompileToAssembly(RegexCompilationInfo[] regexinfos, AssemblyName assemblyname, CustomAttributeBuilder[] attributes, String resourceFile) {
1283 CompileToAssemblyInternal(regexinfos, assemblyname, attributes, resourceFile);
1286 [ResourceExposure(ResourceScope.Machine)] // AssemblyName & resourceFile
1287 [ResourceConsumption(ResourceScope.Machine)]
1288 private static void CompileToAssemblyInternal (RegexCompilationInfo[] regexinfos, AssemblyName assemblyname, CustomAttributeBuilder[] attributes, String resourceFile) {
1289 if (assemblyname == null)
1290 throw new ArgumentNullException("assemblyname");
1292 if (regexinfos == null)
1293 throw new ArgumentNullException("regexinfos");
1295 RegexCompiler.CompileToAssembly(regexinfos, assemblyname, attributes, resourceFile);
1302 protected void InitializeReferences() {
1303 if (refsInitialized)
1304 throw new NotSupportedException(SR.GetString(SR.OnlyAllowedOnce));
1306 refsInitialized = true;
1307 runnerref = new ExclusiveReference();
1308 replref = new SharedReference();
1313 * Internal worker called by all the public APIs
1315 internal Match Run(bool quick, int prevlen, String input, int beginning, int length, int startat) {
1317 RegexRunner runner = null;
1319 if (startat < 0 || startat > input.Length)
1320 throw new ArgumentOutOfRangeException("start", SR.GetString(SR.BeginIndexNotNegative));
1322 if (length < 0 || length > input.Length)
1323 throw new ArgumentOutOfRangeException("length", SR.GetString(SR.LengthNotNegative));
1325 // There may be a cached runner; grab ownership of it if we can.
1327 runner = (RegexRunner)runnerref.Get();
1329 // Create a RegexRunner instance if we need to
1331 if (runner == null) {
1332 // Use the compiled RegexRunner factory if the code was compiled to MSIL
1334 if (factory != null)
1335 runner = factory.CreateInstance();
1337 runner = new RegexInterpreter(code, UseOptionInvariant() ? CultureInfo.InvariantCulture : CultureInfo.CurrentCulture);
1341 // Do the scan starting at the requested position
1342 match = runner.Scan(this, input, beginning, beginning + length, startat, prevlen, quick, internalMatchTimeout);
1344 // Release or fill the cache slot
1345 runnerref.Release(runner);
1349 if (Debug && match != null)
1356 * Find code cache based on options+pattern
1358 private static CachedCodeEntry LookupCachedAndUpdate(String key) {
1360 for (LinkedListNode<CachedCodeEntry> current = livecode.First; current != null; current = current.Next) {
1361 if (current.Value._key == key) {
1362 // If we find an entry in the cache, move it to the head at the same time.
1363 livecode.Remove(current);
1364 livecode.AddFirst(current);
1365 return current.Value;
1374 * Add current code to the cache
1376 private CachedCodeEntry CacheCode(String key) {
1377 CachedCodeEntry newcached = null;
1380 // first look for it in the cache and move it to the head
1381 for (LinkedListNode<CachedCodeEntry> current = livecode.First; current != null; current = current.Next) {
1382 if (current.Value._key == key) {
1383 livecode.Remove(current);
1384 livecode.AddFirst(current);
1385 return current.Value;
1389 // it wasn't in the cache, so we'll add a new one. Shortcut out for the case where cacheSize is zero.
1390 if (cacheSize != 0) {
1391 newcached = new CachedCodeEntry(key, capnames, capslist, code, caps, capsize, runnerref, replref);
1392 livecode.AddFirst(newcached);
1393 if (livecode.Count > cacheSize)
1394 livecode.RemoveLast();
1403 * True if the O option was set
1408 protected bool UseOptionC() {
1409 #if FULL_AOT_RUNTIME
1414 /* Mono: Set to false until we investigate https://bugzilla.xamarin.com/show_bug.cgi?id=25671 */
1417 return(roptions & RegexOptions.Compiled) != 0;
1424 * True if the L option was set
1429 protected bool UseOptionR() {
1430 return(roptions & RegexOptions.RightToLeft) != 0;
1433 internal bool UseOptionInvariant() {
1434 return(roptions & RegexOptions.CultureInvariant) != 0;
1440 * True if the regex has debugging enabled
1445 internal bool Debug {
1447 return(roptions & RegexOptions.Debug) != 0;
1463 public delegate String MatchEvaluator(Match match);
1467 * Used to cache byte codes or compiled factories
1469 internal sealed class CachedCodeEntry {
1470 internal string _key;
1471 internal RegexCode _code;
1473 internal Dictionary<Int32, Int32> _caps;
1474 internal Dictionary<String, Int32> _capnames;
1476 internal Hashtable _caps;
1477 internal Hashtable _capnames;
1479 internal String[] _capslist;
1480 internal int _capsize;
1481 internal RegexRunnerFactory _factory;
1482 internal ExclusiveReference _runnerref;
1483 internal SharedReference _replref;
1486 internal CachedCodeEntry(string key, Dictionary<String, Int32> capnames, String[] capslist, RegexCode code, Dictionary<Int32, Int32> caps, int capsize, ExclusiveReference runner, SharedReference repl)
1488 internal CachedCodeEntry(string key, Hashtable capnames, String[] capslist, RegexCode code, Hashtable caps, int capsize, ExclusiveReference runner, SharedReference repl)
1493 _capnames = capnames;
1494 _capslist = capslist;
1500 _runnerref = runner;
1505 internal void AddCompiled(RegexRunnerFactory factory) {
1513 * Used to cache one exclusive runner reference
1515 internal sealed class ExclusiveReference {
1521 * Return an object and grab an exclusive lock.
1523 * If the exclusive lock can't be obtained, null is returned;
1524 * if the object can't be returned, the lock is released.
1527 internal Object Get() {
1528 // try to obtain the lock
1530 if (0 == Interlocked.Exchange(ref _locked, 1)) {
1536 // release the lock and return null if no reference
1543 // remember the reference and keep the lock
1553 * Release an object back to the cache
1555 * If the object is the one that's under lock, the lock
1558 * If there is no cached object, then the lock is obtained
1559 * and the object is placed in the cache.
1562 internal void Release(Object obj) {
1564 throw new ArgumentNullException("obj");
1566 // if this reference owns the lock, release it
1574 // if no reference owns the lock, try to cache this reference
1577 // try to obtain the lock
1579 if (0 == Interlocked.Exchange(ref _locked, 1)) {
1580 // if there's really no reference, cache this reference
1583 _ref = (RegexRunner) obj;
1595 * Used to cache a weak reference in a threadsafe way
1597 internal sealed class SharedReference {
1598 WeakReference _ref = new WeakReference(null);
1602 * Return an object from a weakref, protected by a lock.
1604 * If the exclusive lock can't be obtained, null is returned;
1606 * Note that _ref.Target is referenced only under the protection
1607 * of the lock. (Is this necessary?)
1609 internal Object Get() {
1610 if (0 == Interlocked.Exchange(ref _locked, 1)) {
1611 Object obj = _ref.Target;
1620 * Suggest an object into a weakref, protected by a lock.
1622 * Note that _ref.Target is referenced only under the protection
1623 * of the lock. (Is this necessary?)
1625 internal void Cache(Object obj) {
1626 if (0 == Interlocked.Exchange(ref _locked, 1)) {