| 1 |
//------------------------------------------------------------------------------ |
| 2 |
// <copyright file="RegexMatchCollection.cs" company="Microsoft"> |
| 3 |
// Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 |
// </copyright> |
| 5 |
//------------------------------------------------------------------------------ |
| 6 |
|
| 7 |
// The MatchCollection lists the successful matches that |
| 8 |
// result when searching a string for a regular expression. |
| 9 |
|
| 10 |
namespace System.Text.RegularExpressions { |
| 11 |
|
| 12 |
using System.Collections; |
| 13 |
using System.Collections.Generic; |
| 14 |
|
| 15 |
|
| 16 |
/* |
| 17 |
* This collection returns a sequence of successful match results, either |
| 18 |
* from GetMatchCollection() or GetExecuteCollection(). It stops when the |
| 19 |
* first failure is encountered (it does not return the failed match). |
| 20 |
*/ |
| 21 |
/// <devdoc> |
| 22 |
/// <para> |
| 23 |
/// Represents the set of names appearing as capturing group |
| 24 |
/// names in a regular expression. |
| 25 |
/// </para> |
| 26 |
/// </devdoc> |
| 27 |
#if !SILVERLIGHT |
| 28 |
[ Serializable() ] |
| 29 |
#endif |
| 30 |
public class MatchCollection : ICollection { |
| 31 |
internal Regex _regex; |
| 32 |
#if SILVERLIGHT |
| 33 |
internal List<Match> _matches; |
| 34 |
#else |
| 35 |
internal ArrayList _matches; |
| 36 |
#endif |
| 37 |
internal bool _done; |
| 38 |
internal String _input; |
| 39 |
internal int _beginning; |
| 40 |
internal int _length; |
| 41 |
internal int _startat; |
| 42 |
internal int _prevlen; |
| 43 |
|
| 44 |
private static int infinite = 0x7FFFFFFF; |
| 45 |
|
| 46 |
/* |
| 47 |
* Nonpublic constructor |
| 48 |
*/ |
| 49 |
internal MatchCollection(Regex regex, String input, int beginning, int length, int startat) { |
| 50 |
if (startat < 0 || startat > input.Length) |
| 51 |
throw new ArgumentOutOfRangeException("startat", SR.GetString(SR.BeginIndexNotNegative)); |
| 52 |
|
| 53 |
_regex = regex; |
| 54 |
_input = input; |
| 55 |
_beginning = beginning; |
| 56 |
_length = length; |
| 57 |
_startat = startat; |
| 58 |
_prevlen = -1; |
| 59 |
#if SILVERLIGHT |
| 60 |
_matches = new List<Match>(); |
| 61 |
#else |
| 62 |
_matches = new ArrayList(); |
| 63 |
#endif |
| 64 |
_done = false; |
| 65 |
} |
| 66 |
|
| 67 |
internal Match GetMatch(int i) { |
| 68 |
if (i < 0) |
| 69 |
return null; |
| 70 |
|
| 71 |
if (_matches.Count > i) |
| 72 |
return (Match)_matches[i]; |
| 73 |
|
| 74 |
if (_done) |
| 75 |
return null; |
| 76 |
|
| 77 |
Match match; |
| 78 |
|
| 79 |
do { |
| 80 |
match = _regex.Run(false, _prevlen, _input, _beginning, _length, _startat); |
| 81 |
|
| 82 |
if (!match.Success) { |
| 83 |
_done = true; |
| 84 |
return null; |
| 85 |
} |
| 86 |
|
| 87 |
_matches.Add(match); |
| 88 |
|
| 89 |
_prevlen = match._length; |
| 90 |
_startat = match._textpos; |
| 91 |
|
| 92 |
} while (_matches.Count <= i); |
| 93 |
|
| 94 |
return match; |
| 95 |
} |
| 96 |
|
| 97 |
/// <devdoc> |
| 98 |
/// <para> |
| 99 |
/// Returns the number of captures. |
| 100 |
/// </para> |
| 101 |
/// </devdoc> |
| 102 |
public int Count { |
| 103 |
get { |
| 104 |
if (_done) |
| 105 |
return _matches.Count; |
| 106 |
|
| 107 |
GetMatch(infinite); |
| 108 |
|
| 109 |
return _matches.Count; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/// <devdoc> |
| 114 |
/// <para>[To be supplied.]</para> |
| 115 |
/// </devdoc> |
| 116 |
public Object SyncRoot { |
| 117 |
get { |
| 118 |
return this; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/// <devdoc> |
| 123 |
/// <para>[To be supplied.]</para> |
| 124 |
/// </devdoc> |
| 125 |
public bool IsSynchronized { |
| 126 |
get { |
| 127 |
return false; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/// <devdoc> |
| 132 |
/// <para>[To be supplied.]</para> |
| 133 |
/// </devdoc> |
| 134 |
public bool IsReadOnly { |
| 135 |
get { |
| 136 |
return true; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
/// <devdoc> |
| 142 |
/// <para> |
| 143 |
/// Returns the ith Match in the collection. |
| 144 |
/// </para> |
| 145 |
/// </devdoc> |
| 146 |
public virtual Match this[int i] |
| 147 |
{ |
| 148 |
get { |
| 149 |
Match match; |
| 150 |
|
| 151 |
match = GetMatch(i); |
| 152 |
|
| 153 |
if (match == null) |
| 154 |
throw new ArgumentOutOfRangeException("i"); |
| 155 |
|
| 156 |
return match; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/// <devdoc> |
| 161 |
/// <para> |
| 162 |
/// Copies all the elements of the collection to the given array |
| 163 |
/// starting at the given index. |
| 164 |
/// </para> |
| 165 |
/// </devdoc> |
| 166 |
public void CopyTo(Array array, int arrayIndex) { |
| 167 |
if ((array != null) && (array.Rank != 1)) |
| 168 |
{ |
| 169 |
throw new ArgumentException(SR.GetString(SR.Arg_RankMultiDimNotSupported)); |
| 170 |
} |
| 171 |
|
| 172 |
// property access to force computation of whole array |
| 173 |
int count = Count; |
| 174 |
try |
| 175 |
{ |
| 176 |
#if SILVERLIGHT |
| 177 |
// Array.Copy will check for null. |
| 178 |
Array.Copy(_matches.ToArray(), 0, array, arrayIndex, count); |
| 179 |
#else |
| 180 |
_matches.CopyTo(array, arrayIndex); |
| 181 |
#endif |
| 182 |
} |
| 183 |
catch (ArrayTypeMismatchException ex) |
| 184 |
{ |
| 185 |
#if FEATURE_LEGACYNETCF |
| 186 |
if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8) |
| 187 |
throw; |
| 188 |
#endif |
| 189 |
throw new ArgumentException(SR.GetString(SR.Arg_InvalidArrayType), ex); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/// <devdoc> |
| 194 |
/// <para> |
| 195 |
/// Provides an enumerator in the same order as Item[i]. |
| 196 |
/// </para> |
| 197 |
/// </devdoc> |
| 198 |
public IEnumerator GetEnumerator() { |
| 199 |
return new MatchEnumerator(this); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
/* |
| 204 |
* This non-public enumerator lists all the group matches. |
| 205 |
* Should it be public? |
| 206 |
*/ |
| 207 |
#if !SILVERLIGHT |
| 208 |
[ Serializable() ] |
| 209 |
#endif |
| 210 |
internal class MatchEnumerator : IEnumerator { |
| 211 |
internal MatchCollection _matchcoll; |
| 212 |
internal Match _match = null; |
| 213 |
internal int _curindex; |
| 214 |
internal bool _done; |
| 215 |
|
| 216 |
/* |
| 217 |
* Nonpublic constructor |
| 218 |
*/ |
| 219 |
internal MatchEnumerator(MatchCollection matchcoll) { |
| 220 |
_matchcoll = matchcoll; |
| 221 |
} |
| 222 |
|
| 223 |
/* |
| 224 |
* Advance to the next match |
| 225 |
*/ |
| 226 |
public bool MoveNext() { |
| 227 |
if (_done) |
| 228 |
return false; |
| 229 |
|
| 230 |
_match = _matchcoll.GetMatch(_curindex); |
| 231 |
_curindex++; |
| 232 |
|
| 233 |
if (_match == null) { |
| 234 |
_done = true; |
| 235 |
return false; |
| 236 |
} |
| 237 |
|
| 238 |
return true; |
| 239 |
} |
| 240 |
|
| 241 |
/* |
| 242 |
* The current match |
| 243 |
*/ |
| 244 |
public Object Current { |
| 245 |
get { |
| 246 |
if (_match == null) |
| 247 |
throw new InvalidOperationException(SR.GetString(SR.EnumNotStarted)); |
| 248 |
return _match; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
/* |
| 253 |
* Position before the first item |
| 254 |
*/ |
| 255 |
public void Reset() { |
| 256 |
_curindex = 0; |
| 257 |
_done = false; |
| 258 |
_match = null; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
} |