-
Notifications
You must be signed in to change notification settings - Fork 75
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
1 changed file
with
86 additions
and
0 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
Shoko.Server/Filters/Logic/Strings/StringFuzzyMatchesExpression.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,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); | ||
} | ||
} |