3 // namespace: System.Text.RegularExpressions
\r
6 // author: Dan Lewis (dlewis@gmx.co.uk)
\r
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 namespace System.Text.RegularExpressions {
\r
35 public class Capture {
\r
37 get { return index; }
\r
41 get { return length; }
\r
44 public string Value {
\r
47 return text.Substring (index, length);
53 public override string ToString () {
\r
59 internal Capture (string text) : this (text, 0, 0) { }
\r
61 internal Capture (string text, int index, int length) {
\r
64 this.length = length;
\r
67 internal string Text {
\r
68 get { return text; }
\r
73 internal int index, length;
\r
74 internal string text;
\r
78 public class Group : Capture {
\r
79 public static Group Synchronized (Group inner) {
\r
80 return inner; // is this enough?
\r
83 public CaptureCollection Captures {
\r
84 get { return captures; }
\r
87 public bool Success {
\r
88 get { return success; }
\r
93 internal Group (string text, int[] caps) : base (text) {
\r
94 this.captures = new CaptureCollection ();
\r
96 if (caps == null || caps.Length == 0) {
\r
97 this.success = false;
\r
101 this.success = true;
\r
102 this.index = caps[0];
\r
103 this.length = caps[1];
\r
104 captures.Add (this);
\r
105 for (int i = 2; i < caps.Length; i += 2)
\r
106 captures.Add (new Capture (text, caps[i], caps[i + 1]));
\r
107 captures.Reverse ();
\r
110 internal Group (): base ("")
\r
112 captures = new CaptureCollection ();
\r
115 private bool success;
\r
116 private CaptureCollection captures;
\r
120 public class Match : Group {
\r
121 public static Match Empty {
\r
122 get { return empty; }
\r
125 public static Match Synchronized (Match inner) {
\r
126 return inner; // FIXME need to sync on machine access
\r
129 public virtual GroupCollection Groups {
\r
130 get { return groups; }
\r
133 public Match NextMatch () {
\r
137 int scan_ptr = regex.RightToLeft ? Index : Index + Length;
\r
139 // next match after an empty match: make sure scan ptr makes progress
\r
142 scan_ptr += regex.RightToLeft ? -1 : +1;
\r
144 return machine.Scan (regex, Text, scan_ptr, text_length);
\r
147 public virtual string Result (string replacement) {
\r
148 return ReplacementEvaluator.Evaluate (replacement, this);
\r
153 internal Match () : base (null, null) {
\r
155 this.machine = null;
\r
156 this.text_length = 0;
\r
157 this.groups = new GroupCollection ();
\r
162 internal Match (Regex regex, IMachine machine, string text, int text_length, int[][] grps) :
\r
163 base (text, grps[0])
\r
165 this.regex = regex;
\r
166 this.machine = machine;
\r
167 this.text_length = text_length;
\r
169 this.groups = new GroupCollection ();
\r
171 for (int i = 1; i < grps.Length; ++ i)
\r
172 groups.Add (new Group (text, grps[i]));
\r
175 internal Regex Regex {
\r
176 get { return regex; }
\r
181 private Regex regex;
\r
182 private IMachine machine;
\r
183 private int text_length;
\r
184 private GroupCollection groups;
\r
186 private static Match empty = new Match ();
\r