Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[mono.git] / mcs / class / System / System.Text.RegularExpressions / Match.cs
1 //
2 // assembly:    System
3 // namespace:   System.Text.RegularExpressions
4 // file:        Match.cs
5 //
6 // author:      Dan Lewis (dlewis@gmx.co.uk)
7 //              (c) 2002
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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 namespace System.Text.RegularExpressions {
31
32         [Serializable]
33         public partial class Match : Group {
34                 public static Match Empty {
35                         get { return empty; }
36                 }
37
38                 [MonoTODO ("not thread-safe")]
39                 public static Match Synchronized (Match inner)
40                 {
41                         if (inner == null)
42                                 throw new ArgumentNullException ("inner");
43                         return inner;   // FIXME need to sync on machine access
44                 }
45                 
46                 public virtual GroupCollection Groups {
47                         get { return groups; }
48                 }
49
50                 public Match NextMatch ()
51                 {
52                         if (this == Empty)
53                                 return Empty;
54
55                         int scan_ptr = regex.RightToLeft ? Index : Index + Length;
56
57                         // next match after an empty match: make sure scan ptr makes progress
58                         
59                         if (Length == 0)
60                                 scan_ptr += regex.RightToLeft ? -1 : +1;
61
62                         return machine.Scan (regex, Text, scan_ptr, text_length);
63                 }
64
65                 public virtual string Result (string replacement)
66                 {
67                         if (replacement == null)
68                                 throw new ArgumentNullException ("replacement");
69                         if (machine == null)
70                                 throw new NotSupportedException ("Result cannot be called on failed Match.");
71
72                         return machine.Result (replacement, this);
73                 }
74
75                 // internal
76
77                 private Match ()
78                 {
79                         this.regex = null;
80                         this.machine = null;
81                         this.text_length = 0;
82
83                         this.groups = new GroupCollection (1, 1);
84                         groups.SetValue (this, 0);
85                 }
86
87                 internal Match (Regex regex, IMachine machine, string text, int text_length, int n_groups, 
88                                 int index, int length) :
89                         base (text, index, length) {
90                         this.regex = regex;
91                         this.machine = machine;
92                         this.text_length = text_length;
93                 }
94                 internal Match (Regex regex, IMachine machine, string text, int text_length, int n_groups, 
95                                 int index, int length, int n_caps) :
96                         base (text, index, length, n_caps)
97                 {
98                         this.regex = regex;
99                         this.machine = machine;
100                         this.text_length = text_length;
101
102                         this.groups = new GroupCollection (n_groups, regex.Gap);
103                         groups.SetValue (this, 0);
104                 }
105                 internal Regex Regex {
106                         get { return regex; }
107                 }
108
109                 // private
110
111                 private Regex regex;
112                 private IMachine machine;
113                 private int text_length;
114                 private GroupCollection groups;
115
116                 private static Match empty = new Match ();
117         }
118 }