System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / System / System.Text.RegularExpressions / Regex.cs
1 //
2 // assembly:    System
3 // namespace:   System.Text.RegularExpressions
4 // file:        regex.cs
5 //
6 // author:      Dan Lewis (dlewis@gmx.co.uk)
7 //              (c) 2002
8
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using System;
31 using System.Text;
32 using System.Collections;
33 using System.Reflection;
34 using System.Reflection.Emit;
35 using System.Runtime.Serialization;
36
37 using RegularExpression = System.Text.RegularExpressions.Syntax.RegularExpression;
38 using Parser = System.Text.RegularExpressions.Syntax.Parser;
39
40 using System.Diagnostics;
41
42
43 namespace System.Text.RegularExpressions {
44         
45         [Serializable]
46         public partial class Regex : ISerializable {
47
48 #if !TARGET_JVM
49                 [MonoTODO]
50                 public static void CompileToAssembly (RegexCompilationInfo [] regexes, AssemblyName aname)
51                 {
52                         Regex.CompileToAssembly(regexes, aname, new CustomAttributeBuilder [] {}, null);
53                 }
54
55                 [MonoTODO]
56                 public static void CompileToAssembly (RegexCompilationInfo [] regexes, AssemblyName aname,
57                                                       CustomAttributeBuilder [] attribs)
58                 {
59                         Regex.CompileToAssembly(regexes, aname, attribs, null);
60                 }
61
62                 [MonoTODO]
63                 public static void CompileToAssembly (RegexCompilationInfo [] regexes, AssemblyName aname,
64                                                       CustomAttributeBuilder [] attribs, string resourceFile)
65                 {
66                         throw new NotImplementedException ();
67                         // TODO : Make use of attribs and resourceFile parameters
68                         /*
69                         AssemblyBuilder asmBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly (aname, AssemblyBuilderAccess.RunAndSave);
70                         ModuleBuilder modBuilder = asmBuilder.DefineDynamicModule("InnerRegexModule",aname.Name);
71                         Parser psr = new Parser ();     
72                         
73                         System.Console.WriteLine("CompileToAssembly");
74                                
75                         for(int i=0; i < regexes.Length; i++)
76                                 {
77                                         System.Console.WriteLine("Compiling expression :" + regexes[i].Pattern);
78                                         RegularExpression re = psr.ParseRegularExpression (regexes[i].Pattern, regexes[i].Options);
79                                         
80                                         // compile
81                                                                                 
82                                         CILCompiler cmp = new CILCompiler (modBuilder, i);
83                                         bool reverse = (regexes[i].Options & RegexOptions.RightToLeft) !=0;
84                                         re.Compile (cmp, reverse);
85                                         cmp.Close();
86                                         
87                                 }
88                        
89
90                         // Define a runtime class with specified name and attributes.
91                         TypeBuilder builder = modBuilder.DefineType("ITest");
92                         builder.CreateType();
93                         asmBuilder.Save(aname.Name);
94                         */
95                 }
96 #endif
97                 
98                 public static string Escape (string str)
99                 {
100                         if (str == null)
101                                 throw new ArgumentNullException ("str");
102                         return Parser.Escape (str);
103                 }
104
105                 public static string Unescape (string str)
106                 {
107                         if (str == null)
108                                 throw new ArgumentNullException ("str");
109                         return Parser.Unescape (str);
110                 }
111
112                 public static bool IsMatch (string input, string pattern)
113                 {
114                         return IsMatch (input, pattern, RegexOptions.None);
115                 }
116
117                 public static bool IsMatch (string input, string pattern, RegexOptions options)
118                 {
119                         Regex re = new Regex (pattern, options);
120                         return re.IsMatch (input);
121                 }
122
123                 public static Match Match (string input, string pattern)
124                 {
125                         return Regex.Match (input, pattern, RegexOptions.None);
126                 }
127
128                 public static Match Match (string input, string pattern, RegexOptions options)
129                 {
130                         Regex re = new Regex (pattern, options);
131                         return re.Match (input);
132                 }
133
134                 public static MatchCollection Matches (string input, string pattern)
135                 {
136                         return Matches (input, pattern, RegexOptions.None);
137                 }
138
139                 public static MatchCollection Matches (string input, string pattern, RegexOptions options)
140                 {
141                         Regex re = new Regex (pattern, options);
142                         return re.Matches (input);
143                 }
144
145                 public static string Replace (string input, string pattern, MatchEvaluator evaluator)
146                 {
147                         return Regex.Replace (input, pattern, evaluator, RegexOptions.None);
148                 }
149
150                 public static string Replace (string input, string pattern, MatchEvaluator evaluator,
151                                               RegexOptions options)
152                 {
153                         Regex re = new Regex (pattern, options);
154                         return re.Replace (input, evaluator);
155                 }
156
157                 public static string Replace (string input, string pattern, string replacement)
158                 {
159                         return Regex.Replace (input, pattern, replacement, RegexOptions.None);
160                 }
161
162                 public static string Replace (string input, string pattern, string replacement,
163                                               RegexOptions options)
164                 {
165                         Regex re = new Regex (pattern, options);
166                         return re.Replace (input, replacement);
167                 }
168
169                 public static string [] Split (string input, string pattern)
170                 {
171                         return Regex.Split (input, pattern, RegexOptions.None);
172                 }
173
174                 public static string [] Split (string input, string pattern, RegexOptions options)
175                 {
176                         Regex re = new Regex (pattern, options);
177                         return re.Split (input);
178                 }
179
180 #if NET_2_0
181                 static FactoryCache cache = new FactoryCache (15);
182                 public static int CacheSize {
183                         get { return cache.Capacity; }
184                         set {
185                                 if (value < 0)
186                                         throw new ArgumentOutOfRangeException ("CacheSize");
187
188                                 cache.Capacity = value; 
189                         }
190                 }
191 #else
192                 static FactoryCache cache = new FactoryCache (200);
193 #endif
194
195                 // private
196
197
198                 // constructors
199
200                 // This constructor is used by compiled regular expressions that are
201                 // classes derived from Regex class. No initialization required.
202                 protected Regex ()
203                 {
204                 }
205
206                 public Regex (string pattern) : this (pattern, RegexOptions.None)
207                 {
208                 }
209
210                 public Regex (string pattern, RegexOptions options)
211                 {
212                         if (pattern == null)
213                                 throw new ArgumentNullException ("pattern");
214                         validate_options (options);
215                         this.pattern = pattern;
216                         this.roptions = options;
217                         Init ();
218                 }
219
220                 static void validate_options (RegexOptions options)
221                 {
222                         const RegexOptions allopts =
223                                 RegexOptions.None |
224                                 RegexOptions.IgnoreCase |
225                                 RegexOptions.Multiline |
226                                 RegexOptions.ExplicitCapture |
227 #if MOBILE || !NET_2_1
228                                 RegexOptions.Compiled |
229 #endif
230                                 RegexOptions.Singleline |
231                                 RegexOptions.IgnorePatternWhitespace |
232                                 RegexOptions.RightToLeft |
233                                 RegexOptions.ECMAScript |
234                                 RegexOptions.CultureInvariant;
235
236                         const RegexOptions ecmaopts =
237                                 RegexOptions.IgnoreCase |
238                                 RegexOptions.Multiline |
239 #if MOBILE || !NET_2_1
240                                 RegexOptions.Compiled |
241 #endif
242                                 RegexOptions.ECMAScript;
243
244                         if ((options & ~allopts) != 0)
245                                 throw new ArgumentOutOfRangeException ("options");
246                         if ((options & RegexOptions.ECMAScript) != 0 && (options & ~ecmaopts) != 0)
247                                 throw new ArgumentOutOfRangeException ("options");
248                 }
249
250 #if !TARGET_JVM
251                 private void Init ()
252                 {
253                         this.machineFactory = cache.Lookup (this.pattern, this.roptions);
254
255                         if (this.machineFactory == null) {
256                                 InitNewRegex();
257                         } else {
258                                 this.group_count = this.machineFactory.GroupCount;
259                                 this.gap = this.machineFactory.Gap;
260                                 this.mapping = this.machineFactory.Mapping;
261                                 this.group_names = this.machineFactory.NamesMapping;
262                         }
263                 }
264 #endif
265
266                 private void InitNewRegex () 
267                 {
268                         this.machineFactory = CreateMachineFactory (this.pattern, this.roptions);
269                         cache.Add (this.pattern, this.roptions, this.machineFactory);
270                         this.group_count = machineFactory.GroupCount;
271                         this.gap = this.machineFactory.Gap;
272                         this.mapping = machineFactory.Mapping;
273                         this.group_names = this.machineFactory.NamesMapping;
274                 }
275
276 #if !NET_2_1
277                 // The new rx engine seems to be working now, but
278                 // potential problems are being tracked down here:
279                 // https://bugzilla.novell.com/show_bug.cgi?id=470827
280                 static readonly bool old_rx =
281                         Environment.GetEnvironmentVariable ("MONO_NEW_RX") == null;
282 #endif
283
284                 private static IMachineFactory CreateMachineFactory (string pattern, RegexOptions options) 
285                 {
286                         Parser psr = new Parser ();
287                         RegularExpression re = psr.ParseRegularExpression (pattern, options);
288
289 #if NET_2_1
290                         ICompiler cmp = new PatternCompiler ();
291 #else
292                         ICompiler cmp;
293                         if (!old_rx) {
294                                 if ((options & RegexOptions.Compiled) != 0)
295                                         cmp = new CILCompiler ();
296                                 else
297                                         cmp = new RxCompiler ();
298                         } else {
299                                 cmp = new PatternCompiler ();
300                         }
301 #endif
302
303                         re.Compile (cmp, (options & RegexOptions.RightToLeft) != 0);
304
305                         IMachineFactory machineFactory = cmp.GetMachineFactory ();
306                         Hashtable mapping = new Hashtable ();
307                         machineFactory.Gap = psr.GetMapping (mapping);
308                         machineFactory.Mapping = mapping;
309                         machineFactory.NamesMapping = GetGroupNamesArray (machineFactory.GroupCount, machineFactory.Mapping);
310
311                         return machineFactory;
312                 }
313
314 #if NET_2_0
315                 protected
316 #else
317                 private
318 #endif
319                 Regex (SerializationInfo info, StreamingContext context) :
320                         this (info.GetString ("pattern"), 
321                               (RegexOptions) info.GetValue ("options", typeof (RegexOptions)))
322                 {
323                 }
324
325 #if ONLY_1_1 && !TARGET_JVM
326                 // fixes public API signature
327                 ~Regex ()
328                 {
329                 }
330 #endif
331                 // public instance properties
332                 
333                 public RegexOptions Options {
334                         get { return roptions; }
335                 }
336
337                 public bool RightToLeft {
338                         get { return (roptions & RegexOptions.RightToLeft) != 0; }
339                 }
340
341                 // public instance methods
342                 
343                 public string [] GetGroupNames ()
344                 {
345                         string [] names = new string [1 + group_count];
346                         Array.Copy (group_names, names, 1 + group_count);
347                         return names;
348                 }
349
350                 public int [] GetGroupNumbers ()
351                 {
352                         int [] numbers = new int [1 + group_count];
353                         Array.Copy (GroupNumbers, numbers, 1 + group_count);
354                         return numbers;
355                 }
356
357                 public string GroupNameFromNumber (int i)
358                 {
359                         i = GetGroupIndex (i);
360                         if (i < 0)
361                                 return "";
362
363                         return group_names [i];
364                 }
365
366                 public int GroupNumberFromName (string name)
367                 {
368                         if (!mapping.Contains (name))
369                                 return -1;
370                         int i = (int) mapping [name];
371                         if (i >= gap)
372                                 i = Int32.Parse (name);
373                         return i;
374                 }
375
376                 internal int GetGroupIndex (int number)
377                 {
378                         if (number < gap)
379                                 return number;
380                         if (gap > group_count)
381                                 return -1;
382                         return Array.BinarySearch (GroupNumbers, gap, group_count - gap + 1, number);
383                 }
384
385                 int default_startat (string input)
386                 {
387                         return (RightToLeft && input != null) ? input.Length : 0;
388                 }
389
390                 // match methods
391                 
392                 public bool IsMatch (string input)
393                 {
394                         return IsMatch (input, default_startat (input));
395                 }
396
397                 public bool IsMatch (string input, int startat)
398                 {
399                         return Match (input, startat).Success;
400                 }
401
402                 public Match Match (string input)
403                 {
404                         return Match (input, default_startat (input));
405                 }
406
407                 public Match Match (string input, int startat)
408                 {
409                         if (input == null)
410                                 throw new ArgumentNullException ("input");
411                         if (startat < 0 || startat > input.Length)
412                                 throw new ArgumentOutOfRangeException ("startat");
413                         return CreateMachine ().Scan (this, input, startat, input.Length);
414                 }
415
416                 public Match Match (string input, int beginning, int length)
417                 {
418                         if (input == null)
419                                 throw new ArgumentNullException ("input");
420                         if (beginning < 0 || beginning > input.Length)
421                                 throw new ArgumentOutOfRangeException ("beginning");
422                         if (length < 0 || length > input.Length - beginning)
423                                 throw new ArgumentOutOfRangeException ("length");
424                         return CreateMachine ().Scan (this, input, beginning, beginning + length);
425                 }
426
427                 public MatchCollection Matches (string input)
428                 {
429                         return Matches (input, default_startat (input));
430                 }
431
432                 public MatchCollection Matches (string input, int startat)
433                 {
434                         Match m = Match (input, startat);
435                         return new MatchCollection (m);
436                 }
437
438                 // replace methods
439
440                 public string Replace (string input, MatchEvaluator evaluator)
441                 {
442                         return Replace (input, evaluator, Int32.MaxValue, default_startat (input));
443                 }
444
445                 public string Replace (string input, MatchEvaluator evaluator, int count)
446                 {
447                         return Replace (input, evaluator, count, default_startat (input));
448                 }
449
450                 class Adapter {
451                         MatchEvaluator ev;
452                         public Adapter (MatchEvaluator ev) { this.ev = ev; }
453                         public void Evaluate (Match m, StringBuilder sb) { sb.Append (ev (m)); }
454                 }
455
456                 public string Replace (string input, MatchEvaluator evaluator, int count, int startat)
457                 {
458                         if (input == null)
459                                 throw new ArgumentNullException ("input");
460                         if (evaluator == null)
461                                 throw new ArgumentNullException ("evaluator");
462                         if (count < -1)
463                                 throw new ArgumentOutOfRangeException ("count");
464                         if (startat < 0 || startat > input.Length)
465                                 throw new ArgumentOutOfRangeException ("startat");
466
467                         BaseMachine m = (BaseMachine)CreateMachine ();
468
469                         if (RightToLeft)
470                                 return m.RTLReplace (this, input, evaluator, count, startat);
471
472                         // NOTE: If this is a cause of a lot of allocations, we can convert it to
473                         //       use a ThreadStatic allocation mitigator
474                         Adapter a = new Adapter (evaluator);
475
476                         return m.LTRReplace (this, input, new BaseMachine.MatchAppendEvaluator (a.Evaluate),
477                                                                  count, startat);
478                 }
479
480                 public string Replace (string input, string replacement)
481                 {
482                         return Replace (input, replacement, Int32.MaxValue, default_startat (input));
483                 }
484
485                 public string Replace (string input, string replacement, int count)
486                 {
487                         return Replace (input, replacement, count, default_startat (input));
488                 }
489
490                 public string Replace (string input, string replacement, int count, int startat)
491                 {
492                         if (input == null)
493                                 throw new ArgumentNullException ("input");
494                         if (replacement == null)
495                                 throw new ArgumentNullException ("replacement");
496                         if (count < -1)
497                                 throw new ArgumentOutOfRangeException ("count");
498                         if (startat < 0 || startat > input.Length)
499                                 throw new ArgumentOutOfRangeException ("startat");
500
501                         return CreateMachine ().Replace (this, input, replacement, count, startat);
502                 }
503
504                 // split methods
505
506                 public string [] Split (string input)
507                 {
508                         return Split (input, Int32.MaxValue, default_startat (input));
509                 }
510
511                 public string [] Split (string input, int count)
512                 {
513                         return Split (input, count, default_startat (input));
514                 }
515
516                 public string [] Split (string input, int count, int startat)
517                 {
518                         if (input == null)
519                                 throw new ArgumentNullException ("input");
520                         if (count < 0)
521                                 throw new ArgumentOutOfRangeException ("count");
522                         if (startat < 0 || startat > input.Length)
523                                 throw new ArgumentOutOfRangeException ("startat");
524
525                         return CreateMachine ().Split (this, input, count, startat);
526                 }
527
528                 // This method is called at the end of the constructor of compiled
529                 // regular expression classes to do internal initialization.
530                 protected void InitializeReferences ()
531                 {
532                         if (refsInitialized)
533                                 throw new NotSupportedException ("This operation is only allowed once per object.");
534
535                         refsInitialized = true;
536
537                         // Compile pattern that results in performance loss as existing
538                         // CIL code is ignored but provides support for regular
539                         // expressions compiled to assemblies.
540                         Init ();
541                 }
542 #if !NET_2_1
543                 protected bool UseOptionC ()
544                 {
545                         return ((roptions & RegexOptions.Compiled) != 0);
546                 }
547 #endif
548                 protected bool UseOptionR ()
549                 {
550                         return ((roptions & RegexOptions.RightToLeft) != 0);
551                 }
552
553                 // object methods
554                 
555                 public override string ToString ()
556                 {
557                         return pattern;
558                 }
559
560                 // ISerializable interface
561                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
562                 {
563                         info.AddValue ("pattern", this.ToString (), typeof (string));
564                         info.AddValue ("options", this.Options, typeof (RegexOptions));
565                 }
566
567                 // internal
568
569                 internal int GroupCount {
570                         get { return group_count; }
571                 }
572
573                 internal int Gap {
574                         get { return gap; }
575                 }
576
577                 // private
578
579                 private IMachine CreateMachine ()
580                 {
581                         return machineFactory.NewInstance ();
582                 }
583
584                 private static string [] GetGroupNamesArray (int groupCount, IDictionary mapping) 
585                 {
586                         string [] group_names = new string [groupCount + 1];
587                         IDictionaryEnumerator de = mapping.GetEnumerator ();
588                         while (de.MoveNext ())
589                                 group_names [(int) de.Value] = (string) de.Key;
590                         return group_names;
591                 }
592
593                 private int [] GroupNumbers {
594                         get {
595                                 if (group_numbers == null) {
596                                         group_numbers = new int [1 + group_count];
597                                         for (int i = 0; i < gap; ++i)
598                                                 group_numbers [i] = i;
599                                         for (int i = gap; i <= group_count; ++i)
600                                                 group_numbers [i] = Int32.Parse (group_names [i]);
601                                         return group_numbers;
602                                 }
603                                 return group_numbers;
604                         }
605                 }
606
607                 private IMachineFactory machineFactory;
608                 private IDictionary mapping;
609                 private int group_count;
610                 private int gap;
611                 private bool refsInitialized;
612                 private string [] group_names;
613                 private int [] group_numbers;
614                 
615                 // protected members
616
617                 protected internal string pattern;
618                 protected internal RegexOptions roptions;
619                 
620                 // MS undocumented members
621 #if NET_2_1
622                 [MonoTODO]
623                 internal System.Collections.Generic.Dictionary<string, int> capnames;
624                 [MonoTODO]
625                 internal System.Collections.Generic.Dictionary<int, int> caps;
626 #else
627                 [MonoTODO]
628                 protected internal System.Collections.Hashtable capnames;
629                 [MonoTODO]
630                 protected internal System.Collections.Hashtable caps;
631
632                 [MonoTODO]
633                 protected internal RegexRunnerFactory factory;
634 #endif
635                 [MonoTODO]
636                 protected internal int capsize;
637                 [MonoTODO]
638                 protected internal string [] capslist;
639         }
640 }