Matcher.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. {
  20. throw new InvalidOperationException();
  21. }
  22. return (_matches[_current].Index + _matches[_current].Length);
  23. }
  24. public bool Find()
  25. {
  26. if (_matches == null)
  27. {
  28. _matches = _regex.Matches(_str);
  29. _current = 0;
  30. }
  31. return (_current < _matches.Count);
  32. }
  33. public bool Find(int index)
  34. {
  35. _matches = _regex.Matches(_str, index);
  36. _current = 0;
  37. return (_matches.Count > 0);
  38. }
  39. public string Group(int n)
  40. {
  41. if ((_matches == null) || (_current >= _matches.Count))
  42. {
  43. throw new InvalidOperationException();
  44. }
  45. Group grp = _matches[_current].Groups[n];
  46. return grp.Success ? grp.Value : null;
  47. }
  48. public bool Matches()
  49. {
  50. _matches = null;
  51. return Find();
  52. }
  53. public string ReplaceFirst(string txt)
  54. {
  55. return _regex.Replace(_str, txt, 1);
  56. }
  57. public Matcher Reset(CharSequence str)
  58. {
  59. return Reset(str.ToString());
  60. }
  61. public Matcher Reset(string str)
  62. {
  63. _matches = null;
  64. this._str = str;
  65. return this;
  66. }
  67. public int Start()
  68. {
  69. if ((_matches == null) || (_current >= _matches.Count))
  70. {
  71. throw new InvalidOperationException();
  72. }
  73. return _matches[_current].Index;
  74. }
  75. }
  76. }