GroupCollection.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // System.Text.RegularExpressions.GroupCollection
  3. //
  4. // Authors:
  5. // Dan Lewis ([email protected])
  6. // Dick Porter ([email protected])
  7. //
  8. // (C) 2002 Dan Lewis
  9. // (C) 2004 Novell, Inc.
  10. //
  11. using System;
  12. using System.Collections;
  13. namespace System.Text.RegularExpressions
  14. {
  15. [Serializable]
  16. public class GroupCollection: ICollection, IEnumerable
  17. {
  18. private ArrayList list;
  19. /* No public constructor */
  20. internal GroupCollection () {
  21. list = new ArrayList ();
  22. }
  23. public virtual int Count {
  24. get {
  25. return(list.Count);
  26. }
  27. }
  28. public bool IsReadOnly {
  29. get {
  30. return(true);
  31. }
  32. }
  33. public virtual bool IsSynchronized {
  34. get {
  35. return(false);
  36. }
  37. }
  38. public Group this[int i] {
  39. get {
  40. if (i < list.Count &&
  41. i >= 0) {
  42. return((Group)list[i]);
  43. } else {
  44. return(new Group ());
  45. }
  46. }
  47. }
  48. public Group this[string groupName] {
  49. get {
  50. foreach (object o in list) {
  51. if (!(o is Match)) {
  52. continue;
  53. }
  54. int index = ((Match)o).Regex.GroupNumberFromName (groupName);
  55. if (index != -1) {
  56. return(this[index]);
  57. }
  58. }
  59. return(new Group ());
  60. }
  61. }
  62. public virtual object SyncRoot {
  63. get {
  64. return(list);
  65. }
  66. }
  67. public virtual void CopyTo (Array array, int index) {
  68. foreach (object o in list) {
  69. if (index > array.Length) {
  70. break;
  71. }
  72. array.SetValue (o, index++);
  73. }
  74. }
  75. public virtual IEnumerator GetEnumerator () {
  76. return(new Enumerator (list));
  77. }
  78. internal void Add (object o) {
  79. list.Add (o);
  80. }
  81. internal void Reverse () {
  82. list.Reverse ();
  83. }
  84. private class Enumerator: IEnumerator {
  85. private IList list;
  86. private int ptr;
  87. public Enumerator (IList list) {
  88. this.list = list;
  89. Reset ();
  90. }
  91. public object Current {
  92. get {
  93. if (ptr >= list.Count) {
  94. throw new InvalidOperationException ();
  95. }
  96. return(list[ptr]);
  97. }
  98. }
  99. public bool MoveNext () {
  100. if (ptr > list.Count) {
  101. throw new InvalidOperationException ();
  102. }
  103. return(++ptr < list.Count);
  104. }
  105. public void Reset () {
  106. ptr = -1;
  107. }
  108. }
  109. }
  110. }