match.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // assembly: System
  3. // namespace: System.Text.RegularExpressions
  4. // file: match.cs
  5. //
  6. // author: Dan Lewis ([email protected])
  7. // (c) 2002
  8. using System;
  9. namespace System.Text.RegularExpressions {
  10. [Serializable]
  11. public class Capture {
  12. public int Index {
  13. get { return index; }
  14. }
  15. public int Length {
  16. get { return length; }
  17. }
  18. public string Value {
  19. get { return text.Substring (index, length); }
  20. }
  21. public override string ToString () {
  22. return Value;
  23. }
  24. // internal members
  25. internal Capture (string text) : this (text, 0, 0) { }
  26. internal Capture (string text, int index, int length) {
  27. this.text = text;
  28. this.index = index;
  29. this.length = length;
  30. }
  31. internal string Text {
  32. get { return text; }
  33. }
  34. // private
  35. internal int index, length;
  36. internal string text;
  37. }
  38. [Serializable]
  39. public class Group : Capture {
  40. public static Group Synchronized (Group inner) {
  41. return inner; // is this enough?
  42. }
  43. public CaptureCollection Captures {
  44. get { return captures; }
  45. }
  46. public bool Success {
  47. get { return success; }
  48. }
  49. // internal
  50. internal Group (string text, int[] caps) : base (text) {
  51. this.captures = new CaptureCollection ();
  52. if (caps == null || caps.Length == 0) {
  53. this.success = false;
  54. return;
  55. }
  56. this.success = true;
  57. this.index = caps[0];
  58. this.length = caps[1];
  59. captures.Add (this);
  60. for (int i = 2; i < caps.Length; i += 2)
  61. captures.Add (new Capture (text, caps[i], caps[i + 1]));
  62. captures.Reverse ();
  63. }
  64. private bool success;
  65. private CaptureCollection captures;
  66. }
  67. [Serializable]
  68. public class Match : Group {
  69. public static Match Empty {
  70. get { return empty; }
  71. }
  72. public static Match Synchronized (Match inner) {
  73. return inner; // FIXME need to sync on machine access
  74. }
  75. public virtual GroupCollection Groups {
  76. get { return groups; }
  77. }
  78. public Match NextMatch () {
  79. if (this == Empty)
  80. return Empty;
  81. int scan_ptr = regex.RightToLeft ? Index : Index + Length;
  82. // next match after an empty match: make sure scan ptr makes progress
  83. if (Length == 0)
  84. scan_ptr += regex.RightToLeft ? -1 : +1;
  85. return machine.Scan (regex, Text, scan_ptr, text_length);
  86. }
  87. public virtual string Result (string replacement) {
  88. return ReplacementEvaluator.Evaluate (replacement, this);
  89. }
  90. // internal
  91. internal Match () : base (null, null) {
  92. this.regex = null;
  93. this.machine = null;
  94. this.text_length = 0;
  95. this.groups = new GroupCollection ();
  96. groups.Add (this);
  97. }
  98. internal Match (Regex regex, IMachine machine, string text, int text_length, int[][] grps) :
  99. base (text, grps[0])
  100. {
  101. this.regex = regex;
  102. this.machine = machine;
  103. this.text_length = text_length;
  104. this.groups = new GroupCollection ();
  105. groups.Add (this);
  106. for (int i = 1; i < grps.Length; ++ i)
  107. groups.Add (new Group (text, grps[i]));
  108. }
  109. internal Regex Regex {
  110. get { return regex; }
  111. }
  112. // private
  113. private Regex regex;
  114. private IMachine machine;
  115. private int text_length;
  116. private GroupCollection groups;
  117. private static Match empty = new Match ();
  118. }
  119. }