9 |
// (C) 2004 Novell, Inc. |
// (C) 2004 Novell, Inc. |
10 |
// |
// |
11 |
|
|
12 |
|
// |
13 |
|
// Permission is hereby granted, free of charge, to any person obtaining |
14 |
|
// a copy of this software and associated documentation files (the |
15 |
|
// "Software"), to deal in the Software without restriction, including |
16 |
|
// without limitation the rights to use, copy, modify, merge, publish, |
17 |
|
// distribute, sublicense, and/or sell copies of the Software, and to |
18 |
|
// permit persons to whom the Software is furnished to do so, subject to |
19 |
|
// the following conditions: |
20 |
|
// |
21 |
|
// The above copyright notice and this permission notice shall be |
22 |
|
// included in all copies or substantial portions of the Software. |
23 |
|
// |
24 |
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
25 |
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
26 |
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
27 |
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
28 |
|
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
29 |
|
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
30 |
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
31 |
|
// |
32 |
|
|
33 |
using System; |
using System; |
34 |
using System.Collections; |
using System.Collections; |
35 |
|
|
36 |
namespace System.Text.RegularExpressions |
namespace System.Text.RegularExpressions |
37 |
{ |
{ |
38 |
[Serializable] |
[Serializable] |
39 |
public class MatchCollection: ICollection, IEnumerable |
public class MatchCollection: ICollection, IEnumerable { |
40 |
{ |
private Match current; |
41 |
|
|
42 |
|
// Stores all the matches before 'current'. If !current.Success, it has all the successful matches. |
43 |
private ArrayList list; |
private ArrayList list; |
44 |
|
|
45 |
/* No public constructor */ |
/* No public constructor */ |
46 |
internal MatchCollection () { |
internal MatchCollection (Match start) |
47 |
|
{ |
48 |
|
current = start; |
49 |
list = new ArrayList (); |
list = new ArrayList (); |
50 |
} |
} |
51 |
|
|
52 |
public virtual int Count { |
public virtual int Count { |
53 |
get { |
get { return FullList.Count; } |
|
return(list.Count); |
|
|
} |
|
54 |
} |
} |
55 |
|
|
56 |
public bool IsReadOnly { |
public bool IsReadOnly { |
57 |
get { |
get { return true; } |
|
return(true); |
|
|
} |
|
58 |
} |
} |
59 |
|
|
60 |
public virtual bool IsSynchronized { |
public virtual bool IsSynchronized { |
61 |
get { |
get { return false; } |
|
return(false); |
|
|
} |
|
62 |
} |
} |
63 |
|
|
64 |
public Match this[int i] { |
|
65 |
|
public Match this [int i] { |
66 |
get { |
get { |
67 |
if (i < 0 || |
if (i < 0 || !TryToGet (i)) |
68 |
i > Count) { |
throw new ArgumentOutOfRangeException ("i"); |
69 |
throw new ArgumentOutOfRangeException ("Index is out of range"); |
return i < list.Count ? (Match) list [i] : current; |
|
} |
|
|
|
|
|
return((Match)list[i]); |
|
70 |
} |
} |
71 |
} |
} |
72 |
|
|
73 |
public virtual object SyncRoot { |
public virtual object SyncRoot { |
74 |
get { |
get { return list; } |
|
return(list); |
|
|
} |
|
75 |
} |
} |
76 |
|
|
77 |
public virtual void CopyTo (Array array, int index) { |
public virtual void CopyTo (Array array, int index) |
78 |
foreach (object o in list) { |
{ |
79 |
if (index > array.Length) { |
FullList.CopyTo (array, index); |
|
break; |
|
|
} |
|
|
|
|
|
array.SetValue (o, index++); |
|
|
} |
|
80 |
} |
} |
81 |
|
|
82 |
public virtual IEnumerator GetEnumerator () { |
public virtual IEnumerator GetEnumerator () |
83 |
return(new Enumerator (list)); |
{ |
84 |
|
// If !current.Success, the list is fully populated. So, just use it. |
85 |
|
return current.Success ? new Enumerator (this) : list.GetEnumerator (); |
86 |
} |
} |
87 |
|
|
88 |
internal void Add (object o) { |
// Returns true when: i < list.Count => this [i] == list [i] |
89 |
list.Add (o); |
// i == list.Count && current.Success => this [i] == current |
90 |
|
private bool TryToGet (int i) |
91 |
|
{ |
92 |
|
while (i > list.Count && current.Success) { |
93 |
|
list.Add (current); |
94 |
|
current = current.NextMatch (); |
95 |
|
} |
96 |
|
// Here we have: !(i > list.Count && current.Success) |
97 |
|
// or in a slightly more useful form: i > list.Count => current.Success == false |
98 |
|
return i < list.Count || current.Success; |
99 |
} |
} |
100 |
|
|
101 |
internal void Reverse () { |
private ICollection FullList { |
102 |
list.Reverse (); |
get { |
103 |
|
if (TryToGet (Int32.MaxValue)) { |
104 |
|
// list.Count == Int32.MaxValue && current.Success |
105 |
|
// i.e., we have more than Int32.MaxValue matches. |
106 |
|
// We can't represent that number with Int32. |
107 |
|
throw new SystemException ("too many matches"); |
108 |
|
} |
109 |
|
return list; |
110 |
|
} |
111 |
} |
} |
112 |
|
|
113 |
private class Enumerator: IEnumerator { |
class Enumerator : IEnumerator { |
114 |
private IList list; |
int index; |
115 |
private int ptr; |
MatchCollection coll; |
116 |
|
|
117 |
public Enumerator (IList list) { |
internal Enumerator (MatchCollection coll) |
118 |
this.list = list; |
{ |
119 |
Reset (); |
this.coll = coll; |
120 |
|
index = -1; |
121 |
} |
} |
122 |
|
|
123 |
public object Current { |
void IEnumerator.Reset () |
124 |
get { |
{ |
125 |
if (ptr >= list.Count) { |
index = -1; |
|
throw new InvalidOperationException (); |
|
|
} |
|
|
|
|
|
return(list[ptr]); |
|
|
} |
|
126 |
} |
} |
127 |
|
|
128 |
public bool MoveNext () { |
object IEnumerator.Current { |
129 |
if (ptr > list.Count) { |
get { |
130 |
throw new InvalidOperationException (); |
if (index < 0) |
131 |
|
throw new InvalidOperationException ("'Current' called before 'MoveNext()'"); |
132 |
|
if (index > coll.list.Count) |
133 |
|
throw new SystemException ("MatchCollection in invalid state"); |
134 |
|
if (index == coll.list.Count && !coll.current.Success) |
135 |
|
throw new InvalidOperationException ("'Current' called after 'MoveNext()' returned false"); |
136 |
|
return index < coll.list.Count ? coll.list [index] : coll.current; |
137 |
} |
} |
|
|
|
|
return(++ptr < list.Count); |
|
138 |
} |
} |
139 |
|
|
140 |
public void Reset () { |
bool IEnumerator.MoveNext () |
141 |
ptr = -1; |
{ |
142 |
|
if (index > coll.list.Count) |
143 |
|
throw new SystemException ("MatchCollection in invalid state"); |
144 |
|
if (index == coll.list.Count && !coll.current.Success) |
145 |
|
return false; |
146 |
|
return coll.TryToGet (++index); |
147 |
} |
} |
148 |
} |
} |
149 |
} |
} |
150 |
} |
} |
|
|
|
|
|
|
|
|
|