do not check order sequence if option /order was not used
[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                 static FactoryCache cache = new FactoryCache (15);
181                 public static int CacheSize {
182                         get { return cache.Capacity; }
183                         set {
184                                 if (value < 0)
185                                         throw new ArgumentOutOfRangeException ("CacheSize");
186
187                                 cache.Capacity = value; 
188                         }
189                 }
190
191                 // private
192
193
194                 // constructors
195
196                 // This constructor is used by compiled regular expressions that are
197                 // classes derived from Regex class. No initialization required.
198                 protected Regex ()
199                 {
200                 }
201
202                 public Regex (string pattern) : this (pattern, RegexOptions.None)
203                 {
204                 }
205
206                 public Regex (string pattern, RegexOptions options)
207                 {
208                         if (pattern == null)
209                                 throw new ArgumentNullException ("pattern");
210                         validate_options (options);
211                         this.pattern = pattern;
212                         this.roptions = options;
213                         Init ();
214                 }
215
216                 static void validate_options (RegexOptions options)
217                 {
218                         const RegexOptions allopts =
219                                 RegexOptions.None |
220                                 RegexOptions.IgnoreCase |
221                                 RegexOptions.Multiline |
222                                 RegexOptions.ExplicitCapture |
223 #if MOBILE || !NET_2_1
224                                 RegexOptions.Compiled |
225 #endif
226                                 RegexOptions.Singleline |
227                                 RegexOptions.IgnorePatternWhitespace |
228                                 RegexOptions.RightToLeft |
229                                 RegexOptions.ECMAScript |
230                                 RegexOptions.CultureInvariant;
231
232                         const RegexOptions ecmaopts =
233                                 RegexOptions.IgnoreCase |
234                                 RegexOptions.Multiline |
235 #if MOBILE || !NET_2_1
236                                 RegexOptions.Compiled |
237 #endif
238                                 RegexOptions.ECMAScript;
239
240                         if ((options & ~allopts) != 0)
241                                 throw new ArgumentOutOfRangeException ("options");
242                         if ((options & RegexOptions.ECMAScript) != 0 && (options & ~ecmaopts) != 0)
243                                 throw new ArgumentOutOfRangeException ("options");
244                 }
245
246 #if !TARGET_JVM
247                 private void Init ()
248                 {
249                         this.machineFactory = cache.Lookup (this.pattern, this.roptions);
250
251                         if (this.machineFactory == null) {
252                                 InitNewRegex();
253                         } else {
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;
258                         }
259                 }
260 #endif
261
262                 private void InitNewRegex () 
263                 {
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;
270                 }
271
272 #if !NET_2_1
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;
278 #endif
279
280                 private static IMachineFactory CreateMachineFactory (string pattern, RegexOptions options) 
281                 {
282                         Parser psr = new Parser ();
283                         RegularExpression re = psr.ParseRegularExpression (pattern, options);
284
285 #if NET_2_1
286                         ICompiler cmp = new PatternCompiler ();
287 #else
288                         ICompiler cmp;
289                         if (!old_rx) {
290                                 if ((options & RegexOptions.Compiled) != 0)
291                                         cmp = new CILCompiler ();
292                                 else
293                                         cmp = new RxCompiler ();
294                         } else {
295                                 cmp = new PatternCompiler ();
296                         }
297 #endif
298
299                         re.Compile (cmp, (options & RegexOptions.RightToLeft) != 0);
300
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);
306
307                         return machineFactory;
308                 }
309
310                 protected Regex (SerializationInfo info, StreamingContext context) :
311                         this (info.GetString ("pattern"), 
312                               (RegexOptions) info.GetValue ("options", typeof (RegexOptions)))
313                 {
314                 }
315
316                 // public instance properties
317                 
318                 public RegexOptions Options {
319                         get { return roptions; }
320                 }
321
322                 public bool RightToLeft {
323                         get { return (roptions & RegexOptions.RightToLeft) != 0; }
324                 }
325
326                 // public instance methods
327                 
328                 public string [] GetGroupNames ()
329                 {
330                         string [] names = new string [1 + group_count];
331                         Array.Copy (group_names, names, 1 + group_count);
332                         return names;
333                 }
334
335                 public int [] GetGroupNumbers ()
336                 {
337                         int [] numbers = new int [1 + group_count];
338                         Array.Copy (GroupNumbers, numbers, 1 + group_count);
339                         return numbers;
340                 }
341
342                 public string GroupNameFromNumber (int i)
343                 {
344                         i = GetGroupIndex (i);
345                         if (i < 0)
346                                 return "";
347
348                         return group_names [i];
349                 }
350
351                 public int GroupNumberFromName (string name)
352                 {
353                         if (!mapping.Contains (name))
354                                 return -1;
355                         int i = (int) mapping [name];
356                         if (i >= gap)
357                                 i = Int32.Parse (name);
358                         return i;
359                 }
360
361                 internal int GetGroupIndex (int number)
362                 {
363                         if (number < gap)
364                                 return number;
365                         if (gap > group_count)
366                                 return -1;
367                         return Array.BinarySearch (GroupNumbers, gap, group_count - gap + 1, number);
368                 }
369
370                 int default_startat (string input)
371                 {
372                         return (RightToLeft && input != null) ? input.Length : 0;
373                 }
374
375                 // match methods
376                 
377                 public bool IsMatch (string input)
378                 {
379                         return IsMatch (input, default_startat (input));
380                 }
381
382                 public bool IsMatch (string input, int startat)
383                 {
384                         return Match (input, startat).Success;
385                 }
386
387                 public Match Match (string input)
388                 {
389                         return Match (input, default_startat (input));
390                 }
391
392                 public Match Match (string input, int startat)
393                 {
394                         if (input == null)
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);
399                 }
400
401                 public Match Match (string input, int beginning, int length)
402                 {
403                         if (input == null)
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);
410                 }
411
412                 public MatchCollection Matches (string input)
413                 {
414                         return Matches (input, default_startat (input));
415                 }
416
417                 public MatchCollection Matches (string input, int startat)
418                 {
419                         Match m = Match (input, startat);
420                         return new MatchCollection (m);
421                 }
422
423                 // replace methods
424
425                 public string Replace (string input, MatchEvaluator evaluator)
426                 {
427                         return Replace (input, evaluator, Int32.MaxValue, default_startat (input));
428                 }
429
430                 public string Replace (string input, MatchEvaluator evaluator, int count)
431                 {
432                         return Replace (input, evaluator, count, default_startat (input));
433                 }
434
435                 class Adapter {
436                         MatchEvaluator ev;
437                         public Adapter (MatchEvaluator ev) { this.ev = ev; }
438                         public void Evaluate (Match m, StringBuilder sb) { sb.Append (ev (m)); }
439                 }
440
441                 public string Replace (string input, MatchEvaluator evaluator, int count, int startat)
442                 {
443                         if (input == null)
444                                 throw new ArgumentNullException ("input");
445                         if (evaluator == null)
446                                 throw new ArgumentNullException ("evaluator");
447                         if (count < -1)
448                                 throw new ArgumentOutOfRangeException ("count");
449                         if (startat < 0 || startat > input.Length)
450                                 throw new ArgumentOutOfRangeException ("startat");
451
452                         BaseMachine m = (BaseMachine)CreateMachine ();
453
454                         if (RightToLeft)
455                                 return m.RTLReplace (this, input, evaluator, count, startat);
456
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);
460
461                         return m.LTRReplace (this, input, new BaseMachine.MatchAppendEvaluator (a.Evaluate),
462                                                                  count, startat);
463                 }
464
465                 public string Replace (string input, string replacement)
466                 {
467                         return Replace (input, replacement, Int32.MaxValue, default_startat (input));
468                 }
469
470                 public string Replace (string input, string replacement, int count)
471                 {
472                         return Replace (input, replacement, count, default_startat (input));
473                 }
474
475                 public string Replace (string input, string replacement, int count, int startat)
476                 {
477                         if (input == null)
478                                 throw new ArgumentNullException ("input");
479                         if (replacement == null)
480                                 throw new ArgumentNullException ("replacement");
481                         if (count < -1)
482                                 throw new ArgumentOutOfRangeException ("count");
483                         if (startat < 0 || startat > input.Length)
484                                 throw new ArgumentOutOfRangeException ("startat");
485
486                         return CreateMachine ().Replace (this, input, replacement, count, startat);
487                 }
488
489                 // split methods
490
491                 public string [] Split (string input)
492                 {
493                         return Split (input, Int32.MaxValue, default_startat (input));
494                 }
495
496                 public string [] Split (string input, int count)
497                 {
498                         return Split (input, count, default_startat (input));
499                 }
500
501                 public string [] Split (string input, int count, int startat)
502                 {
503                         if (input == null)
504                                 throw new ArgumentNullException ("input");
505                         if (count < 0)
506                                 throw new ArgumentOutOfRangeException ("count");
507                         if (startat < 0 || startat > input.Length)
508                                 throw new ArgumentOutOfRangeException ("startat");
509
510                         return CreateMachine ().Split (this, input, count, startat);
511                 }
512
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 ()
516                 {
517                         if (refsInitialized)
518                                 throw new NotSupportedException ("This operation is only allowed once per object.");
519
520                         refsInitialized = true;
521
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.
525                         Init ();
526                 }
527 #if !NET_2_1
528                 protected bool UseOptionC ()
529                 {
530                         return ((roptions & RegexOptions.Compiled) != 0);
531                 }
532 #endif
533                 protected bool UseOptionR ()
534                 {
535                         return ((roptions & RegexOptions.RightToLeft) != 0);
536                 }
537
538                 // object methods
539                 
540                 public override string ToString ()
541                 {
542                         return pattern;
543                 }
544
545                 // ISerializable interface
546                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
547                 {
548                         info.AddValue ("pattern", this.ToString (), typeof (string));
549                         info.AddValue ("options", this.Options, typeof (RegexOptions));
550                 }
551
552                 // internal
553
554                 internal int GroupCount {
555                         get { return group_count; }
556                 }
557
558                 internal int Gap {
559                         get { return gap; }
560                 }
561
562                 // private
563
564                 private IMachine CreateMachine ()
565                 {
566                         return machineFactory.NewInstance ();
567                 }
568
569                 private static string [] GetGroupNamesArray (int groupCount, IDictionary mapping) 
570                 {
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;
575                         return group_names;
576                 }
577
578                 private int [] GroupNumbers {
579                         get {
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;
587                                 }
588                                 return group_numbers;
589                         }
590                 }
591
592                 private IMachineFactory machineFactory;
593                 private IDictionary mapping;
594                 private int group_count;
595                 private int gap;
596                 private bool refsInitialized;
597                 private string [] group_names;
598                 private int [] group_numbers;
599                 
600                 // protected members
601
602                 protected internal string pattern;
603                 protected internal RegexOptions roptions;
604                 
605                 // MS undocumented members
606 #if NET_2_1
607                 [MonoTODO]
608                 internal System.Collections.Generic.Dictionary<string, int> capnames;
609                 [MonoTODO]
610                 internal System.Collections.Generic.Dictionary<int, int> caps;
611 #else
612                 [MonoTODO]
613                 protected internal System.Collections.Hashtable capnames;
614                 [MonoTODO]
615                 protected internal System.Collections.Hashtable caps;
616
617                 [MonoTODO]
618                 protected internal RegexRunnerFactory factory;
619 #endif
620                 [MonoTODO]
621                 protected internal int capsize;
622                 [MonoTODO]
623                 protected internal string [] capslist;
624         }
625 }