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