The Labs \ Source Viewer \ SSCLI \ System.Text.RegularExpressions \ CaptureCollection

  1. //------------------------------------------------------------------------------
  2. // <copyright file="RegexCaptureCollection.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. // The CaptureCollection lists the captured Capture numbers
  16. // contained in a compiled Regex.
  17. namespace System.Text.RegularExpressions
  18. {
  19.    
  20.     using System.Collections;
  21.    
  22. /*
  23.     * This collection returns the Captures for a group
  24.     * in the order in which they were matched (left to right
  25.     * or right to left). It is created by Group.Captures
  26.     */   
  27.     /// <devdoc>
  28.     /// <para>
  29.     /// Represents a sequence of capture substrings. The object is used
  30.     /// to return the set of captures done by a single capturing group.
  31.     /// </para>
  32.     /// </devdoc>
  33.     [Serializable()]
  34.     public class CaptureCollection : ICollection
  35.     {
  36.         internal Group _group;
  37.         internal int _capcount;
  38.         internal Capture[] _captures;
  39.        
  40. /*
  41.         * Nonpublic constructor
  42.         */       
  43.         internal CaptureCollection(Group group)
  44.         {
  45.             _group = group;
  46.             _capcount = _group._capcount;
  47.         }
  48.        
  49. /*
  50.         * The object on which to synchronize
  51.         */       
  52.         /// <devdoc>
  53.         /// <para>[To be supplied.]</para>
  54.         /// </devdoc>
  55.         public object SyncRoot {
  56.             get { return _group; }
  57.         }
  58.        
  59. /*
  60.         * ICollection
  61.         */       
  62.         /// <devdoc>
  63.         /// <para>[To be supplied.]</para>
  64.         /// </devdoc>
  65.         public bool IsSynchronized {
  66.             get { return false; }
  67.         }
  68.        
  69. /*
  70.         * ICollection
  71.         */       
  72.         /// <devdoc>
  73.         /// <para>[To be supplied.]</para>
  74.         /// </devdoc>
  75.         public bool IsReadOnly {
  76.             get { return true; }
  77.         }
  78.        
  79. /*
  80.         * The number of captures for the group
  81.         */       
  82.         /// <devdoc>
  83.         /// <para>
  84.         /// Returns the number of captures.
  85.         /// </para>
  86.         /// </devdoc>
  87.         public int Count {
  88.             get { return _capcount; }
  89.         }
  90.        
  91. /*
  92.         * The ith capture in the group
  93.         */       
  94.         /// <devdoc>
  95.         /// <para>
  96.         /// Provides a means of accessing a specific capture in the collection.
  97.         /// </para>
  98.         /// </devdoc>
  99.         public Capture this[int i]
  100.         {
  101.             get { return GetCapture(i); }
  102.         }
  103.        
  104. /*
  105.         * As required by ICollection
  106.         */       
  107.         /// <devdoc>
  108.         /// <para>
  109.         /// Copies all the elements of the collection to the given array
  110.         /// beginning at the given index.
  111.         /// </para>
  112.         /// </devdoc>
  113.         public void CopyTo(Array array, int arrayIndex)
  114.         {
  115.             if (array == null)
  116.                 throw new ArgumentNullException("array");
  117.            
  118.             for (int i = arrayIndexint j = 0; j < Count; i++,j++) {
  119.                 array.SetValue(this[j], i);
  120.             }
  121.         }
  122.        
  123. /*
  124.         * As required by ICollection
  125.         */       
  126.         /// <devdoc>
  127.         /// <para>
  128.         /// Provides an enumerator in the same order as Item[].
  129.         /// </para>
  130.         /// </devdoc>
  131.         public IEnumerator GetEnumerator()
  132.         {
  133.             return new CaptureEnumerator(this);
  134.         }
  135.        
  136. /*
  137.         * Nonpublic code to return set of captures for the group
  138.         */       
  139.         internal Capture GetCapture(int i)
  140.         {
  141.             if (i == _capcount - 1 && i >= 0)
  142.                 return _group;
  143.            
  144.             if (i >= _capcount || i < 0)
  145.                 throw new ArgumentOutOfRangeException("i");
  146.            
  147.             // first time a capture is accessed, compute them all
  148.             if (_captures == null) {
  149.                 _captures = new Capture[_capcount];
  150.                 for (int j = 0; j < _capcount - 1; j++) {
  151.                     _captures[j] = new Capture(_group._text, _group._caps[j * 2], _group._caps[j * 2 + 1]);
  152.                 }
  153.             }
  154.            
  155.             return _captures[i];
  156.         }
  157.     }
  158.    
  159.    
  160. /*
  161.     * This non-public enumerator lists all the captures
  162.     * Should it be public?
  163.     */   
  164.     [Serializable()]
  165.     internal class CaptureEnumerator : IEnumerator
  166.     {
  167.         internal CaptureCollection _rcc;
  168.         internal int _curindex;
  169.        
  170. /*
  171.         * Nonpublic constructor
  172.         */       
  173.         internal CaptureEnumerator(CaptureCollection rcc)
  174.         {
  175.             _curindex = -1;
  176.             _rcc = rcc;
  177.         }
  178.        
  179. /*
  180.         * As required by IEnumerator
  181.         */       
  182.         public bool MoveNext()
  183.         {
  184.             int size = _rcc.Count;
  185.            
  186.             if (_curindex >= size)
  187.                 return false;
  188.            
  189.             _curindex++;
  190.            
  191.             return (_curindex < size);
  192.         }
  193.        
  194. /*
  195.         * As required by IEnumerator
  196.         */       
  197.         public object Current {
  198.             get { return Capture; }
  199.         }
  200.        
  201. /*
  202.         * Returns the current capture
  203.         */       
  204.         public Capture Capture {
  205.             get {
  206.                 if (_curindex < 0 || _curindex >= _rcc.Count)
  207.                     throw new InvalidOperationException(SR.GetString(SR.EnumNotStarted));
  208.                
  209.                 return _rcc[_curindex];
  210.             }
  211.         }
  212.        
  213. /*
  214.         * Reset to before the first item
  215.         */       
  216.         public void Reset()
  217.         {
  218.             _curindex = -1;
  219.         }
  220.     }
  221.    
  222. }

Developer Fusion