You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
when implenting custom string functions for NCalc, we discovered a bug which prevents using FunctionArgs.Parameters[N].Evaluate() along with EvaluateOptions.IterateParamers when evaluating parameters in custom function evaluators.
Evaluated string parametes are treated as IEnumerable, and therefore fail the same count check in Expression, see:
Hi,
I'm looking for your problem, I think the exception is the result of a recursion to the Iterate loop in the Expression.cs. I suggest the following changes:
Add the marked line to this method in FuntionArgs.cs
public object[] EvaluateParameters()
{
var values = new object[_parameters.Length];
for (var i = 0; i < values.Length; i++)
{
parameters[i].Options &= ~EvaluateOptions.IterateParameters; //<- add this line
values[i] = _parameters[i].Evaluate();
}
return values;
}
``
and change your code to this:
private static void EndsWith(string name, FunctionArgs args)
{
if (name == "EndsWith")
{
var parameters = args.EvaluateParameters();
var firstParam = parameters[0];
var secondParam = parameters[1];
if (firstParam is not string firstArg || secondParam is not string secondArg)
{
throw new FormatException();
}
args.Result = firstArg.EndsWith(secondArg, StringComparison.InvariantCulture);
}
}
Without change in ncalc you can do this
private static void EndsWith(string name, FunctionArgs args)
{
if (name == "EndsWith")
{
foreach (var parameterExpression in args.Parameters)
parameterExpression.Options &= ~EvaluateOptions.IterateParameters;
var firstParam = args.Parameters[0].Evaluate();
var secondParam = args.Parameters[1].Evaluate();
if (firstParam is not string firstArg || secondParam is not string secondArg)
{
throw new FormatException();
}
args.Result = firstArg.EndsWith(secondArg, StringComparison.InvariantCulture);
}
}
Hi,
when implenting custom string functions for NCalc, we discovered a bug which prevents using
FunctionArgs.Parameters[N].Evaluate()
along withEvaluateOptions.IterateParamers
when evaluating parameters in custom function evaluators.Evaluated string parametes are treated as IEnumerable, and therefore fail the same count check in Expression, see:
ncalc/src/NCalc/Expression.cs
Line 284 in 358ed95
Minimal code to reproduce:
Version: NCalcSync 3.8.0
The text was updated successfully, but these errors were encountered: