Skip to content

Commit

Permalink
Add more string expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Oct 5, 2023
1 parent 0fb8b8c commit 98a2bff
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 0 deletions.
85 changes: 85 additions & 0 deletions Shoko.Server/Filters/Logic/Strings/StringEndsWithExpression.cs
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 Shoko.Server/Filters/Logic/Strings/StringRegexMatchesExpression.cs
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 Shoko.Server/Filters/Logic/Strings/StringStartsWithExpression.cs
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);
}
}

0 comments on commit 98a2bff

Please sign in to comment.