Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/replace regex #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 79 additions & 85 deletions Math expression eval/org.matheval/Common/Afe_Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -81,11 +82,6 @@ public static class Afe_Common
/// </summary>
public const string Const_CASE = "CASE";

/// <summary>
/// Condition expression WHEN
/// </summary>
//public const string Const_WHEN = "WHEN";

/// <summary>
/// Condition expression BRACKETS OPEN
/// </summary>
Expand Down Expand Up @@ -224,17 +220,17 @@ public static class Afe_Common
#region Function

/// <summary>
/// Funtion Text
/// Function Text
/// </summary>
public const string Const_Text = "text";

/// <summary>
/// Funtion TODAY
/// Function TODAY
/// </summary>
public const string Const_TODAY = "TODAY";

/// <summary>
/// Funtion left
/// Function left
/// </summary>
public const string Const_LEFT = "left";

Expand Down Expand Up @@ -655,49 +651,36 @@ public static class Afe_Common
/// </summary>
/// <param name="value">The value to act on.</param>
/// <returns>true if Alpha, false if not.</returns>
/*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 ('_');
}

/// <summary>
/// A string extension method that query if value is IsAlphaNumeric.
/// </summary>
/// <param name="value">The value to act on.</param>
/// <returns>true if IsAlphaNumeric, false if not.</returns>
/*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);
}

/// <summary>
/// A string extension method that query if value is isNumeric.
/// </summary>
/// <param name="value">The value to act on.</param>
/// <returns>true if IsNumeric, false if not.</returns>
/*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');
}

/// <summary>
Expand Down Expand Up @@ -754,14 +737,10 @@ public static bool IsNumber(object value)
/// <returns></returns>
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);
}

/// <summary>
Expand All @@ -773,14 +752,12 @@ public static decimal ToDecimal(object value, CultureInfo cultureInfo)
/// <returns></returns>
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);
}


Expand Down Expand Up @@ -818,51 +795,68 @@ public static decimal RoundManualScale(object value, object digit, ExpressionCon
/// <returns>Total Value</returns>
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
Expand Down
1 change: 1 addition & 0 deletions Math expression eval/org.matheval/org.matheval.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
<FileVersion>1.0.0.2</FileVersion>
<SignAssembly>false</SignAssembly>
<RepositoryType>git</RepositoryType>
<LangVersion>latest</LangVersion>
</PropertyGroup>
</Project>