2020-09-26 13:16:54 +00:00
|
|
|
using System;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
namespace BunyMuny {
|
|
|
|
public class Rule {
|
|
|
|
public RuleMatch Match { get; set; }
|
|
|
|
public string Value { get; set; }
|
|
|
|
public string Category { get; set; }
|
|
|
|
public string Description { get; set; }
|
|
|
|
public bool CaseSensitive { get; set; } // TODO: make this have an effect
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks whether or not a given value matches the rule.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="input">The string to check against the rule's Value.</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public bool Check(string input) {
|
2020-09-27 03:59:58 +00:00
|
|
|
var value = Value;
|
|
|
|
if (!CaseSensitive) {
|
|
|
|
input = input.ToLower();
|
|
|
|
value = value.ToLower();
|
|
|
|
}
|
2020-09-26 13:16:54 +00:00
|
|
|
switch (Match) {
|
|
|
|
case RuleMatch.Exact:
|
2020-09-27 03:59:58 +00:00
|
|
|
return input == value;
|
2020-09-26 13:16:54 +00:00
|
|
|
case RuleMatch.Start:
|
2020-09-27 03:59:58 +00:00
|
|
|
return input.StartsWith(value);
|
2020-09-26 13:16:54 +00:00
|
|
|
case RuleMatch.End:
|
2020-09-27 03:59:58 +00:00
|
|
|
return input.EndsWith(value);
|
2020-09-26 13:16:54 +00:00
|
|
|
case RuleMatch.Regex:
|
2020-09-27 03:59:58 +00:00
|
|
|
var re = new Regex(value);
|
2020-09-26 13:16:54 +00:00
|
|
|
return re.IsMatch(input);
|
|
|
|
case RuleMatch.Contains:
|
2020-09-27 03:59:58 +00:00
|
|
|
return input.Contains(value);
|
2020-09-26 13:16:54 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|