mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Use NumberHelper in FilterObject
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace SabreTools.Core.Tools
|
||||
{
|
||||
@@ -81,8 +82,11 @@ namespace SabreTools.Core.Tools
|
||||
/// <summary>
|
||||
/// Determine the multiplier from a numeric string
|
||||
/// </summary>
|
||||
private static long DetermineMultiplier(string numeric)
|
||||
public static long DetermineMultiplier(string? numeric)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(numeric))
|
||||
return 0;
|
||||
|
||||
long multiplier = 1;
|
||||
if (numeric.EndsWith("k") || numeric.EndsWith("kb"))
|
||||
multiplier = KiloByte;
|
||||
@@ -119,5 +123,67 @@ namespace SabreTools.Core.Tools
|
||||
|
||||
return multiplier;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determine if a string is fully numeric or not
|
||||
/// </summary>
|
||||
public static bool IsNumeric(string? value)
|
||||
{
|
||||
// If we have no value, it is not numeric
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
return false;
|
||||
|
||||
// If we have a hex value
|
||||
value = value.ToLowerInvariant();
|
||||
if (value.StartsWith("0x"))
|
||||
value = value[2..];
|
||||
|
||||
if (DetermineMultiplier(value) > 1)
|
||||
value = value.TrimEnd(new char[] { 'k', 'm', 'g', 't', 'p', 'e', 'z', 'y', 'i', 'b', ' ' });
|
||||
|
||||
#if NET7_0_OR_GREATER
|
||||
return value.All(c => char.IsAsciiHexDigit(c) || c == '.' || c == ',');
|
||||
#else
|
||||
return value.All(c => c.IsAsciiHexDigit()|| c == '.' || c == ',');
|
||||
#endif
|
||||
}
|
||||
|
||||
#if NET6_0
|
||||
/// <summary>
|
||||
/// Indicates whether a character is categorized as an ASCII hexademical digit.
|
||||
/// </summary>
|
||||
/// <param name="c">The character to evaluate.</param>
|
||||
/// <returns>true if c is a hexademical digit; otherwise, false.</returns>
|
||||
/// <remarks>This method determines whether the character is in the range '0' through '9', inclusive, 'A' through 'F', inclusive, or 'a' through 'f', inclusive.</remarks>
|
||||
private static bool IsAsciiHexDigit(this char c)
|
||||
{
|
||||
return c switch
|
||||
{
|
||||
'0' => true,
|
||||
'1' => true,
|
||||
'2' => true,
|
||||
'3' => true,
|
||||
'4' => true,
|
||||
'5' => true,
|
||||
'6' => true,
|
||||
'7' => true,
|
||||
'8' => true,
|
||||
'9' => true,
|
||||
'a' => true,
|
||||
'A' => true,
|
||||
'b' => true,
|
||||
'B' => true,
|
||||
'c' => true,
|
||||
'C' => true,
|
||||
'd' => true,
|
||||
'D' => true,
|
||||
'e' => true,
|
||||
'E' => true,
|
||||
'f' => true,
|
||||
'F' => true,
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using SabreTools.Core.Tools;
|
||||
using SabreTools.Models.Internal;
|
||||
|
||||
namespace SabreTools.Filter
|
||||
@@ -86,7 +88,6 @@ namespace SabreTools.Filter
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// TODO: Add regex matching to this method
|
||||
/// TODO: Add logic to convert SI suffixes and hex
|
||||
/// </remarks>
|
||||
private bool MatchesEqual(DictionaryBase dictionaryBase)
|
||||
{
|
||||
@@ -94,6 +95,19 @@ namespace SabreTools.Filter
|
||||
return this.Value == null;
|
||||
|
||||
string? checkValue = dictionaryBase.ReadString(this.Key[1]);
|
||||
if (NumberHelper.IsNumeric(checkValue) && NumberHelper.IsNumeric(this.Value))
|
||||
{
|
||||
long? checkValueLong = NumberHelper.ConvertToInt64(checkValue);
|
||||
long? matchValueLong = NumberHelper.ConvertToInt64(checkValue);
|
||||
if (checkValueLong != null && matchValueLong != null)
|
||||
return checkValueLong == matchValueLong;
|
||||
|
||||
double? checkValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
||||
double? matchValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
||||
if (checkValueDouble != null && matchValueDouble != null)
|
||||
return checkValueDouble == matchValueDouble;
|
||||
}
|
||||
|
||||
return checkValue == this.Value;
|
||||
}
|
||||
|
||||
@@ -102,7 +116,6 @@ namespace SabreTools.Filter
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// TODO: Add regex matching to this method
|
||||
/// TODO: Add logic to convert SI suffixes and hex
|
||||
/// </remarks>
|
||||
private bool MatchesNotEqual(DictionaryBase dictionaryBase)
|
||||
{
|
||||
@@ -110,13 +123,25 @@ namespace SabreTools.Filter
|
||||
return this.Value != null;
|
||||
|
||||
string? checkValue = dictionaryBase.ReadString(this.Key[1]);
|
||||
if (NumberHelper.IsNumeric(checkValue) && NumberHelper.IsNumeric(this.Value))
|
||||
{
|
||||
long? checkValueLong = NumberHelper.ConvertToInt64(checkValue);
|
||||
long? matchValueLong = NumberHelper.ConvertToInt64(checkValue);
|
||||
if (checkValueLong != null && matchValueLong != null)
|
||||
return checkValueLong != matchValueLong;
|
||||
|
||||
double? checkValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
||||
double? matchValueDouble = NumberHelper.ConvertToDouble(checkValue);
|
||||
if (checkValueDouble != null && matchValueDouble != null)
|
||||
return checkValueDouble != matchValueDouble;
|
||||
}
|
||||
|
||||
return checkValue != this.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if a value is strictly greater than
|
||||
/// </summary>
|
||||
/// <remarks>TODO: Add logic to convert SI suffixes and hex</remarks>
|
||||
private bool MatchesGreaterThan(DictionaryBase dictionaryBase)
|
||||
{
|
||||
if (!dictionaryBase.ContainsKey(this.Key[1]))
|
||||
@@ -125,7 +150,8 @@ namespace SabreTools.Filter
|
||||
long? checkLongValue = dictionaryBase.ReadLong(this.Key[1]);
|
||||
if (checkLongValue != null)
|
||||
{
|
||||
if (!long.TryParse(this.Value, out long matchValue))
|
||||
long? matchValue = NumberHelper.ConvertToInt64(this.Value);
|
||||
if (matchValue == null)
|
||||
return false;
|
||||
|
||||
return checkLongValue > matchValue;
|
||||
@@ -134,7 +160,8 @@ namespace SabreTools.Filter
|
||||
double? checkDoubleValue = dictionaryBase.ReadDouble(this.Key[1]);
|
||||
if (checkDoubleValue != null)
|
||||
{
|
||||
if (!double.TryParse(this.Value, out double matchValue))
|
||||
double? matchValue = NumberHelper.ConvertToDouble(this.Value);
|
||||
if (matchValue == null)
|
||||
return false;
|
||||
|
||||
return checkDoubleValue > matchValue;
|
||||
@@ -146,7 +173,6 @@ namespace SabreTools.Filter
|
||||
/// <summary>
|
||||
/// Determines if a value is greater than or equal
|
||||
/// </summary>
|
||||
/// <remarks>TODO: Add logic to convert SI suffixes and hex</remarks>
|
||||
private bool MatchesGreaterThanOrEqual(DictionaryBase dictionaryBase)
|
||||
{
|
||||
if (!dictionaryBase.ContainsKey(this.Key[1]))
|
||||
@@ -155,7 +181,8 @@ namespace SabreTools.Filter
|
||||
long? checkLongValue = dictionaryBase.ReadLong(this.Key[1]);
|
||||
if (checkLongValue != null)
|
||||
{
|
||||
if (!long.TryParse(this.Value, out long matchValue))
|
||||
long? matchValue = NumberHelper.ConvertToInt64(this.Value);
|
||||
if (matchValue == null)
|
||||
return false;
|
||||
|
||||
return checkLongValue >= matchValue;
|
||||
@@ -164,7 +191,8 @@ namespace SabreTools.Filter
|
||||
double? checkDoubleValue = dictionaryBase.ReadDouble(this.Key[1]);
|
||||
if (checkDoubleValue != null)
|
||||
{
|
||||
if (!double.TryParse(this.Value, out double matchValue))
|
||||
double? matchValue = NumberHelper.ConvertToDouble(this.Value);
|
||||
if (matchValue == null)
|
||||
return false;
|
||||
|
||||
return checkDoubleValue >= matchValue;
|
||||
@@ -176,7 +204,6 @@ namespace SabreTools.Filter
|
||||
/// <summary>
|
||||
/// Determines if a value is strictly less than
|
||||
/// </summary>
|
||||
/// <remarks>TODO: Add logic to convert SI suffixes and hex</remarks>
|
||||
private bool MatchesLessThan(DictionaryBase dictionaryBase)
|
||||
{
|
||||
if (!dictionaryBase.ContainsKey(this.Key[1]))
|
||||
@@ -185,7 +212,8 @@ namespace SabreTools.Filter
|
||||
long? checkLongValue = dictionaryBase.ReadLong(this.Key[1]);
|
||||
if (checkLongValue != null)
|
||||
{
|
||||
if (!long.TryParse(this.Value, out long matchValue))
|
||||
long? matchValue = NumberHelper.ConvertToInt64(this.Value);
|
||||
if (matchValue == null)
|
||||
return false;
|
||||
|
||||
return checkLongValue < matchValue;
|
||||
@@ -194,7 +222,8 @@ namespace SabreTools.Filter
|
||||
double? checkDoubleValue = dictionaryBase.ReadDouble(this.Key[1]);
|
||||
if (checkDoubleValue != null)
|
||||
{
|
||||
if (!double.TryParse(this.Value, out double matchValue))
|
||||
double? matchValue = NumberHelper.ConvertToDouble(this.Value);
|
||||
if (matchValue == null)
|
||||
return false;
|
||||
|
||||
return checkDoubleValue < matchValue;
|
||||
@@ -206,7 +235,6 @@ namespace SabreTools.Filter
|
||||
/// <summary>
|
||||
/// Determines if a value is less than or equal
|
||||
/// </summary>
|
||||
/// <remarks>TODO: Add logic to convert SI suffixes and hex</remarks>
|
||||
private bool MatchesLessThanOrEqual(DictionaryBase dictionaryBase)
|
||||
{
|
||||
if (!dictionaryBase.ContainsKey(this.Key[1]))
|
||||
@@ -215,7 +243,8 @@ namespace SabreTools.Filter
|
||||
long? checkLongValue = dictionaryBase.ReadLong(this.Key[1]);
|
||||
if (checkLongValue != null)
|
||||
{
|
||||
if (!long.TryParse(this.Value, out long matchValue))
|
||||
long? matchValue = NumberHelper.ConvertToInt64(this.Value);
|
||||
if (matchValue == null)
|
||||
return false;
|
||||
|
||||
return checkLongValue <= matchValue;
|
||||
@@ -224,7 +253,8 @@ namespace SabreTools.Filter
|
||||
double? checkDoubleValue = dictionaryBase.ReadDouble(this.Key[1]);
|
||||
if (checkDoubleValue != null)
|
||||
{
|
||||
if (!double.TryParse(this.Value, out double matchValue))
|
||||
double? matchValue = NumberHelper.ConvertToDouble(this.Value);
|
||||
if (matchValue == null)
|
||||
return false;
|
||||
|
||||
return checkDoubleValue <= matchValue;
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SabreTools.Core\SabreTools.Core.csproj" />
|
||||
<ProjectReference Include="..\SabreTools.Models\SabreTools.Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user