mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Add boolean checking
This commit is contained in:
@@ -97,18 +97,24 @@ namespace SabreTools.Filter
|
|||||||
if (checkValue == null)
|
if (checkValue == null)
|
||||||
return this.Value == null;
|
return this.Value == null;
|
||||||
|
|
||||||
|
// If we have both a potentally boolean check and value
|
||||||
|
bool? checkValueBool = ConvertToBoolean(checkValue);
|
||||||
|
bool? matchValueBool = ConvertToBoolean(this.Value);
|
||||||
|
if (checkValueBool != null && matchValueBool != null)
|
||||||
|
return checkValueBool == matchValueBool;
|
||||||
|
|
||||||
// If we have both a potentially numeric check and value
|
// If we have both a potentially numeric check and value
|
||||||
if (NumberHelper.IsNumeric(checkValue) && NumberHelper.IsNumeric(this.Value))
|
if (NumberHelper.IsNumeric(checkValue) && NumberHelper.IsNumeric(this.Value))
|
||||||
{
|
{
|
||||||
// Check Int64 values
|
// Check Int64 values
|
||||||
long? checkValueLong = NumberHelper.ConvertToInt64(checkValue);
|
long? checkValueLong = NumberHelper.ConvertToInt64(checkValue);
|
||||||
long? matchValueLong = NumberHelper.ConvertToInt64(checkValue);
|
long? matchValueLong = NumberHelper.ConvertToInt64(this.Value);
|
||||||
if (checkValueLong != null && matchValueLong != null)
|
if (checkValueLong != null && matchValueLong != null)
|
||||||
return checkValueLong == matchValueLong;
|
return checkValueLong == matchValueLong;
|
||||||
|
|
||||||
// Check Double values
|
// Check Double values
|
||||||
double? checkValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
double? checkValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
||||||
double? matchValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
double? matchValueDouble = NumberHelper.ConvertToDouble(this.Value);
|
||||||
if (checkValueDouble != null && matchValueDouble != null)
|
if (checkValueDouble != null && matchValueDouble != null)
|
||||||
return checkValueDouble == matchValueDouble;
|
return checkValueDouble == matchValueDouble;
|
||||||
}
|
}
|
||||||
@@ -134,18 +140,24 @@ namespace SabreTools.Filter
|
|||||||
if (checkValue == null)
|
if (checkValue == null)
|
||||||
return this.Value == null;
|
return this.Value == null;
|
||||||
|
|
||||||
|
// If we have both a potentally boolean check and value
|
||||||
|
bool? checkValueBool = ConvertToBoolean(checkValue);
|
||||||
|
bool? matchValueBool = ConvertToBoolean(this.Value);
|
||||||
|
if (checkValueBool != null && matchValueBool != null)
|
||||||
|
return checkValueBool != matchValueBool;
|
||||||
|
|
||||||
// If we have both a potentially numeric check and value
|
// If we have both a potentially numeric check and value
|
||||||
if (NumberHelper.IsNumeric(checkValue) && NumberHelper.IsNumeric(this.Value))
|
if (NumberHelper.IsNumeric(checkValue) && NumberHelper.IsNumeric(this.Value))
|
||||||
{
|
{
|
||||||
// Check Int64 values
|
// Check Int64 values
|
||||||
long? checkValueLong = NumberHelper.ConvertToInt64(checkValue);
|
long? checkValueLong = NumberHelper.ConvertToInt64(checkValue);
|
||||||
long? matchValueLong = NumberHelper.ConvertToInt64(checkValue);
|
long? matchValueLong = NumberHelper.ConvertToInt64(this.Value);
|
||||||
if (checkValueLong != null && matchValueLong != null)
|
if (checkValueLong != null && matchValueLong != null)
|
||||||
return checkValueLong != matchValueLong;
|
return checkValueLong != matchValueLong;
|
||||||
|
|
||||||
// Check Double values
|
// Check Double values
|
||||||
double? checkValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
double? checkValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
||||||
double? matchValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
double? matchValueDouble = NumberHelper.ConvertToDouble(this.Value);
|
||||||
if (checkValueDouble != null && matchValueDouble != null)
|
if (checkValueDouble != null && matchValueDouble != null)
|
||||||
return checkValueDouble != matchValueDouble;
|
return checkValueDouble != matchValueDouble;
|
||||||
}
|
}
|
||||||
@@ -176,13 +188,13 @@ namespace SabreTools.Filter
|
|||||||
{
|
{
|
||||||
// Check Int64 values
|
// Check Int64 values
|
||||||
long? checkValueLong = NumberHelper.ConvertToInt64(checkValue);
|
long? checkValueLong = NumberHelper.ConvertToInt64(checkValue);
|
||||||
long? matchValueLong = NumberHelper.ConvertToInt64(checkValue);
|
long? matchValueLong = NumberHelper.ConvertToInt64(this.Value);
|
||||||
if (checkValueLong != null && matchValueLong != null)
|
if (checkValueLong != null && matchValueLong != null)
|
||||||
return checkValueLong > matchValueLong;
|
return checkValueLong > matchValueLong;
|
||||||
|
|
||||||
// Check Double values
|
// Check Double values
|
||||||
double? checkValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
double? checkValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
||||||
double? matchValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
double? matchValueDouble = NumberHelper.ConvertToDouble(this.Value);
|
||||||
if (checkValueDouble != null && matchValueDouble != null)
|
if (checkValueDouble != null && matchValueDouble != null)
|
||||||
return checkValueDouble > matchValueDouble;
|
return checkValueDouble > matchValueDouble;
|
||||||
}
|
}
|
||||||
@@ -209,13 +221,13 @@ namespace SabreTools.Filter
|
|||||||
{
|
{
|
||||||
// Check Int64 values
|
// Check Int64 values
|
||||||
long? checkValueLong = NumberHelper.ConvertToInt64(checkValue);
|
long? checkValueLong = NumberHelper.ConvertToInt64(checkValue);
|
||||||
long? matchValueLong = NumberHelper.ConvertToInt64(checkValue);
|
long? matchValueLong = NumberHelper.ConvertToInt64(this.Value);
|
||||||
if (checkValueLong != null && matchValueLong != null)
|
if (checkValueLong != null && matchValueLong != null)
|
||||||
return checkValueLong >= matchValueLong;
|
return checkValueLong >= matchValueLong;
|
||||||
|
|
||||||
// Check Double values
|
// Check Double values
|
||||||
double? checkValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
double? checkValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
||||||
double? matchValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
double? matchValueDouble = NumberHelper.ConvertToDouble(this.Value);
|
||||||
if (checkValueDouble != null && matchValueDouble != null)
|
if (checkValueDouble != null && matchValueDouble != null)
|
||||||
return checkValueDouble >= matchValueDouble;
|
return checkValueDouble >= matchValueDouble;
|
||||||
}
|
}
|
||||||
@@ -242,13 +254,13 @@ namespace SabreTools.Filter
|
|||||||
{
|
{
|
||||||
// Check Int64 values
|
// Check Int64 values
|
||||||
long? checkValueLong = NumberHelper.ConvertToInt64(checkValue);
|
long? checkValueLong = NumberHelper.ConvertToInt64(checkValue);
|
||||||
long? matchValueLong = NumberHelper.ConvertToInt64(checkValue);
|
long? matchValueLong = NumberHelper.ConvertToInt64(this.Value);
|
||||||
if (checkValueLong != null && matchValueLong != null)
|
if (checkValueLong != null && matchValueLong != null)
|
||||||
return checkValueLong < matchValueLong;
|
return checkValueLong < matchValueLong;
|
||||||
|
|
||||||
// Check Double values
|
// Check Double values
|
||||||
double? checkValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
double? checkValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
||||||
double? matchValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
double? matchValueDouble = NumberHelper.ConvertToDouble(this.Value);
|
||||||
if (checkValueDouble != null && matchValueDouble != null)
|
if (checkValueDouble != null && matchValueDouble != null)
|
||||||
return checkValueDouble < matchValueDouble;
|
return checkValueDouble < matchValueDouble;
|
||||||
}
|
}
|
||||||
@@ -275,13 +287,13 @@ namespace SabreTools.Filter
|
|||||||
{
|
{
|
||||||
// Check Int64 values
|
// Check Int64 values
|
||||||
long? checkValueLong = NumberHelper.ConvertToInt64(checkValue);
|
long? checkValueLong = NumberHelper.ConvertToInt64(checkValue);
|
||||||
long? matchValueLong = NumberHelper.ConvertToInt64(checkValue);
|
long? matchValueLong = NumberHelper.ConvertToInt64(this.Value);
|
||||||
if (checkValueLong != null && matchValueLong != null)
|
if (checkValueLong != null && matchValueLong != null)
|
||||||
return checkValueLong <= matchValueLong;
|
return checkValueLong <= matchValueLong;
|
||||||
|
|
||||||
// Check Double values
|
// Check Double values
|
||||||
double? checkValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
double? checkValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
||||||
double? matchValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
double? matchValueDouble = NumberHelper.ConvertToDouble(this.Value);
|
||||||
if (checkValueDouble != null && matchValueDouble != null)
|
if (checkValueDouble != null && matchValueDouble != null)
|
||||||
return checkValueDouble <= matchValueDouble;
|
return checkValueDouble <= matchValueDouble;
|
||||||
}
|
}
|
||||||
@@ -330,6 +342,25 @@ namespace SabreTools.Filter
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Convert a string to a Boolean
|
||||||
|
/// </summary>
|
||||||
|
private bool? ConvertToBoolean(string? value)
|
||||||
|
{
|
||||||
|
// If we don't have a valid string, we can't do anything
|
||||||
|
if (string.IsNullOrWhiteSpace(value))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return value.ToLowerInvariant() switch
|
||||||
|
{
|
||||||
|
"true" => true,
|
||||||
|
"yes" => true,
|
||||||
|
"false" => false,
|
||||||
|
"no" => false,
|
||||||
|
_ => null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Derive an operation from the input string, if possible
|
/// Derive an operation from the input string, if possible
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user