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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="RegexGroup.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. // Group represents the substring or substrings that
  16. // are captured by a single capturing group after one
  17. // regular expression match.
  18. namespace System.Text.RegularExpressions
  19. {
  20.     using System.Security.Permissions;
  21.    
  22.     /// <devdoc>
  23.     /// Group
  24.     /// represents the results from a single capturing group. A capturing group can
  25.     /// capture zero, one, or more strings in a single match because of quantifiers, so
  26.     /// Group supplies a collection of Capture objects.
  27.     /// </devdoc>
  28.     [Serializable()]
  29.     public class Group : Capture
  30.     {
  31.         // the empty group object
  32.         static internal Group _emptygroup = new Group(String.Empty, new int[0], 0);
  33.        
  34.         internal int[] _caps;
  35.         internal int _capcount;
  36.         internal CaptureCollection _capcoll;
  37.        
  38.        
  39.         internal Group(string text, int[] caps, int capcount) : base(text, capcount == 0 ? 0 : caps[(capcount - 1) * 2], capcount == 0 ? 0 : caps[(capcount * 2) - 1])
  40.         {
  41.            
  42.             _caps = caps;
  43.             _capcount = capcount;
  44.         }
  45.        
  46. /*
  47.         * True if the match was successful
  48.         */       
  49.         /// <devdoc>
  50.         /// <para>Indicates whether the match is successful.</para>
  51.         /// </devdoc>
  52.         public bool Success {
  53.             get { return _capcount != 0; }
  54.         }
  55.        
  56. /*
  57.         * The collection of all captures for this group
  58.         */       
  59.         /// <devdoc>
  60.         /// <para>
  61.         /// Returns a collection of all the captures matched by the capturing
  62.         /// group, in innermost-leftmost-first order (or innermost-rightmost-first order if
  63.         /// compiled with the "r" option). The collection may have zero or more items.
  64.         /// </para>
  65.         /// </devdoc>
  66.         public CaptureCollection Captures {
  67.             get {
  68.                 if (_capcoll == null)
  69.                     _capcoll = new CaptureCollection(this);
  70.                
  71.                 return _capcoll;
  72.             }
  73.         }
  74.        
  75. /*
  76.         * Convert to a thread-safe object by precomputing cache contents
  77.         */       
  78.         /// <devdoc>
  79.         /// <para>Returns
  80.         /// a Group object equivalent to the one supplied that is safe to share between
  81.         /// multiple threads.</para>
  82.         /// </devdoc>
  83.         [HostProtection(Synchronization = true)]
  84.         public static Group Synchronized(Group inner)
  85.         {
  86.             if (inner == null)
  87.                 throw new ArgumentNullException("inner");
  88.            
  89.             // force Captures to be computed.
  90.            
  91.             CaptureCollection capcoll;
  92.             Capture dummy;
  93.            
  94.             capcoll = inner.Captures;
  95.            
  96.             if (inner._capcount > 0)
  97.                 dummy = capcoll[0];
  98.            
  99.             return inner;
  100.         }
  101.     }
  102.    
  103.    
  104. }

Developer Fusion