Match.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 Match : Group {
  32. public static Match Empty {
  33. get { return empty; }
  34. }
  35. public static Match Synchronized (Match inner)
  36. {
  37. return inner; // FIXME need to sync on machine access
  38. }
  39. public virtual GroupCollection Groups {
  40. get { return groups; }
  41. }
  42. public Match NextMatch ()
  43. {
  44. if (this == Empty)
  45. return Empty;
  46. int scan_ptr = regex.RightToLeft ? Index : Index + Length;
  47. // next match after an empty match: make sure scan ptr makes progress
  48. if (Length == 0)
  49. scan_ptr += regex.RightToLeft ? -1 : +1;
  50. return machine.Scan (regex, Text, scan_ptr, text_length);
  51. }
  52. public virtual string Result (string replacement)
  53. {
  54. return ReplacementEvaluator.Evaluate (replacement, this);
  55. }
  56. // internal
  57. private Match () : base ()
  58. {
  59. this.regex = null;
  60. this.machine = null;
  61. this.text_length = 0;
  62. this.groups = new GroupCollection (1);
  63. groups.SetValue (this, 0);
  64. }
  65. internal Match (Regex regex, IMachine machine, string text, int text_length, int n_groups,
  66. int index, int length, int n_caps) :
  67. base (text, index, length, n_caps)
  68. {
  69. this.regex = regex;
  70. this.machine = machine;
  71. this.text_length = text_length;
  72. this.groups = new GroupCollection (n_groups);
  73. groups.SetValue (this, 0);
  74. }
  75. internal Regex Regex {
  76. get { return regex; }
  77. }
  78. // private
  79. private Regex regex;
  80. private IMachine machine;
  81. private int text_length;
  82. private GroupCollection groups;
  83. private static Match empty = new Match ();
  84. }
  85. }