Matcher.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Text.RegularExpressions;
  3. namespace SharpCifs.Util.Sharpen
  4. {
  5. internal class Matcher
  6. {
  7. private int _current;
  8. private MatchCollection _matches;
  9. private Regex _regex;
  10. private string _str;
  11. internal Matcher (Regex regex, string str)
  12. {
  13. this._regex = regex;
  14. this._str = str;
  15. }
  16. public int End ()
  17. {
  18. if ((_matches == null) || (_current >= _matches.Count)) {
  19. throw new InvalidOperationException ();
  20. }
  21. return (_matches[_current].Index + _matches[_current].Length);
  22. }
  23. public bool Find ()
  24. {
  25. if (_matches == null) {
  26. _matches = _regex.Matches (_str);
  27. _current = 0;
  28. }
  29. return (_current < _matches.Count);
  30. }
  31. public bool Find (int index)
  32. {
  33. _matches = _regex.Matches (_str, index);
  34. _current = 0;
  35. return (_matches.Count > 0);
  36. }
  37. public string Group (int n)
  38. {
  39. if ((_matches == null) || (_current >= _matches.Count)) {
  40. throw new InvalidOperationException ();
  41. }
  42. Group grp = _matches[_current].Groups[n];
  43. return grp.Success ? grp.Value : null;
  44. }
  45. public bool Matches ()
  46. {
  47. _matches = null;
  48. return Find ();
  49. }
  50. public string ReplaceFirst (string txt)
  51. {
  52. return _regex.Replace (_str, txt, 1);
  53. }
  54. public Matcher Reset (CharSequence str)
  55. {
  56. return Reset (str.ToString ());
  57. }
  58. public Matcher Reset (string str)
  59. {
  60. _matches = null;
  61. this._str = str;
  62. return this;
  63. }
  64. public int Start ()
  65. {
  66. if ((_matches == null) || (_current >= _matches.Count)) {
  67. throw new InvalidOperationException ();
  68. }
  69. return _matches[_current].Index;
  70. }
  71. }
  72. }