match.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // assembly: System
  3. // namespace: System.Text.RegularExpressions
  4. // file: match.cs
  5. //
  6. // author: Dan Lewis ([email protected])
  7. // (c) 2002
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. namespace System.Text.RegularExpressions {
  30. [Serializable]
  31. public class Capture {
  32. public int Index {
  33. get { return index; }
  34. }
  35. public int Length {
  36. get { return length; }
  37. }
  38. public string Value {
  39. get {
  40. if (text!= null)
  41. return text.Substring (index, length);
  42. else
  43. return String.Empty;
  44. }
  45. }
  46. public override string ToString () {
  47. return Value;
  48. }
  49. // internal members
  50. internal Capture (string text) : this (text, 0, 0) { }
  51. internal Capture (string text, int index, int length) {
  52. this.text = text;
  53. this.index = index;
  54. this.length = length;
  55. }
  56. internal string Text {
  57. get { return text; }
  58. }
  59. // private
  60. internal int index, length;
  61. internal string text;
  62. }
  63. [Serializable]
  64. public class Group : Capture {
  65. public static Group Synchronized (Group inner) {
  66. return inner; // is this enough?
  67. }
  68. public CaptureCollection Captures {
  69. get { return captures; }
  70. }
  71. public bool Success {
  72. get { return success; }
  73. }
  74. // internal
  75. internal Group (string text, int[] caps) : base (text) {
  76. this.captures = new CaptureCollection ();
  77. if (caps == null || caps.Length == 0) {
  78. this.success = false;
  79. return;
  80. }
  81. this.success = true;
  82. this.index = caps[0];
  83. this.length = caps[1];
  84. captures.Add (this);
  85. for (int i = 2; i < caps.Length; i += 2)
  86. captures.Add (new Capture (text, caps[i], caps[i + 1]));
  87. captures.Reverse ();
  88. }
  89. internal Group (): base ("")
  90. {
  91. captures = new CaptureCollection ();
  92. }
  93. private bool success;
  94. private CaptureCollection captures;
  95. }
  96. [Serializable]
  97. public class Match : Group {
  98. public static Match Empty {
  99. get { return empty; }
  100. }
  101. public static Match Synchronized (Match inner) {
  102. return inner; // FIXME need to sync on machine access
  103. }
  104. public virtual GroupCollection Groups {
  105. get { return groups; }
  106. }
  107. public Match NextMatch () {
  108. if (this == Empty)
  109. return Empty;
  110. int scan_ptr = regex.RightToLeft ? Index : Index + Length;
  111. // next match after an empty match: make sure scan ptr makes progress
  112. if (Length == 0)
  113. scan_ptr += regex.RightToLeft ? -1 : +1;
  114. return machine.Scan (regex, Text, scan_ptr, text_length);
  115. }
  116. public virtual string Result (string replacement) {
  117. return ReplacementEvaluator.Evaluate (replacement, this);
  118. }
  119. // internal
  120. internal Match () : base (null, null) {
  121. this.regex = null;
  122. this.machine = null;
  123. this.text_length = 0;
  124. this.groups = new GroupCollection ();
  125. groups.Add (this);
  126. }
  127. internal Match (Regex regex, IMachine machine, string text, int text_length, int[][] grps) :
  128. base (text, grps[0])
  129. {
  130. this.regex = regex;
  131. this.machine = machine;
  132. this.text_length = text_length;
  133. this.groups = new GroupCollection ();
  134. groups.Add (this);
  135. for (int i = 1; i < grps.Length; ++ i)
  136. groups.Add (new Group (text, grps[i]));
  137. }
  138. internal Regex Regex {
  139. get { return regex; }
  140. }
  141. // private
  142. private Regex regex;
  143. private IMachine machine;
  144. private int text_length;
  145. private GroupCollection groups;
  146. private static Match empty = new Match ();
  147. }
  148. }