Debian Codegraph @

mcs/class/referencesource/System/regex/system/text/regularexpressions/RegexCaptureCollection.cs

raw
1 //------------------------------------------------------------------------------
2 // <copyright file="RegexCaptureCollection.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6
7 // The CaptureCollection lists the captured Capture numbers
8 // contained in a compiled Regex.
9
10 namespace System.Text.RegularExpressions {
11
12 using System.Collections;
13
14 /*
15 * This collection returns the Captures for a group
16 * in the order in which they were matched (left to right
17 * or right to left). It is created by Group.Captures
18 */
19 /// <devdoc>
20 /// <para>
21 /// Represents a sequence of capture substrings. The object is used
22 /// to return the set of captures done by a single capturing group.
23 /// </para>
24 /// </devdoc>
25 #if !SILVERLIGHT
26 [ Serializable() ]
27 #endif
28 public class CaptureCollection : ICollection {
29 internal Group _group;
30 internal int _capcount;
31 internal Capture[] _captures;
32
33 /*
34 * Nonpublic constructor
35 */
36 internal CaptureCollection(Group group) {
37 _group = group;
38 _capcount = _group._capcount;
39 }
40
41 /*
42 * The object on which to synchronize
43 */
44 /// <devdoc>
45 /// <para>[To be supplied.]</para>
46 /// </devdoc>
47 public Object SyncRoot {
48 get {
49 return _group;
50 }
51 }
52
53 /*
54 * ICollection
55 */
56 /// <devdoc>
57 /// <para>[To be supplied.]</para>
58 /// </devdoc>
59 public bool IsSynchronized {
60 get {
61 return false;
62 }
63 }
64
65 /*
66 * ICollection
67 */
68 /// <devdoc>
69 /// <para>[To be supplied.]</para>
70 /// </devdoc>
71 public bool IsReadOnly {
72 get {
73 return true;
74 }
75 }
76
77 /*
78 * The number of captures for the group
79 */
80 /// <devdoc>
81 /// <para>
82 /// Returns the number of captures.
83 /// </para>
84 /// </devdoc>
85 public int Count {
86 get {
87 return _capcount;
88 }
89 }
90
91 /*
92 * The ith capture in the group
93 */
94 /// <devdoc>
95 /// <para>
96 /// Provides a means of accessing a specific capture in the collection.
97 /// </para>
98 /// </devdoc>
99 public Capture this[int i]
100 {
101 get {
102 return GetCapture(i);
103 }
104 }
105
106 /*
107 * As required by ICollection
108 */
109 /// <devdoc>
110 /// <para>
111 /// Copies all the elements of the collection to the given array
112 /// beginning at the given index.
113 /// </para>
114 /// </devdoc>
115 public void CopyTo(Array array, int arrayIndex) {
116 if (array == null)
117 throw new ArgumentNullException("array");
118
119 for (int i = arrayIndex, j = 0; j < Count; i++, j++) {
120 array.SetValue(this[j], i);
121 }
122 }
123
124 /*
125 * As required by ICollection
126 */
127 /// <devdoc>
128 /// <para>
129 /// Provides an enumerator in the same order as Item[].
130 /// </para>
131 /// </devdoc>
132 public IEnumerator GetEnumerator() {
133 return new CaptureEnumerator(this);
134 }
135
136 /*
137 * Nonpublic code to return set of captures for the group
138 */
139 internal Capture GetCapture(int i) {
140 if (i == _capcount - 1 && i >= 0)
141 return _group;
142
143 if (i >= _capcount || i < 0)
144 throw new ArgumentOutOfRangeException("i");
145
146 // first time a capture is accessed, compute them all
147 if (_captures == null) {
148 _captures = new Capture[_capcount];
149 for (int j = 0; j < _capcount - 1; j++) {
150 _captures[j] = new Capture(_group._text, _group._caps[j * 2], _group._caps[j * 2 + 1]);
151 }
152 }
153
154 return _captures[i];
155 }
156 }
157
158
159 /*
160 * This non-public enumerator lists all the captures
161 * Should it be public?
162 */
163 #if !SILVERLIGHT
164 [ Serializable() ]
165 #endif
166 internal class CaptureEnumerator : IEnumerator {
167 internal CaptureCollection _rcc;
168 internal int _curindex;
169
170 /*
171 * Nonpublic constructor
172 */
173 internal CaptureEnumerator(CaptureCollection rcc) {
174 _curindex = -1;
175 _rcc = rcc;
176 }
177
178 /*
179 * As required by IEnumerator
180 */
181 public bool MoveNext() {
182 int size = _rcc.Count;
183
184 if (_curindex >= size)
185 return false;
186
187 _curindex++;
188
189 return(_curindex < size);
190 }
191
192 /*
193 * As required by IEnumerator
194 */
195 public Object Current {
196 get { return Capture;}
197 }
198
199 /*
200 * Returns the current capture
201 */
202 public Capture Capture {
203 get {
204 if (_curindex < 0 || _curindex >= _rcc.Count)
205 throw new InvalidOperationException(SR.GetString(SR.EnumNotStarted));
206
207 return _rcc[_curindex];
208 }
209 }
210
211 /*
212 * Reset to before the first item
213 */
214 public void Reset() {
215 _curindex = -1;
216 }
217 }
218
219 }