2 // System.Text.RegularExpressions.MatchCollection
5 // Dan Lewis (dlewis@gmx.co.uk)
6 // Dick Porter (dick@ximian.com)
9 // (C) 2004 Novell, Inc.
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
36 namespace System.Text.RegularExpressions
39 public class MatchCollection: ICollection, IEnumerable {
40 private Match current;
41 private ArrayList list;
43 /* No public constructor */
44 internal MatchCollection (Match start)
47 list = new ArrayList ();
50 public virtual int Count {
52 TryToGet (Int32.MaxValue);
57 public bool IsReadOnly {
61 public virtual bool IsSynchronized {
65 private bool TryToGet (int i)
67 while (i >= list.Count && current.Success) {
69 current = current.NextMatch ();
71 return i < list.Count;
74 public Match this [int i] {
76 if (i < 0 || !TryToGet (i))
77 throw new ArgumentOutOfRangeException ("index out of range", "i");
78 return (Match) list [i];
82 public virtual object SyncRoot {
86 public virtual void CopyTo (Array array, int index)
88 TryToGet (Int32.MaxValue);
89 list.CopyTo (array, index);
92 public virtual IEnumerator GetEnumerator ()
94 // If !current.Success, the list is fully populated. So, just use it.
95 return current.Success ? new Enumerator (this) : list.GetEnumerator ();
98 class Enumerator : IEnumerator {
100 MatchCollection coll;
102 internal Enumerator (MatchCollection coll)
108 void IEnumerator.Reset ()
113 object IEnumerator.Current {
115 if (index < 0 || index >= coll.list.Count)
116 throw new InvalidOperationException ();
117 return coll.list [index];
121 bool IEnumerator.MoveNext ()
123 if (coll.TryToGet (++index))
125 index = coll.list.Count;