3 // namespace: System.Text.RegularExpressions
6 // author: Dan Lewis (dlewis@gmx.co.uk)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Reflection;
34 using System.Reflection.Emit;
35 using System.Runtime.Serialization;
37 using RegularExpression = System.Text.RegularExpressions.Syntax.RegularExpression;
38 using Parser = System.Text.RegularExpressions.Syntax.Parser;
40 using System.Diagnostics;
43 namespace System.Text.RegularExpressions {
46 public partial class Regex : ISerializable {
50 public static void CompileToAssembly (RegexCompilationInfo [] regexes, AssemblyName aname)
52 Regex.CompileToAssembly(regexes, aname, new CustomAttributeBuilder [] {}, null);
56 public static void CompileToAssembly (RegexCompilationInfo [] regexes, AssemblyName aname,
57 CustomAttributeBuilder [] attribs)
59 Regex.CompileToAssembly(regexes, aname, attribs, null);
63 public static void CompileToAssembly (RegexCompilationInfo [] regexes, AssemblyName aname,
64 CustomAttributeBuilder [] attribs, string resourceFile)
66 throw new NotImplementedException ();
67 // TODO : Make use of attribs and resourceFile parameters
69 AssemblyBuilder asmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly (aname, AssemblyBuilderAccess.RunAndSave);
70 ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule("InnerRegexModule",aname.Name);
71 Parser psr = new Parser ();
73 System.Console.WriteLine("CompileToAssembly");
75 for(int i=0; i < regexes.Length; i++)
77 System.Console.WriteLine("Compiling expression :" + regexes[i].Pattern);
78 RegularExpression re = psr.ParseRegularExpression (regexes[i].Pattern, regexes[i].Options);
82 CILCompiler cmp = new CILCompiler (modBuilder, i);
83 bool reverse = (regexes[i].Options & RegexOptions.RightToLeft) !=0;
84 re.Compile (cmp, reverse);
90 // Define a runtime class with specified name and attributes.
91 TypeBuilder builder = modBuilder.DefineType("ITest");
93 asmBuilder.Save(aname.Name);
98 public static string Escape (string str)
101 throw new ArgumentNullException ("str");
102 return Parser.Escape (str);
105 public static string Unescape (string str)
108 throw new ArgumentNullException ("str");
109 return Parser.Unescape (str);
112 public static bool IsMatch (string input, string pattern)
114 return IsMatch (input, pattern, RegexOptions.None);
117 public static bool IsMatch (string input, string pattern, RegexOptions options)
119 Regex re = new Regex (pattern, options);
120 return re.IsMatch (input);
123 public static Match Match (string input, string pattern)
125 return Regex.Match (input, pattern, RegexOptions.None);
128 public static Match Match (string input, string pattern, RegexOptions options)
130 Regex re = new Regex (pattern, options);
131 return re.Match (input);
134 public static MatchCollection Matches (string input, string pattern)
136 return Matches (input, pattern, RegexOptions.None);
139 public static MatchCollection Matches (string input, string pattern, RegexOptions options)
141 Regex re = new Regex (pattern, options);
142 return re.Matches (input);
145 public static string Replace (string input, string pattern, MatchEvaluator evaluator)
147 return Regex.Replace (input, pattern, evaluator, RegexOptions.None);
150 public static string Replace (string input, string pattern, MatchEvaluator evaluator,
151 RegexOptions options)
153 Regex re = new Regex (pattern, options);
154 return re.Replace (input, evaluator);
157 public static string Replace (string input, string pattern, string replacement)
159 return Regex.Replace (input, pattern, replacement, RegexOptions.None);
162 public static string Replace (string input, string pattern, string replacement,
163 RegexOptions options)
165 Regex re = new Regex (pattern, options);
166 return re.Replace (input, replacement);
169 public static string [] Split (string input, string pattern)
171 return Regex.Split (input, pattern, RegexOptions.None);
174 public static string [] Split (string input, string pattern, RegexOptions options)
176 Regex re = new Regex (pattern, options);
177 return re.Split (input);
180 static FactoryCache cache = new FactoryCache (15);
181 public static int CacheSize {
182 get { return cache.Capacity; }
185 throw new ArgumentOutOfRangeException ("CacheSize");
187 cache.Capacity = value;
196 // This constructor is used by compiled regular expressions that are
197 // classes derived from Regex class. No initialization required.
202 public Regex (string pattern) : this (pattern, RegexOptions.None)
206 public Regex (string pattern, RegexOptions options)
209 throw new ArgumentNullException ("pattern");
210 validate_options (options);
211 this.pattern = pattern;
212 this.roptions = options;
216 static void validate_options (RegexOptions options)
218 const RegexOptions allopts =
220 RegexOptions.IgnoreCase |
221 RegexOptions.Multiline |
222 RegexOptions.ExplicitCapture |
223 #if MOBILE || !NET_2_1
224 RegexOptions.Compiled |
226 RegexOptions.Singleline |
227 RegexOptions.IgnorePatternWhitespace |
228 RegexOptions.RightToLeft |
229 RegexOptions.ECMAScript |
230 RegexOptions.CultureInvariant;
232 const RegexOptions ecmaopts =
233 RegexOptions.IgnoreCase |
234 RegexOptions.Multiline |
235 #if MOBILE || !NET_2_1
236 RegexOptions.Compiled |
238 RegexOptions.ECMAScript;
240 if ((options & ~allopts) != 0)
241 throw new ArgumentOutOfRangeException ("options");
242 if ((options & RegexOptions.ECMAScript) != 0 && (options & ~ecmaopts) != 0)
243 throw new ArgumentOutOfRangeException ("options");
249 this.machineFactory = cache.Lookup (this.pattern, this.roptions);
251 if (this.machineFactory == null) {
254 this.group_count = this.machineFactory.GroupCount;
255 this.gap = this.machineFactory.Gap;
256 this.mapping = this.machineFactory.Mapping;
257 this.group_names = this.machineFactory.NamesMapping;
262 private void InitNewRegex ()
264 this.machineFactory = CreateMachineFactory (this.pattern, this.roptions);
265 cache.Add (this.pattern, this.roptions, this.machineFactory);
266 this.group_count = machineFactory.GroupCount;
267 this.gap = this.machineFactory.Gap;
268 this.mapping = machineFactory.Mapping;
269 this.group_names = this.machineFactory.NamesMapping;
273 // The new rx engine seems to be working now, but
274 // potential problems are being tracked down here:
275 // https://bugzilla.novell.com/show_bug.cgi?id=470827
276 static readonly bool old_rx =
277 Environment.GetEnvironmentVariable ("MONO_NEW_RX") == null;
280 private static IMachineFactory CreateMachineFactory (string pattern, RegexOptions options)
282 Parser psr = new Parser ();
283 RegularExpression re = psr.ParseRegularExpression (pattern, options);
286 ICompiler cmp = new PatternCompiler ();
290 if ((options & RegexOptions.Compiled) != 0)
291 cmp = new CILCompiler ();
293 cmp = new RxCompiler ();
295 cmp = new PatternCompiler ();
299 re.Compile (cmp, (options & RegexOptions.RightToLeft) != 0);
301 IMachineFactory machineFactory = cmp.GetMachineFactory ();
302 Hashtable mapping = new Hashtable ();
303 machineFactory.Gap = psr.GetMapping (mapping);
304 machineFactory.Mapping = mapping;
305 machineFactory.NamesMapping = GetGroupNamesArray (machineFactory.GroupCount, machineFactory.Mapping);
307 return machineFactory;
310 protected Regex (SerializationInfo info, StreamingContext context) :
311 this (info.GetString ("pattern"),
312 (RegexOptions) info.GetValue ("options", typeof (RegexOptions)))
316 // public instance properties
318 public RegexOptions Options {
319 get { return roptions; }
322 public bool RightToLeft {
323 get { return (roptions & RegexOptions.RightToLeft) != 0; }
326 // public instance methods
328 public string [] GetGroupNames ()
330 string [] names = new string [1 + group_count];
331 Array.Copy (group_names, names, 1 + group_count);
335 public int [] GetGroupNumbers ()
337 int [] numbers = new int [1 + group_count];
338 Array.Copy (GroupNumbers, numbers, 1 + group_count);
342 public string GroupNameFromNumber (int i)
344 i = GetGroupIndex (i);
348 return group_names [i];
351 public int GroupNumberFromName (string name)
353 if (!mapping.Contains (name))
355 int i = (int) mapping [name];
357 i = Int32.Parse (name);
361 internal int GetGroupIndex (int number)
365 if (gap > group_count)
367 return Array.BinarySearch (GroupNumbers, gap, group_count - gap + 1, number);
370 int default_startat (string input)
372 return (RightToLeft && input != null) ? input.Length : 0;
377 public bool IsMatch (string input)
379 return IsMatch (input, default_startat (input));
382 public bool IsMatch (string input, int startat)
384 return Match (input, startat).Success;
387 public Match Match (string input)
389 return Match (input, default_startat (input));
392 public Match Match (string input, int startat)
395 throw new ArgumentNullException ("input");
396 if (startat < 0 || startat > input.Length)
397 throw new ArgumentOutOfRangeException ("startat");
398 return CreateMachine ().Scan (this, input, startat, input.Length);
401 public Match Match (string input, int beginning, int length)
404 throw new ArgumentNullException ("input");
405 if (beginning < 0 || beginning > input.Length)
406 throw new ArgumentOutOfRangeException ("beginning");
407 if (length < 0 || length > input.Length - beginning)
408 throw new ArgumentOutOfRangeException ("length");
409 return CreateMachine ().Scan (this, input, beginning, beginning + length);
412 public MatchCollection Matches (string input)
414 return Matches (input, default_startat (input));
417 public MatchCollection Matches (string input, int startat)
419 Match m = Match (input, startat);
420 return new MatchCollection (m);
425 public string Replace (string input, MatchEvaluator evaluator)
427 return Replace (input, evaluator, Int32.MaxValue, default_startat (input));
430 public string Replace (string input, MatchEvaluator evaluator, int count)
432 return Replace (input, evaluator, count, default_startat (input));
437 public Adapter (MatchEvaluator ev) { this.ev = ev; }
438 public void Evaluate (Match m, StringBuilder sb) { sb.Append (ev (m)); }
441 public string Replace (string input, MatchEvaluator evaluator, int count, int startat)
444 throw new ArgumentNullException ("input");
445 if (evaluator == null)
446 throw new ArgumentNullException ("evaluator");
448 throw new ArgumentOutOfRangeException ("count");
449 if (startat < 0 || startat > input.Length)
450 throw new ArgumentOutOfRangeException ("startat");
452 BaseMachine m = (BaseMachine)CreateMachine ();
455 return m.RTLReplace (this, input, evaluator, count, startat);
457 // NOTE: If this is a cause of a lot of allocations, we can convert it to
458 // use a ThreadStatic allocation mitigator
459 Adapter a = new Adapter (evaluator);
461 return m.LTRReplace (this, input, new BaseMachine.MatchAppendEvaluator (a.Evaluate),
465 public string Replace (string input, string replacement)
467 return Replace (input, replacement, Int32.MaxValue, default_startat (input));
470 public string Replace (string input, string replacement, int count)
472 return Replace (input, replacement, count, default_startat (input));
475 public string Replace (string input, string replacement, int count, int startat)
478 throw new ArgumentNullException ("input");
479 if (replacement == null)
480 throw new ArgumentNullException ("replacement");
482 throw new ArgumentOutOfRangeException ("count");
483 if (startat < 0 || startat > input.Length)
484 throw new ArgumentOutOfRangeException ("startat");
486 return CreateMachine ().Replace (this, input, replacement, count, startat);
491 public string [] Split (string input)
493 return Split (input, Int32.MaxValue, default_startat (input));
496 public string [] Split (string input, int count)
498 return Split (input, count, default_startat (input));
501 public string [] Split (string input, int count, int startat)
504 throw new ArgumentNullException ("input");
506 throw new ArgumentOutOfRangeException ("count");
507 if (startat < 0 || startat > input.Length)
508 throw new ArgumentOutOfRangeException ("startat");
510 return CreateMachine ().Split (this, input, count, startat);
513 // This method is called at the end of the constructor of compiled
514 // regular expression classes to do internal initialization.
515 protected void InitializeReferences ()
518 throw new NotSupportedException ("This operation is only allowed once per object.");
520 refsInitialized = true;
522 // Compile pattern that results in performance loss as existing
523 // CIL code is ignored but provides support for regular
524 // expressions compiled to assemblies.
528 protected bool UseOptionC ()
530 return ((roptions & RegexOptions.Compiled) != 0);
533 protected bool UseOptionR ()
535 return ((roptions & RegexOptions.RightToLeft) != 0);
540 public override string ToString ()
545 // ISerializable interface
546 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
548 info.AddValue ("pattern", this.ToString (), typeof (string));
549 info.AddValue ("options", this.Options, typeof (RegexOptions));
554 internal int GroupCount {
555 get { return group_count; }
564 private IMachine CreateMachine ()
566 return machineFactory.NewInstance ();
569 private static string [] GetGroupNamesArray (int groupCount, IDictionary mapping)
571 string [] group_names = new string [groupCount + 1];
572 IDictionaryEnumerator de = mapping.GetEnumerator ();
573 while (de.MoveNext ())
574 group_names [(int) de.Value] = (string) de.Key;
578 private int [] GroupNumbers {
580 if (group_numbers == null) {
581 group_numbers = new int [1 + group_count];
582 for (int i = 0; i < gap; ++i)
583 group_numbers [i] = i;
584 for (int i = gap; i <= group_count; ++i)
585 group_numbers [i] = Int32.Parse (group_names [i]);
586 return group_numbers;
588 return group_numbers;
592 private IMachineFactory machineFactory;
593 private IDictionary mapping;
594 private int group_count;
596 private bool refsInitialized;
597 private string [] group_names;
598 private int [] group_numbers;
602 protected internal string pattern;
603 protected internal RegexOptions roptions;
605 // MS undocumented members
608 internal System.Collections.Generic.Dictionary<string, int> capnames;
610 internal System.Collections.Generic.Dictionary<int, int> caps;
613 protected internal System.Collections.Hashtable capnames;
615 protected internal System.Collections.Hashtable caps;
618 protected internal RegexRunnerFactory factory;
621 protected internal int capsize;
623 protected internal string [] capslist;