collections.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // assembly: System
  3. // namespace: System.Text.RegularExpressions
  4. // file: collections.cs
  5. //
  6. // author: Dan Lewis ([email protected])
  7. // (c) 2002
  8. using System;
  9. using System.Collections;
  10. namespace System.Text.RegularExpressions {
  11. public abstract class RegexCollectionBase : ICollection, IEnumerable {
  12. public int Count {
  13. get { return list.Count; }
  14. }
  15. public bool IsReadOnly {
  16. get { return true; } // FIXME
  17. }
  18. public bool IsSynchronized {
  19. get { return false; } // FIXME
  20. }
  21. public object SyncRoot {
  22. get { return list; } // FIXME
  23. }
  24. public void CopyTo (Array array, int index) {
  25. foreach (Object o in list) {
  26. if (index > array.Length)
  27. break;
  28. array.SetValue (o, index ++);
  29. }
  30. }
  31. public IEnumerator GetEnumerator () {
  32. return new Enumerator (list);
  33. }
  34. // internal methods
  35. internal RegexCollectionBase () {
  36. list = new ArrayList ();
  37. }
  38. internal void Add (Object o) {
  39. list.Add (o);
  40. }
  41. internal void Reverse () {
  42. list.Reverse ();
  43. }
  44. // IEnumerator implementation
  45. private class Enumerator : IEnumerator {
  46. public Enumerator (IList list) {
  47. this.list = list;
  48. Reset ();
  49. }
  50. public object Current {
  51. get {
  52. if (ptr >= list.Count)
  53. throw new InvalidOperationException ();
  54. return list[ptr];
  55. }
  56. }
  57. public bool MoveNext () {
  58. if (ptr > list.Count)
  59. throw new InvalidOperationException ();
  60. return ++ ptr < list.Count;
  61. }
  62. public void Reset () {
  63. ptr = -1;
  64. }
  65. private IList list;
  66. private int ptr;
  67. }
  68. // protected fields
  69. protected ArrayList list;
  70. }
  71. [Serializable]
  72. public class CaptureCollection : RegexCollectionBase, ICollection, IEnumerable {
  73. public Capture this[int i] {
  74. get { return (Capture)list[i]; }
  75. }
  76. internal CaptureCollection () {
  77. }
  78. }
  79. [Serializable]
  80. public class GroupCollection : RegexCollectionBase, ICollection, IEnumerable {
  81. public Group this[int i] {
  82. get { return (Group)list[i]; }
  83. }
  84. internal GroupCollection () {
  85. }
  86. }
  87. [Serializable]
  88. public class MatchCollection : RegexCollectionBase, ICollection, IEnumerable {
  89. public virtual Match this[int i] {
  90. get { return (Match)list[i]; }
  91. }
  92. internal MatchCollection () {
  93. }
  94. }
  95. }