-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
254 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
Shoko.Server/Filters/Logic/Strings/StringEndsWithExpression.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System; | ||
using Shoko.Server.Filters.Interfaces; | ||
|
||
namespace Shoko.Server.Filters.Logic.Strings; | ||
|
||
public class StringEndsWithExpression : FilterExpression<bool>, IWithStringSelectorParameter, IWithSecondStringSelectorParameter, IWithStringParameter | ||
{ | ||
public StringEndsWithExpression(FilterExpression<string> left, FilterExpression<string> right) | ||
{ | ||
Left = left; | ||
Right = right; | ||
} | ||
|
||
public StringEndsWithExpression(FilterExpression<string> left, string parameter) | ||
{ | ||
Left = left; | ||
Parameter = parameter; | ||
} | ||
|
||
public StringEndsWithExpression() { } | ||
|
||
public FilterExpression<string> Left { get; set; } | ||
public FilterExpression<string> Right { get; set; } | ||
public string Parameter { get; set; } | ||
public override bool TimeDependent => Left.TimeDependent || (Right?.TimeDependent ?? false); | ||
public override bool UserDependent => Left.UserDependent || (Right?.UserDependent ?? false); | ||
public override string HelpDescription => "This passes if the left selector ends with either the right selector or the parameter"; | ||
|
||
public override bool Evaluate(IFilterable filterable) | ||
{ | ||
var left = Left.Evaluate(filterable); | ||
var right = Parameter ?? Right?.Evaluate(filterable); | ||
if (string.IsNullOrEmpty(left) || string.IsNullOrEmpty(right)) | ||
{ | ||
return false; | ||
} | ||
|
||
return left.EndsWith(right, StringComparison.InvariantCultureIgnoreCase); | ||
} | ||
|
||
protected bool Equals(StringEndsWithExpression other) | ||
{ | ||
return base.Equals(other) && Equals(Left, other.Left) && Equals(Right, other.Right) && Parameter == other.Parameter; | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if (ReferenceEquals(null, obj)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (ReferenceEquals(this, obj)) | ||
{ | ||
return true; | ||
} | ||
|
||
if (obj.GetType() != this.GetType()) | ||
{ | ||
return false; | ||
} | ||
|
||
return Equals((StringEndsWithExpression)obj); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return HashCode.Combine(base.GetHashCode(), Left, Right, Parameter); | ||
} | ||
|
||
public static bool operator ==(StringEndsWithExpression left, StringEndsWithExpression right) | ||
{ | ||
return Equals(left, right); | ||
} | ||
|
||
public static bool operator !=(StringEndsWithExpression left, StringEndsWithExpression right) | ||
{ | ||
return !Equals(left, right); | ||
} | ||
|
||
public override bool IsType(FilterExpression expression) | ||
{ | ||
return expression is StringEndsWithExpression exp && Left.IsType(exp.Left) && (Right?.IsType(exp.Right) ?? true); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
Shoko.Server/Filters/Logic/Strings/StringRegexMatchesExpression.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System; | ||
using System.Text.RegularExpressions; | ||
using Shoko.Server.Filters.Interfaces; | ||
|
||
namespace Shoko.Server.Filters.Logic.Strings; | ||
|
||
public class StringRegexMatchesExpression : FilterExpression<bool>, IWithStringSelectorParameter, IWithStringParameter | ||
{ | ||
private readonly Regex _regex; | ||
public StringRegexMatchesExpression(FilterExpression<string> left, string parameter) | ||
{ | ||
Left = left; | ||
Parameter = parameter; | ||
try | ||
{ | ||
_regex = new Regex(parameter, RegexOptions.Compiled | RegexOptions.IgnoreCase); | ||
} | ||
catch { /* ignore */ } | ||
} | ||
|
||
public StringRegexMatchesExpression() { } | ||
|
||
public FilterExpression<string> Left { get; set; } | ||
public string Parameter { get; set; } | ||
public override bool TimeDependent => Left.TimeDependent; | ||
public override bool UserDependent => Left.UserDependent; | ||
public override string HelpDescription => "This passes if the left selector matches the regular expression given in the parameter"; | ||
|
||
public override bool Evaluate(IFilterable filterable) | ||
{ | ||
var left = Left.Evaluate(filterable); | ||
if (string.IsNullOrEmpty(left) || _regex == null) | ||
{ | ||
return false; | ||
} | ||
|
||
return _regex.IsMatch(left); | ||
} | ||
|
||
protected bool Equals(StringRegexMatchesExpression other) | ||
{ | ||
return base.Equals(other) && Equals(Left, other.Left) && Parameter == other.Parameter; | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if (ReferenceEquals(null, obj)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (ReferenceEquals(this, obj)) | ||
{ | ||
return true; | ||
} | ||
|
||
if (obj.GetType() != this.GetType()) | ||
{ | ||
return false; | ||
} | ||
|
||
return Equals((StringRegexMatchesExpression)obj); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return HashCode.Combine(base.GetHashCode(), Left, Parameter); | ||
} | ||
|
||
public static bool operator ==(StringRegexMatchesExpression left, StringRegexMatchesExpression right) | ||
{ | ||
return Equals(left, right); | ||
} | ||
|
||
public static bool operator !=(StringRegexMatchesExpression left, StringRegexMatchesExpression right) | ||
{ | ||
return !Equals(left, right); | ||
} | ||
|
||
public override bool IsType(FilterExpression expression) | ||
{ | ||
return expression is StringRegexMatchesExpression exp && Left.IsType(exp.Left); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
Shoko.Server/Filters/Logic/Strings/StringStartsWithExpression.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System; | ||
using Shoko.Server.Filters.Interfaces; | ||
|
||
namespace Shoko.Server.Filters.Logic.Strings; | ||
|
||
public class StringStartsWithExpression : FilterExpression<bool>, IWithStringSelectorParameter, IWithSecondStringSelectorParameter, IWithStringParameter | ||
{ | ||
public StringStartsWithExpression(FilterExpression<string> left, FilterExpression<string> right) | ||
{ | ||
Left = left; | ||
Right = right; | ||
} | ||
|
||
public StringStartsWithExpression(FilterExpression<string> left, string parameter) | ||
{ | ||
Left = left; | ||
Parameter = parameter; | ||
} | ||
|
||
public StringStartsWithExpression() { } | ||
|
||
public FilterExpression<string> Left { get; set; } | ||
public FilterExpression<string> Right { get; set; } | ||
public string Parameter { get; set; } | ||
public override bool TimeDependent => Left.TimeDependent || (Right?.TimeDependent ?? false); | ||
public override bool UserDependent => Left.UserDependent || (Right?.UserDependent ?? false); | ||
public override string HelpDescription => "This passes if the left selector starts with either the right selector or the parameter"; | ||
|
||
public override bool Evaluate(IFilterable filterable) | ||
{ | ||
var left = Left.Evaluate(filterable); | ||
var right = Parameter ?? Right?.Evaluate(filterable); | ||
if (string.IsNullOrEmpty(left) || string.IsNullOrEmpty(right)) | ||
{ | ||
return false; | ||
} | ||
|
||
return left.StartsWith(right, StringComparison.InvariantCultureIgnoreCase); | ||
} | ||
|
||
protected bool Equals(StringStartsWithExpression other) | ||
{ | ||
return base.Equals(other) && Equals(Left, other.Left) && Equals(Right, other.Right) && Parameter == other.Parameter; | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if (ReferenceEquals(null, obj)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (ReferenceEquals(this, obj)) | ||
{ | ||
return true; | ||
} | ||
|
||
if (obj.GetType() != this.GetType()) | ||
{ | ||
return false; | ||
} | ||
|
||
return Equals((StringStartsWithExpression)obj); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return HashCode.Combine(base.GetHashCode(), Left, Right, Parameter); | ||
} | ||
|
||
public static bool operator ==(StringStartsWithExpression left, StringStartsWithExpression right) | ||
{ | ||
return Equals(left, right); | ||
} | ||
|
||
public static bool operator !=(StringStartsWithExpression left, StringStartsWithExpression right) | ||
{ | ||
return !Equals(left, right); | ||
} | ||
|
||
public override bool IsType(FilterExpression expression) | ||
{ | ||
return expression is StringStartsWithExpression exp && Left.IsType(exp.Left) && (Right?.IsType(exp.Right) ?? true); | ||
} | ||
} |