* parser.cs: Allow creating a regular expression using {,n} as the
[mono.git] / mcs / class / System / System.Text.RegularExpressions / MatchCollection.cs
1 //
2 // System.Text.RegularExpressions.MatchCollection
3 //
4 // Authors:
5 //      Dan Lewis (dlewis@gmx.co.uk)
6 //      Dick Porter (dick@ximian.com)
7 //
8 // (C) 2002 Dan Lewis
9 // (C) 2004 Novell, Inc.
10 //
11
12 using System;
13 using System.Collections;
14
15 namespace System.Text.RegularExpressions 
16 {
17         [Serializable]
18         public class MatchCollection: ICollection, IEnumerable
19         {
20                 private ArrayList list;
21
22                 /* No public constructor */
23                 internal MatchCollection () {
24                         list = new ArrayList ();
25                 }
26
27                 public virtual int Count {
28                         get {
29                                 return(list.Count);
30                         }
31                 }
32
33                 public bool IsReadOnly {
34                         get {
35                                 return(true);
36                         }
37                 }
38
39                 public virtual bool IsSynchronized {
40                         get {
41                                 return(false);
42                         }
43                 }
44
45                 public Match this[int i] {
46                         get {
47                                 if (i < 0 ||
48                                     i > Count) {
49                                         throw new ArgumentOutOfRangeException ("Index is out of range");
50                                 }
51                                 
52                                 return((Match)list[i]);
53                         }
54                 }
55
56                 public virtual object SyncRoot {
57                         get {
58                                 return(list);
59                         }
60                 }
61
62                 public virtual void CopyTo (Array array, int index) {
63                         foreach (object o in list) {
64                                 if (index > array.Length) {
65                                         break;
66                                 }
67
68                                 array.SetValue (o, index++);
69                         }
70                 }
71
72                 public virtual IEnumerator GetEnumerator () {
73                         return(new Enumerator (list));
74                 }
75
76                 internal void Add (object o) {
77                         list.Add (o);
78                 }
79
80                 internal void Reverse () {
81                         list.Reverse ();
82                 }
83
84                 private class Enumerator: IEnumerator {
85                         private IList list;
86                         private int ptr;
87
88                         public Enumerator (IList list) {
89                                 this.list = list;
90                                 Reset ();
91                         }
92
93                         public object Current {
94                                 get {
95                                         if (ptr >= list.Count) {
96                                                 throw new InvalidOperationException ();
97                                         }
98
99                                         return(list[ptr]);
100                                 }
101                         }
102
103                         public bool MoveNext () {
104                                 if (ptr > list.Count) {
105                                         throw new InvalidOperationException ();
106                                 }
107
108                                 return(++ptr < list.Count);
109                         }
110
111                         public void Reset () {
112                                 ptr = -1;
113                         }
114                 }
115         }
116 }
117
118                 
119