Add new tests from Juraj
[mono.git] / mcs / class / System / System.Text.RegularExpressions / collections.cs
1 //\r
2 // assembly:    System\r
3 // namespace:   System.Text.RegularExpressions\r
4 // file:        collections.cs\r
5 //\r
6 // author:      Dan Lewis (dlewis@gmx.co.uk)\r
7 //              (c) 2002\r
8 \r
9 using System;\r
10 using System.Collections;\r
11 \r
12 namespace System.Text.RegularExpressions {\r
13         public abstract class RegexCollectionBase : ICollection, IEnumerable {\r
14                 public int Count {\r
15                         get { return list.Count; }\r
16                 }\r
17 \r
18                 public bool IsReadOnly {\r
19                         get { return true; }    // FIXME\r
20                 }\r
21 \r
22                 public bool IsSynchronized {\r
23                         get { return false; }   // FIXME\r
24                 }\r
25 \r
26                 public object SyncRoot {\r
27                         get { return list; }    // FIXME\r
28                 }\r
29 \r
30                 public void CopyTo (Array array, int index) {\r
31                         foreach (Object o in list) {\r
32                                 if (index > array.Length)\r
33                                         break;\r
34                                 \r
35                                 array.SetValue (o, index ++);\r
36                         }\r
37                 }\r
38 \r
39                 public IEnumerator GetEnumerator () {\r
40                         return new Enumerator (list);\r
41                 }\r
42 \r
43                 // internal methods\r
44 \r
45                 internal RegexCollectionBase () {\r
46                         list = new ArrayList ();\r
47                 }\r
48 \r
49                 internal void Add (Object o) {\r
50                         list.Add (o);\r
51                 }\r
52 \r
53                 internal void Reverse () {\r
54                         list.Reverse ();\r
55                 }\r
56 \r
57                 // IEnumerator implementation\r
58 \r
59                 private class Enumerator : IEnumerator {\r
60                         public Enumerator (IList list) {\r
61                                 this.list = list;\r
62                                 Reset ();\r
63                         }\r
64 \r
65                         public object Current {\r
66                                 get {\r
67                                         if (ptr >= list.Count)\r
68                                                 throw new InvalidOperationException ();\r
69 \r
70                                         return list[ptr];\r
71                                 }\r
72                         }\r
73 \r
74                         public bool MoveNext () {\r
75                                 if (ptr > list.Count)\r
76                                         throw new InvalidOperationException ();\r
77                                 \r
78                                 return ++ ptr < list.Count;\r
79                         }\r
80 \r
81                         public void Reset () {\r
82                                 ptr = -1;\r
83                         }\r
84 \r
85                         private IList list;\r
86                         private int ptr;\r
87                 }\r
88 \r
89                 // protected fields\r
90 \r
91                 protected ArrayList list;\r
92         }\r
93 \r
94         [Serializable]\r
95         public class CaptureCollection : RegexCollectionBase, ICollection, IEnumerable {\r
96                 public Capture this[int i] {\r
97                         get { return (Capture)list[i]; }\r
98                 }\r
99 \r
100                 internal CaptureCollection () {\r
101                 }\r
102         }\r
103 \r
104         [Serializable]\r
105         public class GroupCollection : RegexCollectionBase, ICollection, IEnumerable {\r
106                 public Group this[int i] {\r
107                         get { return (Group)list[i]; }\r
108                 }\r
109                 \r
110                 public Group this[string groupName] {\r
111                         get {\r
112                                 foreach (object o in list) {\r
113                                         if (!(o is Match))\r
114                                                 continue;\r
115 \r
116                                         int index = ((Match) o).Regex.GroupNumberFromName (groupName);\r
117                                         if (index != -1)\r
118                                                 return this [index];\r
119                                 }\r
120 \r
121                                 return null;\r
122                         }\r
123                 }\r
124                 \r
125                 internal GroupCollection () {\r
126                 }\r
127         }\r
128 \r
129         [Serializable]\r
130         public class MatchCollection : RegexCollectionBase, ICollection, IEnumerable {\r
131                 public virtual Match this[int i] {\r
132                         get { return (Match)list[i]; }\r
133                 }\r
134 \r
135                 internal MatchCollection () {\r
136                 }\r
137         }\r
138 }\r