**** Merged from MCS ****
[mono-project.git] / mcs / class / System / System.Text.RegularExpressions / match.cs
blob60c982a558b0b04ad1117934f80f545e6896eebc
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
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.
30 using System;
32 namespace System.Text.RegularExpressions {
34 [Serializable]
35 public class Capture {
36 public int Index {
37 get { return index; }
40 public int Length {
41 get { return length; }
44 public string Value {
45 get {
46 if (text!= null)
47 return text.Substring (index, length);
48 else
49 return String.Empty;
53 public override string ToString () {
54 return Value;
57 // internal members
59 internal Capture (string text) : this (text, 0, 0) { }
61 internal Capture (string text, int index, int length) {
62 this.text = text;
63 this.index = index;
64 this.length = length;
67 internal string Text {
68 get { return text; }
71 // private
73 internal int index, length;
74 internal string text;
77 [Serializable]
78 public class Group : Capture {
79 public static Group Synchronized (Group inner) {
80 return inner; // is this enough?
83 public CaptureCollection Captures {
84 get { return captures; }
87 public bool Success {
88 get { return success; }
91 // internal
93 internal Group (string text, int[] caps) : base (text) {
94 this.captures = new CaptureCollection ();
96 if (caps == null || caps.Length == 0) {
97 this.success = false;
98 return;
101 this.success = true;
102 this.index = caps[0];
103 this.length = caps[1];
104 captures.Add (this);
105 for (int i = 2; i < caps.Length; i += 2)
106 captures.Add (new Capture (text, caps[i], caps[i + 1]));
107 captures.Reverse ();
110 internal Group (): base ("")
112 captures = new CaptureCollection ();
115 private bool success;
116 private CaptureCollection captures;
119 [Serializable]
120 public class Match : Group {
121 public static Match Empty {
122 get { return empty; }
125 public static Match Synchronized (Match inner) {
126 return inner; // FIXME need to sync on machine access
129 public virtual GroupCollection Groups {
130 get { return groups; }
133 public Match NextMatch () {
134 if (this == Empty)
135 return Empty;
137 int scan_ptr = regex.RightToLeft ? Index : Index + Length;
139 // next match after an empty match: make sure scan ptr makes progress
141 if (Length == 0)
142 scan_ptr += regex.RightToLeft ? -1 : +1;
144 return machine.Scan (regex, Text, scan_ptr, text_length);
147 public virtual string Result (string replacement) {
148 return ReplacementEvaluator.Evaluate (replacement, this);
151 // internal
153 internal Match () : base (null, null) {
154 this.regex = null;
155 this.machine = null;
156 this.text_length = 0;
157 this.groups = new GroupCollection ();
159 groups.Add (this);
162 internal Match (Regex regex, IMachine machine, string text, int text_length, int[][] grps) :
163 base (text, grps[0])
165 this.regex = regex;
166 this.machine = machine;
167 this.text_length = text_length;
169 this.groups = new GroupCollection ();
170 groups.Add (this);
171 for (int i = 1; i < grps.Length; ++ i)
172 groups.Add (new Group (text, grps[i]));
175 internal Regex Regex {
176 get { return regex; }
179 // private
181 private Regex regex;
182 private IMachine machine;
183 private int text_length;
184 private GroupCollection groups;
186 private static Match empty = new Match ();