jellyfin-server/Emby.Common.Implementations/IO/SharpCifs/Util/Sharpen/Matcher.cs

88 lines
2.1 KiB
C#
Raw Normal View History

2017-04-02 00:36:06 +00:00
using System;
using System.Text.RegularExpressions;
namespace SharpCifs.Util.Sharpen
{
internal class Matcher
2017-06-21 06:46:57 +00:00
{
private int _current;
private MatchCollection _matches;
private Regex _regex;
private string _str;
2017-04-02 00:36:06 +00:00
2017-06-21 06:46:57 +00:00
internal Matcher(Regex regex, string str)
{
this._regex = regex;
this._str = str;
}
2017-04-02 00:36:06 +00:00
2017-06-21 06:46:57 +00:00
public int End()
{
if ((_matches == null) || (_current >= _matches.Count))
{
throw new InvalidOperationException();
}
return (_matches[_current].Index + _matches[_current].Length);
}
2017-04-02 00:36:06 +00:00
2017-06-21 06:46:57 +00:00
public bool Find()
{
if (_matches == null)
{
_matches = _regex.Matches(_str);
_current = 0;
}
return (_current < _matches.Count);
}
2017-04-02 00:36:06 +00:00
2017-06-21 06:46:57 +00:00
public bool Find(int index)
{
_matches = _regex.Matches(_str, index);
_current = 0;
return (_matches.Count > 0);
}
2017-04-02 00:36:06 +00:00
2017-06-21 06:46:57 +00:00
public string Group(int n)
{
if ((_matches == null) || (_current >= _matches.Count))
{
throw new InvalidOperationException();
}
Group grp = _matches[_current].Groups[n];
return grp.Success ? grp.Value : null;
}
2017-04-02 00:36:06 +00:00
2017-06-21 06:46:57 +00:00
public bool Matches()
{
_matches = null;
return Find();
}
2017-04-02 00:36:06 +00:00
2017-06-21 06:46:57 +00:00
public string ReplaceFirst(string txt)
{
return _regex.Replace(_str, txt, 1);
}
2017-04-02 00:36:06 +00:00
2017-06-21 06:46:57 +00:00
public Matcher Reset(CharSequence str)
{
return Reset(str.ToString());
}
2017-04-02 00:36:06 +00:00
2017-06-21 06:46:57 +00:00
public Matcher Reset(string str)
{
_matches = null;
this._str = str;
return this;
}
2017-04-02 00:36:06 +00:00
2017-06-21 06:46:57 +00:00
public int Start()
{
if ((_matches == null) || (_current >= _matches.Count))
{
throw new InvalidOperationException();
}
return _matches[_current].Index;
}
}
2017-04-02 00:36:06 +00:00
}