Co-Authored-By: Mark McDowall <markus101@users.noreply.github.com>pull/646/head
parent
27ebe36454
commit
1cc12446b6
@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
using NzbDrone.Common.Cache;
|
||||
using NzbDrone.Core.Profiles.Releases.TermMatchers;
|
||||
|
||||
namespace NzbDrone.Core.Profiles.Releases
|
||||
{
|
||||
public interface ITermMatcherService
|
||||
{
|
||||
bool IsMatch(string term, string value);
|
||||
string MatchingTerm(string term, string value);
|
||||
}
|
||||
|
||||
public class TermMatcherService : ITermMatcherService
|
||||
{
|
||||
private ICached<ITermMatcher> _matcherCache;
|
||||
|
||||
public TermMatcherService(ICacheManager cacheManager)
|
||||
{
|
||||
_matcherCache = cacheManager.GetCache<ITermMatcher>(GetType());
|
||||
}
|
||||
|
||||
public bool IsMatch(string term, string value)
|
||||
{
|
||||
return GetMatcher(term).IsMatch(value);
|
||||
}
|
||||
|
||||
public string MatchingTerm(string term, string value)
|
||||
{
|
||||
return GetMatcher(term).MatchingTerm(value);
|
||||
}
|
||||
|
||||
public ITermMatcher GetMatcher(string term)
|
||||
{
|
||||
return _matcherCache.Get(term, () => CreateMatcherInternal(term), TimeSpan.FromHours(24));
|
||||
}
|
||||
|
||||
private ITermMatcher CreateMatcherInternal(string term)
|
||||
{
|
||||
Regex regex;
|
||||
if (PerlRegexFactory.TryCreateRegex(term, out regex))
|
||||
{
|
||||
return new RegexTermMatcher(regex);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new CaseInsensitiveTermMatcher(term);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
namespace NzbDrone.Core.Profiles.Releases.TermMatchers
|
||||
{
|
||||
public sealed class CaseInsensitiveTermMatcher : ITermMatcher
|
||||
{
|
||||
private readonly string _term;
|
||||
|
||||
public CaseInsensitiveTermMatcher(string term)
|
||||
{
|
||||
_term = term.ToLowerInvariant();
|
||||
}
|
||||
|
||||
public bool IsMatch(string value)
|
||||
{
|
||||
return value.ToLowerInvariant().Contains(_term);
|
||||
}
|
||||
|
||||
public string MatchingTerm(string value)
|
||||
{
|
||||
return _term;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
namespace NzbDrone.Core.Profiles.Releases.TermMatchers
|
||||
{
|
||||
public interface ITermMatcher
|
||||
{
|
||||
bool IsMatch(string value);
|
||||
string MatchingTerm(string value);
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace NzbDrone.Core.Profiles.Releases.TermMatchers
|
||||
{
|
||||
public class RegexTermMatcher : ITermMatcher
|
||||
{
|
||||
private readonly Regex _regex;
|
||||
|
||||
public RegexTermMatcher(Regex regex)
|
||||
{
|
||||
_regex = regex;
|
||||
}
|
||||
|
||||
public bool IsMatch(string value)
|
||||
{
|
||||
return _regex.IsMatch(value);
|
||||
}
|
||||
|
||||
public string MatchingTerm(string value)
|
||||
{
|
||||
return _regex.Match(value).Value;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue