Bug 10670 fix.
[mono.git] / mcs / class / System / System.Text.RegularExpressions / GroupCollection.cs
index d6ac6d29f04a6e7a4679b6f0cde3ce4f18f86bb5..5ff2383c9b9e372f8ea80ebf59bda2f5cdf25608 100644 (file)
@@ -38,118 +38,66 @@ namespace System.Text.RegularExpressions
        [Serializable]
        public class GroupCollection: ICollection, IEnumerable
        {
-               private ArrayList list;
+               private Group [] list;
 
                /* No public constructor */
-               internal GroupCollection () {
-                       list = new ArrayList ();
+               internal GroupCollection (int n)
+               {
+                       list = new Group [n];
                }
 
-               public virtual int Count {
-                       get {
-                               return(list.Count);
-                       }
+               public int Count {
+                       get { return list.Length; }
                }
 
                public bool IsReadOnly {
-                       get {
-                               return(true);
-                       }
+                       get { return true; }
                }
 
-               public virtual bool IsSynchronized {
-                       get {
-                               return(false);
-                       }
+               public bool IsSynchronized {
+                       get { return false; }
                }
 
-               public Group this[int i] {
+               public Group this [int i] {
                        get {
-                               if (i < list.Count &&
-                                   i >= 0) {
-                                       return((Group)list[i]);
-                               } else {
-                                       return(new Group ());
-                               }
+                               if (i < list.Length && i >= 0)
+                                       return list [i];
+                               else
+                                       return Group.Fail;
                        }
                }
 
-               public Group this[string groupName] {
-                       get {
-                               foreach (object o in list) {
-                                       if (!(o is Match)) {
-                                               continue;
-                                       }
-
-                                       int index = ((Match)o).Regex.GroupNumberFromName (groupName);
-
-                                       if (index != -1) {
-                                               return(this[index]);
-                                       }
-                               }
-
-                               return(new Group ());
-                       }
+               internal void SetValue (Group g, int i)
+               {
+                       list [i] = g;
                }
 
-               public virtual object SyncRoot {
+               public Group this [string groupName] {
                        get {
-                               return(list);
-                       }
-               }
-
-               public virtual void CopyTo (Array array, int index) {
-                       foreach (object o in list) {
-                               if (index > array.Length) {
-                                       break;
+                               // The 0th group is the match.
+                               Match m = (Match) list [0];
+                               if (m != Match.Empty) {
+                                       int index = m.Regex.GroupNumberFromName (groupName);
+                                       if (index != -1)
+                                               return this [index];
                                }
 
-                               array.SetValue (o, index++);
+                               return Group.Fail;
                        }
                }
 
-               public virtual IEnumerator GetEnumerator () {
-                       return(new Enumerator (list));
-               }
-
-               internal void Add (object o) {
-                       list.Add (o);
+               public object SyncRoot {
+                       get { return list; }
                }
 
-               internal void Reverse () {
-                       list.Reverse ();
+               public void CopyTo (Array array, int index)
+               {
+                       list.CopyTo (array, index);
                }
 
-               private class Enumerator: IEnumerator {
-                       private IList list;
-                       private int ptr;
-
-                       public Enumerator (IList list) {
-                               this.list = list;
-                               Reset ();
-                       }
-
-                       public object Current {
-                               get {
-                                       if (ptr >= list.Count) {
-                                               throw new InvalidOperationException ();
-                                       }
-
-                                       return(list[ptr]);
-                               }
-                       }
-
-                       public bool MoveNext () {
-                               if (ptr > list.Count) {
-                                       throw new InvalidOperationException ();
-                               }
-
-                               return(++ptr < list.Count);
-                       }
-
-                       public void Reset () {
-                               ptr = -1;
-                       }
+               public IEnumerator GetEnumerator ()
+               {
+                       return list.GetEnumerator ();
                }
        }
 }