Skip to content

Commit

Permalink
Add Fuzzy Match Filter Expression
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Dec 3, 2023
1 parent 5ed005a commit 52ee489
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions Shoko.Server/Filters/Logic/Strings/StringFuzzyMatchesExpression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using Shoko.Commons.Utils;
using Shoko.Server.Filters.Interfaces;

namespace Shoko.Server.Filters.Logic.Strings;

public class StringFuzzyMatchesExpression : FilterExpression<bool>, IWithStringSelectorParameter, IWithSecondStringSelectorParameter, IWithStringParameter
{
public StringFuzzyMatchesExpression(FilterExpression<string> left, FilterExpression<string> right)
{
Left = left;
Right = right;
}

public StringFuzzyMatchesExpression(FilterExpression<string> left, string parameter)
{
Left = left;
Parameter = parameter;
}

public StringFuzzyMatchesExpression() { }

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 fuzzy matches either the right selector or the parameter";

public override bool Evaluate(IFilterable filterable, IFilterableUserInfo userInfo)
{
var left = Left.Evaluate(filterable, userInfo);
var right = Parameter ?? Right?.Evaluate(filterable, userInfo);
if (string.IsNullOrEmpty(left) || string.IsNullOrEmpty(right))
{
return false;
}

return left.FuzzyMatches(right);
}

protected bool Equals(StringFuzzyMatchesExpression 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((StringFuzzyMatchesExpression)obj);
}

public override int GetHashCode()
{
return HashCode.Combine(base.GetHashCode(), Left, Right, Parameter);
}

public static bool operator ==(StringFuzzyMatchesExpression left, StringFuzzyMatchesExpression right)
{
return Equals(left, right);
}

public static bool operator !=(StringFuzzyMatchesExpression left, StringFuzzyMatchesExpression right)
{
return !Equals(left, right);
}

public override bool IsType(FilterExpression expression)
{
return expression is StringFuzzyMatchesExpression exp && Left.IsType(exp.Left) && (Right?.IsType(exp.Right) ?? true);
}
}

0 comments on commit 52ee489

Please sign in to comment.