| 1 |
//------------------------------------------------------------------------------ |
| 2 |
// <copyright file="RegexGroupCollection.cs" company="Microsoft"> |
| 3 |
// Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 |
// </copyright> |
| 5 |
//------------------------------------------------------------------------------ |
| 6 |
|
| 7 |
// The GroupCollection lists the captured Capture numbers |
| 8 |
// contained in a compiled Regex. |
| 9 |
|
| 10 |
namespace System.Text.RegularExpressions { |
| 11 |
|
| 12 |
using System.Collections; |
| 13 |
using System.Collections.Generic; |
| 14 |
|
| 15 |
/// <devdoc> |
| 16 |
/// <para> |
| 17 |
/// Represents a sequence of capture substrings. The object is used |
| 18 |
/// to return the set of captures done by a single capturing group. |
| 19 |
/// </para> |
| 20 |
/// </devdoc> |
| 21 |
#if !SILVERLIGHT |
| 22 |
[ Serializable() ] |
| 23 |
#endif |
| 24 |
public class GroupCollection : ICollection { |
| 25 |
internal Match _match; |
| 26 |
#if SILVERLIGHT |
| 27 |
internal Dictionary<Int32, Int32> _captureMap; |
| 28 |
#else |
| 29 |
internal Hashtable _captureMap; |
| 30 |
#endif |
| 31 |
|
| 32 |
// cache of Group objects fed to the user |
| 33 |
internal Group[] _groups; |
| 34 |
|
| 35 |
/* |
| 36 |
* Nonpublic constructor |
| 37 |
*/ |
| 38 |
#if SILVERLIGHT |
| 39 |
internal GroupCollection(Match match, Dictionary<Int32, Int32> caps) { |
| 40 |
#else |
| 41 |
internal GroupCollection(Match match, Hashtable caps) { |
| 42 |
#endif |
| 43 |
_match = match; |
| 44 |
_captureMap = caps; |
| 45 |
} |
| 46 |
|
| 47 |
/* |
| 48 |
* The object on which to synchronize |
| 49 |
*/ |
| 50 |
/// <devdoc> |
| 51 |
/// <para>[To be supplied.]</para> |
| 52 |
/// </devdoc> |
| 53 |
public Object SyncRoot { |
| 54 |
get { |
| 55 |
return _match; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/* |
| 60 |
* ICollection |
| 61 |
*/ |
| 62 |
/// <devdoc> |
| 63 |
/// <para>[To be supplied.]</para> |
| 64 |
/// </devdoc> |
| 65 |
public bool IsSynchronized { |
| 66 |
get { |
| 67 |
return false; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/* |
| 72 |
* ICollection |
| 73 |
*/ |
| 74 |
/// <devdoc> |
| 75 |
/// <para>[To be supplied.]</para> |
| 76 |
/// </devdoc> |
| 77 |
public bool IsReadOnly { |
| 78 |
get { |
| 79 |
return true; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/// <devdoc> |
| 84 |
/// <para> |
| 85 |
/// Returns the number of groups. |
| 86 |
/// </para> |
| 87 |
/// </devdoc> |
| 88 |
public int Count { |
| 89 |
get { |
| 90 |
return _match._matchcount.Length; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/// <devdoc> |
| 95 |
/// <para>[To be supplied.]</para> |
| 96 |
/// </devdoc> |
| 97 |
public Group this[int groupnum] |
| 98 |
{ |
| 99 |
get { |
| 100 |
return GetGroup(groupnum); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/// <devdoc> |
| 105 |
/// <para>[To be supplied.]</para> |
| 106 |
/// </devdoc> |
| 107 |
public Group this[String groupname] { |
| 108 |
get { |
| 109 |
if (_match._regex == null) |
| 110 |
return Group._emptygroup; |
| 111 |
|
| 112 |
return GetGroup(_match._regex.GroupNumberFromName(groupname)); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
internal Group GetGroup(int groupnum) { |
| 117 |
if (_captureMap != null) { |
| 118 |
Object o; |
| 119 |
|
| 120 |
o = _captureMap[groupnum]; |
| 121 |
if (o == null) |
| 122 |
return Group._emptygroup; |
| 123 |
//throw new ArgumentOutOfRangeException("groupnum"); |
| 124 |
|
| 125 |
return GetGroupImpl((int)o); |
| 126 |
} |
| 127 |
else { |
| 128 |
//if (groupnum >= _match._regex.CapSize || groupnum < 0) |
| 129 |
// throw new ArgumentOutOfRangeException("groupnum"); |
| 130 |
if (groupnum >= _match._matchcount.Length || groupnum < 0) |
| 131 |
return Group._emptygroup; |
| 132 |
|
| 133 |
return GetGroupImpl(groupnum); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
/* |
| 139 |
* Caches the group objects |
| 140 |
*/ |
| 141 |
internal Group GetGroupImpl(int groupnum) { |
| 142 |
if (groupnum == 0) |
| 143 |
return _match; |
| 144 |
|
| 145 |
// Construct all the Group objects the first time GetGroup is called |
| 146 |
|
| 147 |
if (_groups == null) { |
| 148 |
_groups = new Group[_match._matchcount.Length - 1]; |
| 149 |
for (int i = 0; i < _groups.Length; i++) { |
| 150 |
string groupname = _match._regex.GroupNameFromNumber(i + 1); |
| 151 |
_groups[i] = new Group(_match._text, _match._matches[i + 1], _match._matchcount[i + 1], groupname); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return _groups[groupnum - 1]; |
| 156 |
} |
| 157 |
|
| 158 |
/* |
| 159 |
* As required by ICollection |
| 160 |
*/ |
| 161 |
/// <devdoc> |
| 162 |
/// <para> |
| 163 |
/// Copies all the elements of the collection to the given array |
| 164 |
/// beginning at the given index. |
| 165 |
/// </para> |
| 166 |
/// </devdoc> |
| 167 |
public void CopyTo(Array array, int arrayIndex) { |
| 168 |
if (array == null) |
| 169 |
throw new ArgumentNullException("array"); |
| 170 |
|
| 171 |
for (int i = arrayIndex, j = 0; j < Count; i++, j++) { |
| 172 |
array.SetValue(this[j], i); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/* |
| 177 |
* As required by ICollection |
| 178 |
*/ |
| 179 |
/// <devdoc> |
| 180 |
/// <para> |
| 181 |
/// Provides an enumerator in the same order as Item[]. |
| 182 |
/// </para> |
| 183 |
/// </devdoc> |
| 184 |
public IEnumerator GetEnumerator() { |
| 185 |
return new GroupEnumerator(this); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
/* |
| 191 |
* This non-public enumerator lists all the captures |
| 192 |
* Should it be public? |
| 193 |
*/ |
| 194 |
internal class GroupEnumerator : IEnumerator { |
| 195 |
internal GroupCollection _rgc; |
| 196 |
internal int _curindex; |
| 197 |
|
| 198 |
/* |
| 199 |
* Nonpublic constructor |
| 200 |
*/ |
| 201 |
internal GroupEnumerator(GroupCollection rgc) { |
| 202 |
_curindex = -1; |
| 203 |
_rgc = rgc; |
| 204 |
} |
| 205 |
|
| 206 |
/* |
| 207 |
* As required by IEnumerator |
| 208 |
*/ |
| 209 |
public bool MoveNext() { |
| 210 |
int size = _rgc.Count; |
| 211 |
|
| 212 |
if (_curindex >= size) |
| 213 |
return false; |
| 214 |
|
| 215 |
_curindex++; |
| 216 |
|
| 217 |
return(_curindex < size); |
| 218 |
} |
| 219 |
|
| 220 |
/* |
| 221 |
* As required by IEnumerator |
| 222 |
*/ |
| 223 |
public Object Current { |
| 224 |
get { return Capture;} |
| 225 |
} |
| 226 |
|
| 227 |
/* |
| 228 |
* Returns the current capture |
| 229 |
*/ |
| 230 |
public Capture Capture { |
| 231 |
get { |
| 232 |
if (_curindex < 0 || _curindex >= _rgc.Count) |
| 233 |
throw new InvalidOperationException(SR.GetString(SR.EnumNotStarted)); |
| 234 |
|
| 235 |
return _rgc[_curindex]; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/* |
| 240 |
* Reset to before the first item |
| 241 |
*/ |
| 242 |
public void Reset() { |
| 243 |
_curindex = -1; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
} |