From 9a6e8da8a7e385771e49bae78d7bd346314f78cd Mon Sep 17 00:00:00 2001 From: "Schulze, Steve" Date: Fri, 30 Dec 2022 20:04:17 +0100 Subject: [PATCH 1/2] Switched to latest lang version --- Math expression eval/org.matheval/org.matheval.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/Math expression eval/org.matheval/org.matheval.csproj b/Math expression eval/org.matheval/org.matheval.csproj index 677a79e..09b637f 100644 --- a/Math expression eval/org.matheval/org.matheval.csproj +++ b/Math expression eval/org.matheval/org.matheval.csproj @@ -24,5 +24,6 @@ 1.0.0.2 false git + latest From ab620591c05b1b733378648d16545bc905ab8d10 Mon Sep 17 00:00:00 2001 From: "Schulze, Steve" Date: Fri, 30 Dec 2022 20:05:19 +0100 Subject: [PATCH 2/2] Removed Regex for Alpha Numeric parsing (performance issue) --- .../org.matheval/Common/Afe_Common.cs | 164 +++++++++--------- 1 file changed, 79 insertions(+), 85 deletions(-) diff --git a/Math expression eval/org.matheval/Common/Afe_Common.cs b/Math expression eval/org.matheval/Common/Afe_Common.cs index dd4c207..cc3a0fb 100644 --- a/Math expression eval/org.matheval/Common/Afe_Common.cs +++ b/Math expression eval/org.matheval/Common/Afe_Common.cs @@ -25,7 +25,8 @@ THE SOFTWARE. using System.Collections; using System.Collections.Generic; using System.Globalization; -using System.Text.RegularExpressions; +// ReSharper disable InconsistentNaming +// ReSharper disable IdentifierTypo namespace org.matheval.Common { @@ -81,11 +82,6 @@ public static class Afe_Common /// public const string Const_CASE = "CASE"; - /// - /// Condition expression WHEN - /// - //public const string Const_WHEN = "WHEN"; - /// /// Condition expression BRACKETS OPEN /// @@ -224,17 +220,17 @@ public static class Afe_Common #region Function /// - /// Funtion Text + /// Function Text /// public const string Const_Text = "text"; /// - /// Funtion TODAY + /// Function TODAY /// public const string Const_TODAY = "TODAY"; /// - /// Funtion left + /// Function left /// public const string Const_LEFT = "left"; @@ -655,16 +651,13 @@ public static class Afe_Common /// /// The value to act on. /// true if Alpha, false if not. - /*public static bool IsAlpha(string value) - { - //return !Regex.IsMatch(value, "[^a-zA-Z]"); - Regex rg = new Regex(@"^[a-zA-Z_]+$"); - return rg.IsMatch(value); - }*/ public static bool IsAlpha(char value) { - Regex rg = new Regex(@"^[a-zA-Z_]+$"); - return rg.IsMatch(value.ToString()); + //Regex rg = new Regex(@"^[a-zA-Z_]+$"); + //return rg.IsMatch(value.ToString()); + return value is (>='A' and <='Z') + or (>='a' and <='z') + or ('_'); } /// @@ -672,15 +665,10 @@ public static bool IsAlpha(char value) /// /// The value to act on. /// true if IsAlphaNumeric, false if not. - /*public static bool IsAlphaNumeric(string value) - { - Regex rg = new Regex(@"^[a-zA-Z0-9_]+$"); - return rg.IsMatch(value); - }*/ public static bool IsAlphaNumeric(char value) { - Regex rg = new Regex(@"^[a-zA-Z0-9_]+$"); - return rg.IsMatch(value.ToString()); + //Regex rg = new Regex(@"^[a-zA-Z0-9_]+$"); + return IsAlpha(value) || IsNumeric(value); } /// @@ -688,16 +676,11 @@ public static bool IsAlphaNumeric(char value) /// /// The value to act on. /// true if IsNumeric, false if not. - /*public static bool IsNumeric(string value) - { - //Regex rg = new Regex(@"^(-?)(0|([1-9][0-9]*))(\\.[0-9]+)?$"); - Regex rg = new Regex(@"^[0-9]+$"); - return rg.IsMatch(value); - }*/ public static bool IsNumeric(char value) { - Regex rg = new Regex(@"^[0-9]+$"); - return rg.IsMatch(value.ToString()); + //Regex rg = new Regex(@"^[0-9]+$"); + //return rg.IsMatch(value.ToString()); + return value is (>= '0' and <= '9'); } /// @@ -754,14 +737,10 @@ public static bool IsNumber(object value) /// public static decimal ToDecimal(object value, CultureInfo cultureInfo) { - if (value is decimal) - { - return (decimal)value; - } - else - { - return Convert.ToDecimal(value, cultureInfo); - } + if (value is decimal dec) + return dec; + + return Convert.ToDecimal(value, cultureInfo); } /// @@ -773,14 +752,12 @@ public static decimal ToDecimal(object value, CultureInfo cultureInfo) /// public static string ToString(object value, CultureInfo cultureInfo) { - if (value is string) - { - return (string)value; - } - else + if (value is string stringValue) { - return Convert.ToString(value, cultureInfo); + return stringValue; } + + return Convert.ToString(value, cultureInfo); } @@ -818,51 +795,68 @@ public static decimal RoundManualScale(object value, object digit, ExpressionCon /// Total Value public static int DateDif(DateTime startDate, DateTime endDate, string unit) { - // TODO: - if (unit != null && unit.Equals("d", StringComparison.InvariantCultureIgnoreCase)) - { - return (endDate - startDate).Days; - } - else if (unit != null && unit.Equals("m", StringComparison.InvariantCultureIgnoreCase)) + while (true) { - int monthDiff = endDate.Year * 12 + endDate.Month - (startDate.Year * 12 + startDate.Month); - if (endDate.Day < startDate.Day) + // TODO: + if (unit != null && unit.Equals("d", StringComparison.InvariantCultureIgnoreCase)) { - monthDiff--; + return (endDate - startDate).Days; } - return monthDiff; - } - else if (unit != null && unit.Equals("y", StringComparison.InvariantCultureIgnoreCase)) - { - int monthDiff = endDate.Year * 12 + endDate.Month - (startDate.Year * 12 + startDate.Month); - if (endDate.Day < startDate.Day) + + if (unit != null && unit.Equals("m", StringComparison.InvariantCultureIgnoreCase)) + { + int monthDiff = endDate.Year * 12 + endDate.Month - (startDate.Year * 12 + startDate.Month); + if (endDate.Day < startDate.Day) + { + monthDiff--; + } + + return monthDiff; + } + + if (unit != null && unit.Equals("y", StringComparison.InvariantCultureIgnoreCase)) { - monthDiff--; + int monthDiff = endDate.Year * 12 + endDate.Month - (startDate.Year * 12 + startDate.Month); + if (endDate.Day < startDate.Day) + { + monthDiff--; + } + + return monthDiff / 12; } - return monthDiff / 12; - } - else if (unit != null && unit.Equals("ym", StringComparison.InvariantCultureIgnoreCase)) - { - DateTime stDate = startDate; - int yearDiff = DateDif(startDate, endDate, "y"); - stDate = stDate.AddYears(yearDiff); - return DateDif(stDate, endDate, "m"); - } - else if (unit != null && unit.Equals("yd", StringComparison.InvariantCultureIgnoreCase)) - { - DateTime stDate = startDate; - int yearDiff = DateDif(startDate, endDate, "y"); - stDate = stDate.AddYears(yearDiff); - return DateDif(stDate, endDate, "d"); - } - else if (unit != null && unit.Equals("md", StringComparison.InvariantCultureIgnoreCase)) - { - DateTime stDate = startDate; - int mDiff = DateDif(startDate, endDate, "m"); - stDate = stDate.AddMonths(mDiff); - return DateDif(stDate, endDate, "d"); + + if (unit != null && unit.Equals("ym", StringComparison.InvariantCultureIgnoreCase)) + { + var stDate = startDate; + var yearDiff = DateDif(startDate, endDate, "y"); + stDate = stDate.AddYears(yearDiff); + startDate = stDate; + unit = "m"; + continue; + } + + if (unit != null && unit.Equals("yd", StringComparison.InvariantCultureIgnoreCase)) + { + var stDate = startDate; + var yearDiff = DateDif(startDate, endDate, "y"); + stDate = stDate.AddYears(yearDiff); + startDate = stDate; + unit = "d"; + continue; + } + + if (unit != null && unit.Equals("md", StringComparison.InvariantCultureIgnoreCase)) + { + var stDate = startDate; + var mDiff = DateDif(startDate, endDate, "m"); + stDate = stDate.AddMonths(mDiff); + startDate = stDate; + unit = "d"; + continue; + } + + throw new Exception("Please set M or D or Y for UNIT param"); } - throw new Exception("Please set M or D or Y for UNIT param"); } #endregion